Download the PHP package hediet/contracts without Composer
On this page you can find all versions of the php package hediet/contracts. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hediet/contracts
More information about hediet/contracts
Files in hediet/contracts
Package contracts
Short Description A library which helps to specify, check and reflect code contracts.
License MIT
Informations about the package contracts
PHP Contracts - A Powerful Assertion Library for PHP
PHP Contracts is an assertion library for PHP to validate arguments and invariants. This library aims to replace my library nunzion/php-expect.
Warning
This library is still in development!
However, the signature of the method Contract::requires
will not change
and it is guaranteed that an exception is beeing thrown if the condition evaluates to false
.
Installation
You can use Composer to download and install PHP Contracts.
To add PHP Contracts to your project, simply add a dependency on hediet/contracts to your project's composer.json
file.
Here is a minimal example of a composer.json
file that just defines a dependency on PHP Contracts:
Usage
Using this library is as simple as calling Contract::requires
with an arbitrary condition:
If the specified condition evaluates to false
, PHP Contracts analyzes the condition and throws
an exception with an appropriate error message.
The following code will throw an \InvalidArgumentException
with the message Argument 'b' must be of type 'integer', but is of type 'string'.
:
Explicitly Supported Tests
Currently PHP Contracts understands the following conditions:
- All is_TYPE functions for TYPE being a primitive.
- Comparison operators like
<
,<=
,>
,>=
. - Disjunction of type constraints, e.g.
is_int($a) || is_float($a) || $a === null
. Internally, a type constraint is being created for $a which denotes that $a must be of typeint|float|null
. - Conjunction of constraints.
However, currently all expressions must be constants or arguments.
Examples
Internals
If the condition evaluates to true
, the requires method returns immediately.
Thus, if a condition does not fail, there is no significant performance decrease.
If a condition fails, i.e. evaluates to false
, the stacktrace is used to determine the location of
the requires
call. After that, the invocation is parsed with nikic/PHP-Parser and converted
to a set of constraints. These constraints may reference expressions to which the constraints are applied to.
By using the stacktrace a second time, the arguments and the value of $this
can be obtained for the context
of the invocation to evaluate these expressions, so that a constraint can add an explanation of why he has failed.
Todos
- Support deep expressions, so that conditions like
Contract::requires(count($a) > 0)
can be analyzed. Since $a may be obtained from the stacktrace and count is a pure method,count($a)
can be evaluated without side effects. - Support various array tests.
- Throw an invariant exception if no argument is referenced in the condition.
- Use optionally provided values to evaluate expressions which uses variables that are neither arguments nor
$this
. - Add a reflection API which parses the first
Contract::requires
calls of a method and returns their corresponding constraints. This enables propagating constraints to the UI.
Author
Henning Dieterichs - [email protected]
License
PHP Expect is licensed under the MIT License.
All versions of contracts with dependencies
nikic/php-parser Version ^1.1.0
nunzion/stacktrace Version ^0.2.1
hediet/type-reflection Version ^0.1.0