mehr-als-nix / preconditions
Preconditions for PHP
v0.0.3
2015-11-15 19:44 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^5.0
This package is auto-updated.
Last update: 2024-11-29 04:09:01 UTC
README
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);