A little show and tell about the my idea of a streamlined argument checking helper library.
Better solutions for later versions of PHP
Argument checking is a way same or different developers communicate about what a function of method should receive as input in number(as in cardinality) and type; PHP type hinting is suggestive but not exhausting and since half of my development happens in PHP 5.2 (as per WordPress minimum requirements) I even lose the possibility to type-hint the callable
type.
I’ve researched the amazing idea of Design by Contract in the PHP context and what I could find will not run on PHP 5.2 and that’s a pretty much a forgotten chapter in the book.
My solution so far
I came up with a possible solution I’ve presented in a previous post and have worked on it to add a function I missed:
Arg::_($value)->not()->is_string();
the simple not()
method will make sure the method coming after it, in this case is_string()
, will pass if the match is negative (e.g. $value
is not a string).
Next
I’d like to implement another sugar method like _or()
(and method evaluation is the default behaviour) to allow code like this
Arg::_($value)->is_string()->_or()->is_array();
which, in natural language, means “value must be a string OR an array”.
For more complex argument assertions the assert()
method will do.