PHP Classes
elePHPant
Icontem

Checker: Validate the values of function arguments

Recommend this page to a friend!
  Info   View files Documentation   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2014-09-06 (1 year ago) RSS 2.0 feedNot yet rated by the usersTotal: 135 This week: 1All time: 8,185 This week: 882Up
Version License PHP version Categories
checker 1.0BSD License5.3PHP 5, Validation, Traits
Description Author

This class can validate the values of function arguments.

It can take the name and the value of arguments passed to a function and check the values to verify if they are of the expected types.

Currently it can check if the argument value is a resource, callable, boolean, array, non-empty array, string, string or null, non-empty string, non-empty string or null, non-negative integer, non-negative integer or null, positive integer, positive integer or null, float, positive float, object, object or null.

When the argument does not match the expected type, the class throws an exception.

Picture of wapmorgan
  Performance   Level  
Name: wapmorgan is available for providing paid consulting. Contact wapmorgan .
Classes: 22 packages by
Country: Russian Federation Russian Federation
Age: ???
All time rank: 107135 in Russian Federation Russian Federation
Week rank: 161 Up12 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 8x

Winner: 1x

Details

Checker is a utility for classes self-testing. It provides a useful set of testing methods that can test ins and outs for every method.

The idea

You declare a function or method like the following. It simply create a file with given filepath, fills it up with numberOfRetries, and handle some options via additionalParameters.

    function doSomeWork($filepath, $numberOfRetries, array $additionalParameters) {
        // ..
        $fmode = (isset($additionalParameters['fmode']) ? $additionalParameters['fmode'] : 'w';
        $fp = fopen($filepath, $fmode);
        $numberOfRetries++;
        $i = 0;
        while ($numberOfRetries--) {
            fwrite($fp, "Try #".($i++)."\n");
        }
        fclose($fp);
        return true;
    }

This method is simply written, without any checking for errors in ins. It's possible that user's of your class code in purely innocence replace the first parameter with bool, isn't it? Your code in this situation will work unpredictable for you and most likely throws a php error in line $fp = fopen($filepath). The users of your code can use your classes wrong and most likely they do not want to harm.

To improve code quality and improve methods self-documenting in php I suggest using Checker. Previously listed function can use Checker and avoid occasional terminations of your code.

Intro

Checker has two implementations. Priority belongs to CheckerTrait, a trait. See example in examples/intro.trait.php.

For old code (5.3.x) and non-class functions exists Checker, a class. See example in examples/intro.class.php.

They both exists in SelfTestingCode namespace.

Work flow

Both realisations has the same methods:

  • checkResult(value, checker, options)
  • checkArgument([argName, argValue], checker, options)
  • check(value, checker, options)

You should not even try to call them from user-land code.

Extending

If you would like to add a checker, you need to create a trait (or class) extending CheckerTrait or Checker.

See CheckerTrait extending example in examples/extending.trait.php.

See Checker extending example in examples/extending.class.php.

Checker - is a simple static method in CheckerTrait or Checker that actually does call to checkResult() or checkArgument().

Testing

In place where you want test ins or outs of your program make a call to one of them (Checkers). Checkers testing ins has the arg prefix and has that signature: argScalar($argName, $argValue)

Checkers testings outs has the ret prefix and has that signature: retResource($value)

Testing effect

Checkers testing ins:

  • does not return any value
  • does not impact on arg value
  • uses argName in error output

See example in examples/testing.ins.php.

Checkers testing outs:

  • return the value
  • does not impact on value
  • uses 'return of result statement'

See example in examples/testing.outs.php.

Throwing errors

If any test fails, checker throws an exception with properly formed message explaining what type it was waiting for and what it got.

Checkers

A lot of testing algorithms bundled into Checker.

  • ins tests * scalar * string * string-or-null * string-non-empty * string-non-empty-or-null * int * int-or-null * float * float-or-null * object-or-null array-of-instances.Accept 'class' parameter*.
  • outs tests * resource * callable * bool * array * empty-array * non-empty-array * int-positive * int-negative * float-positive * float-negative * int-positive-or-zero * int-negative-or-zero * float-positive-or-zero * float-negative-or-zero * object instance-of.Accept 'class' parameter.*

Checker method naming convention

in string-or-null = argStringOrNull

in string-non-empty-or-null = argNonEmptyStringOrNull

The same is applicable to out.

Outs testing

In order to provide transparent outs testing these checkers return value. See example in examples/testing.outs.php.

  Files folder image Files  
File Role Description
Files folder imageexamples (6 files)
Files folder imageSelfTestingCode (2 files)
Plain text file Checker.php Class Class file
Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file doc.md Doc. Documentation
Plain text file example.php Example Example script
Plain text file OldChecker.php Class Class source
Plain text file OldChecker.README.md Data Auxiliary data
Plain text file README.md Data Auxiliary data

  Files folder image Files  /  examples  
File Role Description
  Plain text file extending.class.php Example Example script
  Plain text file extending.trait.php Example Example script
  Plain text file intro.class.php Example Example script
  Plain text file intro.trait.php Example Example script
  Plain text file testing.ins.php Example Example script
  Plain text file testing.outs.php Example Example script

  Files folder image Files  /  SelfTestingCode  
File Role Description
  Plain text file Checker.php Class Class source
  Plain text file CheckerTrait.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 86%
Total:135
This week:1
All time:8,185
This week:882Up