fleshgrinder / assertion
Library to ease defensive and design by contract (DbC) programming with assert() in PHP.
Installs: 25 186
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 2
Open Issues: 4
Requires
- php: ^7.0
- symfony/polyfill-php71: ^1.3
Requires (Dev)
- ext-gd: *
- codeclimate/php-test-reporter: ^0.4.2
- phpunit/phpunit: ^5.7
- satooshi/php-coveralls: ^1.0
- scrutinizer/ocular: ^1.3
Suggests
- ext-bcmath: For asserting arbitrary precision numbers.
This package is not auto-updated.
Last update: 2020-02-05 02:01:29 UTC
README
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
- 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”