mehr-als-nix/preconditions

Preconditions for PHP

Maintainers

Package info

github.com/MehrAlsNix/Preconditions

Homepage

pkg:composer/mehr-als-nix/preconditions

Statistics

Installs: 23

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

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

This package is auto-updated.

Last update: 2026-03-01 00:10:08 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);