Safe-conducts for your PHP. Kills up to 99.9% of bad arguments.
WIP.
function add($a, $b)
{
\Guard\Conditions::requires($a, 'a')->isNumeric();
\Guard\Conditions::requires($b, 'b')->isNumeric();
return $a + $b;
}
add(1, 2);
add('string', 2);
# => \Guard\Exception\ConditionEvaluationException(
# 'The condition "isNumeric" for argument "a" failed to evaluate.',
# \Guard\Exception\InvalidArgumentException(
# 'The value "\'string\'" is not numeric.'
# )
# )
# Run this in your terminal to get the latest Composer version:
$ curl -sS https://getcomposer.org/installer | php
# ...or if you don't have cURL:
$ php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
# Add Guard as a dependency:
$ php composer.phar require 'stan-angeloff/guard'
After installing, you need to require Composer's autoloader file:
require 'vendor/autoload.php';
To enforce conditions on your arguments, use:
\Guard\Conditions::requires($value, $argumentName)
->condition1()
->condition2('option, e.g., interface name');
where $value
is the argument value and $argumentName
is the argument name (see example above). Conditions can be chained.
-
instanceOf($className)
Throws an exception if the value is not an object or an instance of the expected
$className
. -
isNotNull()
Throws an exception if the value is
NULL
(empty values are allowed, e.g., an empty string''
). -
isNumeric()
Throws an exception if the value is not numeric.
-
typeOf($internalType)
Throws an exception if the type of the value is not the expected one.
See gettype for a full list of possible types.