fleshgrinder/assertion

This package is abandoned and no longer maintained. No replacement package was suggested.

Library to ease defensive and design by contract (DbC) programming with assert() in PHP.

2.0.0 2018-01-09 19:18 UTC

This package is not auto-updated.

Last update: 2020-02-05 02:01:29 UTC


README

Latest Stable Version Latest Unstable Version Travis Build status License Total Downloads

Coveralls branch Scrutinizer SensioLabs Insight Code Climate: GPA Code Climate: Issues

PHP Assertions

Library to ease defensive and design by contract (DbC) programming with assert() in PHP.

Installation

Open a terminal, enter your project directory and execute the following command to add this package to your dependencies:

$ composer require fleshgrinder/assertion

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

NOTE: Do not install the library as a development requirement because composer does not install them when a library is installed as a dependency of another library. You want your assertions to be executed at all times, except when the full application goes into production; which is managed through the configuration.

Usage

This library provides a single purely static class that can be used in assertions to ease repetitive scalar inspections as well as writing custom inspections on top of it. The following code example illustrates the basic usage:

// Use built-in functions whenever possible.
assert(is_string($var), 'variable must be of type string.');
assert(Variable::isStringWithContent($var), 'variable must be of type string with content');
assert(Variable::isStringable($var), 'variable must be of type string or a convertible object');
assert(Variable::isStringableWithContent($var), 'variable must be of type string or a convertible object with content');

// Again, use built-in functions whenever possible.
assert($var === null || Variable::isInteger($var), 'variable must be NULL or an integer (ℤ)');
assert($var === null || Variable::isNaturalNumber($var), 'variable must be NULL or a natural number (ℕ₀)');
assert($var === null || Variable::isScalarPositiveNaturalNumber($var), 'variable must be NULL or a positive natural number (ℕ₁) of type int');

Big Floats

The PHP extension BC Math is required to validate big float numbers. It is not required but a E_USER_NOTICE error is triggered if a big float is encountered and the extension is not available. PHP must be compiled with the --enable-bcmath flag and the extension is always loaded on Windows systems.

Defensive Programming / Design by Contract

Be polite, Never Assert

Avoid the assert() mechanism, because it could turn a three-day debug fest into a ten minute one.”

How to Write Unmaintainable Code, Roedy Green.

Be sure to check the Weblinks section and read through all the sources to find out what assertions are good for, when to use them, and when not. Feel free to open an issue if you are still in doubt.

Configuration

Assertions need to be configured appropriately in order to be useful during Development as well as in Production.

Development

assert.exception = 1
zend.assertions  = 1

Production

assert.exception = 0
zend.assertions  = -1

Note

Default functions that are already provided by PHP are not redefined in this library. Use the language functions whenever possible, e.g.: is_string, is_int, is_float, is_numeric, …

However, many of the filter functions are exposed via methods that do not require additional arguments, for instance the integer and float assertions pass their arguments to the filter function with the appropriate filter and options set. Be careful and consider using is_int and is_float if you actually need the correct type but be even more careful if you need to handle very big numbers, in these cases use this library again since it will fall back to bcmath functions as needed while ensuring best performance by using the most appropriate library in the background.

FAQ

Why is the class called Variable?

In order to result in nice English sentences, just look at the following:

assert(Variable::isPositiveNaturalNumber($id));
// Assert variable (id) is (a) positive natural number.

Why is it a class in the first place and not a collection of procedural functions?

Because PHP (composer) does not support lazy loading of procedural functions and it makes no sense to include the file in production. Using a class on the other hand makes lazy loading possible.

Why does the class not follow PSR-4 (and the associated vendor prefixing to avoid conflicts) while the tests do?

To minimize noise within the assert calls and possible extra work with IDEs (like PhpStorm) that automatically import classes with use statements that do not work nicely together with assert. I consider the likelihood of another class being named after something generic as variable to be very low and thus concluded that above arguments are reason enough to put it in the global namespace.

Credits

Credit where credit is due: this library was inspired by Drupal’s Inspector class.

Weblinks

License

MIT License