mehr-als-nix / preconditions
Preconditions for PHP
Installs: 23
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/mehr-als-nix/preconditions
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^5.0
This package is auto-updated.
Last update: 2025-09-29 02:01:22 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);