Download the PHP package fleshgrinder/assertion without Composer
On this page you can find all versions of the php package fleshgrinder/assertion. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fleshgrinder/assertion
More information about fleshgrinder/assertion
Files in fleshgrinder/assertion
Package assertion
Short Description Library to ease defensive and design by contract (DbC) programming with assert() in PHP.
License MIT
Informations about the package assertion
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:
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:
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 Production.
Development
Production
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:
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”