, (*1)
, (*2)
PHP Assertions
Library to ease defensive and design by contract (DbC) programming with
assert()
in PHP., (*3)
Installation
Open a terminal, enter your project directory and execute the following command
to add this package to your dependencies:, (*4)
$ composer require fleshgrinder/assertion
This command requires you to have Composer installed globally, as explained in
the installation chapter of the Composer documentation., (*5)
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., (*6)
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:, (*7)
// 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., (*8)
Defensive Programming / Design by Contract
βBe polite, Never Assert, (*9)
Avoid the assert()
mechanism, because it could turn a three-day debug fest
into a ten minute one.β, (*10)
β How to Write Unmaintainable Code, Roedy Green., (*11)
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., (*12)
Configuration
Assertions need to be configured appropriately in order to be useful during
Development as well as in Production., (*13)
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
, β¦, (*14)
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., (*15)
FAQ
Why is the class called Variable?, (*16)
In order to result in nice English sentences, just look at the following:, (*17)
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?, (*18)
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., (*19)
Why does the class not follow PSR-4 (and the associated vendor prefixing to
avoid conflicts) while the tests do?, (*20)
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., (*21)
Credits
Credit where credit is due: this library was inspired by Drupalβs
Inspector class., (*22)
Weblinks
- The PHP Group: β
assert()
β
- Steve McConnell: βCode Complete: 2nd Edition,β
2004, Microsoft Press, 960 pages. ISBN: 0735619670. Section 8.2 in particular
but there is more general advice in regards to defensive programming and how
to use
assert()
effectively.
- Aki Tendo, et al.: βAdding Assertions to Drupal - Test Tools.β
- Jess (xjm), et al.: β[policy, no patch] Define best practices for using and testing assertions and document them before adding assertions to coreβ
- Aki Tendo: βRuntime Assertions have been added to Drupal core,β September 29, 2015.
- Drupal contributors: βWell Formed Errors Initiative,β March 6, 2015.
- Stackoverflow contributors: βWhen should assertions stay in production code?β
- Wikipedia contributors: βAssertion (software development)β
- Wikipedia contributors: βDesign by contractβ
- Wikipedia contributors: βException handlingβ
License
, (*23)