mehr-als-nix / preconditions
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-08-29 03:32:20 UTC
README
前置条件旨在提供一种替代方式来确保特定方法的先决条件。
可用检查
- checkArgument()
- checkArgNotNull()
- checkNotNull()
- checkElementIndex()
- checkPositionIndex()
- checkPositionIndexes()
- checkState()
- checkValue()
附加异常
- IndexOutOfBoundsException
- NullPointerException
- IllegalStateException
示例
代替编写
if ($count <= 0) {
throw new \InvalidArgumentException("must be positive: " . $count);
}
您可以使用前置条件,例如
use \MehrAlsNix\Preconditions\PreconditionUtil;
//...
PreconditionUtil::checkArgument($count <= 0, 'must be positive: %s', $count);