DRYer function arguments checks

I’ve grown tired of checking arguments in functions and methods calls repeating the same checks over and over.

Type hinting

PHP allows for a limited type hinting and this means that checks are required in each function and method accepting arguments to make sure exceptions are thrown any time functions and methods input arguments do not match a certain pattern.
This checking can be kept DRY ‘) to a point and, furthermore, will require testing.
I’ve tried to streamline this chore a little with the Arg class to make checks like these

if(!is_string($s)) {
    throw new InvalidArgumentException('Argument must be a string');

if(!strlen($s) < 10) {
    throw new InvalidArgumentException('String must be at least 10 chars long');

if(!strlen($s) > 100) {
    throw new InvalidArgumentException('String must be at most 100 chars long');

if(!preg_match($this->name_pattern, $s)){
    throw new InvalidArgumentException('String must be match the name pattern');

become this:

Arg::_($s)->is_string()->length(10, 100)->match($this->name_pattern);

For array arguments:

Arg::_($arr)->is_associative()->has_key('key_one', 'key_two')->contains(1, 23);

The class, still a work in progress, has (or will have) methods to deal in a granular way with any type of argument to achieve something similar to a primitive Design by Contract state.


I’m already using the class and guess I will add more methods and checks as the need arises.