Skip to content

Preconditions for PHP is a static util class providing an assert like framework.

License

Notifications You must be signed in to change notification settings

MehrAlsNix/Preconditions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Preconditions

Build Status

Preconditions are meant to be an alternative way to ensure that a precondition for a specific method is given.

Available checks

  • checkArgument()
  • checkArgNotNull()
  • checkNotNull()
  • checkElementIndex()
  • checkPositionIndex()
  • checkPositionIndexes()
  • checkState()
  • checkValue()

Additional exceptions

  • IndexOutOfBoundsException
  • NullPointerException
  • IllegalStateException

Example

Instead of writing

    if ($count <= 0) {
        throw new \InvalidArgumentException("must be positive: " . $count);
    }

you could use a precondition like

    use \MehrAlsNix\Preconditions\PreconditionUtil;
    //...
    PreconditionUtil::checkArgument($count <= 0, 'must be positive: %s', $count);