Assert
Provides runtime assertions
|
Assert provides functions for assorting preconditions (such as parameter types) and postconditions. More...
Static Public Member Functions | |
static | precondition ( $condition, $description) |
Checks a precondition, that is, throws a PreconditionException if $condition is false. | |
static | parameter ( $condition, $name, $description) |
Checks a parameter, that is, throws a ParameterAssertionException if $condition is false. | |
static | parameterType ( $types, $value, $name) |
Checks an parameter's type, that is, throws a InvalidArgumentException if $value is not of $type. | |
static | parameterKeyType ( $type, $value, $name) |
static | parameterElementType ( $types, $value, $name) |
Checks the type of all elements of an parameter, assuming the parameter is an array, that is, throws a ParameterElementTypeException if any elements in $value are not of $type. | |
static | nonEmptyString ( $value, $name) |
static | postcondition ( $condition, $description) |
Checks a postcondition, that is, throws a PostconditionException if $condition is false. | |
static | invariant ( $condition, $description) |
Checks an invariant, that is, throws a InvariantException if $condition is false. | |
Assert provides functions for assorting preconditions (such as parameter types) and postconditions.
It is intended as a safer alternative to PHP's assert() function.
Note that assertions evaluate expressions and add function calls, so using assertions may have a negative impact on performance when used in performance hotspots. The idea if this class is to have a neat tool for assertions if and when they are needed. It is not recommended to place assertions all over the code indiscriminately.
For more information, see the README file.
@license MIT
|
static |
Checks an invariant, that is, throws a InvariantException if $condition is false.
This is very similar Assert::postcondition() but is intended for use throughout the code.
instead of
Assert::invariant( false, '…' )`. Code checking tools will complain about the latter.bool | $condition | |
string | $description | The message to include in the exception if the condition fails. |
InvariantException | @phan-assert-true-condition $condition |
|
static |
string | $value | |
string | $name |
ParameterTypeException | if $value is not a non-empty string. @phan-assert non-empty-string $value |
|
static |
Checks a parameter, that is, throws a ParameterAssertionException if $condition is false.
This is similar to Assert::precondition().
bool | $condition | |
string | $name | The name of the parameter that was checked. |
string | $description | The message to include in the exception if the condition fails. |
ParameterAssertionException | if $condition is not true. @phan-assert-true-condition $condition |
|
static |
Checks the type of all elements of an parameter, assuming the parameter is an array, that is, throws a ParameterElementTypeException if any elements in $value are not of $type.
string | string[] | $types | The elements' expected type. Can be the name of a native type or a class or interface. Multiple types can be given in an array (or a string separated by a pipe character ("|"), for compatibility with versions before 0.5.0). |
array | $value | The parameter's actual value. If this is not an array, a ParameterTypeException is raised. |
string | $name | The name of the parameter that was checked. |
ParameterTypeException | If $value is not an array. |
ParameterElementTypeException | If an element of $value is not of type (or, for objects, is not an instance of) $type. |
|
static |
string | $type | Either "integer" or "string". Mixing "integer|string" is not supported because this is PHP's default anyway. It is of no value to check this. |
array | $value | The parameter's actual value. If this is not an array, a ParameterTypeException is raised. |
string | $name | The name of the parameter that was checked. |
ParameterTypeException | if one of the keys in the array $value is not of type $type. |
|
static |
Checks an parameter's type, that is, throws a InvalidArgumentException if $value is not of $type.
This is really a special case of Assert::precondition().
string | string[] | $types | The parameter's expected type. Can be the name of a native type or a class or interface, or a list of such names. For compatibility with versions before 0.4.0, multiple types can also be given separated by pipe characters ("|"). |
mixed | $value | The parameter's actual value. |
string | $name | The name of the parameter that was checked. |
ParameterTypeException | if $value is not of type (or, for objects, is not an instance of) $type. |
|
static |
Checks a postcondition, that is, throws a PostconditionException if $condition is false.
This is very similar Assert::invariant() but is intended for use only after a computation is complete.
bool | $condition | |
string | $description | The message to include in the exception if the condition fails. |
PostconditionException | @phan-assert-true-condition $condition |
|
static |
Checks a precondition, that is, throws a PreconditionException if $condition is false.
For checking call parameters, use Assert::parameter() instead.
This is provided for completeness, most preconditions should be covered by Assert::parameter() and related assertions.
bool | $condition | |
string | $description | The message to include in the exception if the condition fails. |
PreconditionException | if $condition is not true. @phan-assert-true-condition $condition |