mehr-als-nix/preconditions

Preconditions for PHP

v0.0.3 2015-11-15 19:44 UTC

This package is auto-updated.

Last update: 2020-01-31 01:05:34 UTC


README

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);