PHPackages                             fleshgrinder/assertion - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. fleshgrinder/assertion

AbandonedArchivedLibrary[Debugging &amp; Profiling](/categories/debugging)

fleshgrinder/assertion
======================

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

2.0.0(8y ago)425.5k2[4 issues](https://github.com/Fleshgrinder/php-assertion/issues)MITPHPPHP ^7.0

Since Jan 10Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Fleshgrinder/php-assertion)[ Packagist](https://packagist.org/packages/fleshgrinder/assertion)[ RSS](/packages/fleshgrinder-assertion/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (3)Dependencies (5)Versions (4)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/20f2434c8b612191cfb9a87af01800053eae1e972b13d8927ba5364f5bed4540/68747470733a2f2f706f7365722e707567782e6f72672f666c6573686772696e6465722f617373657274696f6e2f762f737461626c65)](https://packagist.org/packages/fleshgrinder/assertion)[![Latest Unstable Version](https://camo.githubusercontent.com/ad48cda6c36c2511d4d652bb2b8f8f881e60dfdd15bb7d07ad35e1146acc934c/68747470733a2f2f706f7365722e707567782e6f72672f666c6573686772696e6465722f617373657274696f6e2f762f756e737461626c65)](https://packagist.org/packages/fleshgrinder/assertion)[![Travis](https://camo.githubusercontent.com/f5a663c02e2e3196508bedb49b44fbb84214e5239a2542603fe5cda90c04f925/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f466c6573686772696e6465722f7068702d617373657274696f6e2e737667)](https://travis-ci.org/Fleshgrinder/php-assertion)[![Build status](https://camo.githubusercontent.com/5727df5f03e66b9055ece83da46e775ccbf19ff2b3201993369fb991107136a3/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f6466796661637635396d706d6d6864742f6272616e63682f6d61737465723f7376673d74727565)](https://ci.appveyor.com/project/Fleshgrinder/php-assertion/branch/master)[![License](https://camo.githubusercontent.com/03312d831f9f1aa3071ab315ee04aecf156180b507b5a5d9bdf2991d198d65f6/68747470733a2f2f706f7365722e707567782e6f72672f666c6573686772696e6465722f617373657274696f6e2f6c6963656e7365)](https://packagist.org/packages/fleshgrinder/assertion)[![Total Downloads](https://camo.githubusercontent.com/5d5f971a381dcb2b339b20f8ce29d01e77dd84c631fb22288bd61b5ac0d1c887/68747470733a2f2f706f7365722e707567782e6f72672f666c6573686772696e6465722f617373657274696f6e2f646f776e6c6f616473)](https://packagist.org/packages/fleshgrinder/assertion)

[![Coveralls branch](https://camo.githubusercontent.com/70cb792f99984e5f2e1433e12968384a72fefb56775d727d68decb368f24143f/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f466c6573686772696e6465722f7068702d617373657274696f6e2f6d61737465722e737667)](https://coveralls.io/github/Fleshgrinder/php-assertion)[![Scrutinizer](https://camo.githubusercontent.com/fa0f06ab163f0db3ae04be4f95930cd831c6f5e976685507d44e677a16b57ac8/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f466c6573686772696e6465722f7068702d617373657274696f6e2e737667)](https://scrutinizer-ci.com/g/Fleshgrinder/php-assertion/)[![SensioLabs Insight](https://camo.githubusercontent.com/23d4ec194321f49c1be2b52fb78a12c8fc0b6c689d58050cf91d029f919fc777/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f36313534306538652d326361392d343130322d626434332d3362396335613061333865322e737667)](https://insight.sensiolabs.com/projects/61540e8e-2ca9-4102-bd43-3b9c5a0a38e2)[![Code Climate: GPA](https://camo.githubusercontent.com/8c8da2d60cdd73776ccbcbe645becae53c68fd28fc5b00c1dc3fa2fdeb1756bb/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6769746875622f466c6573686772696e6465722f7068702d617373657274696f6e2e737667)](https://codeclimate.com/github/Fleshgrinder/php-assertion)[![Code Climate: Issues](https://camo.githubusercontent.com/bb2c50219535d3209d4562ce68604ccde7697d6155de392a33f0796cb76c3a4c/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6973737565732f6769746875622f466c6573686772696e6465722f7068702d617373657274696f6e2e737667)](https://codeclimate.com/github/Fleshgrinder/php-assertion/issues)

PHP Assertions
==============

[](#php-assertions)

Library to ease defensive and design by contract (DbC) programming with [`assert()`](https://secure.php.net/assert) in PHP.

Installation
------------

[](#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](https://getcomposer.org/doc/00-intro.md) 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](#configuration).

Usage
-----

[](#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

[](#big-floats)

The PHP extension [BC Math](https://secure.php.net/bcmath) 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

[](#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](https://thc.org/root/phun/unmaintain.html), Roedy Green.

Be sure to check the [Weblinks](#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

[](#configuration)

Assertions need to be configured appropriately in order to be useful during [Development](#Development) as well as in [Production](#Production).

#### Development

[](#development)

```
assert.exception = 1
zend.assertions  = 1
```

#### Production

[](#production)

```
assert.exception = 0
zend.assertions  = -1
```

### Note

[](#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

[](#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
-------

[](#credits)

Credit where credit is due: this library was inspired by [Drupal’s](https://www.drupal.org/)[Inspector class](https://github.com/drupal/drupal/blob/8.0.x/core/lib/Drupal/Component/Assertion/Inspector.php).

Weblinks
--------

[](#weblinks)

- The PHP Group: “[*`assert()`*](https://secure.php.net/assert)”
- Steve McConnell: “[*Code Complete: 2nd Edition*](http://www.stevemcconnell.com/cc.htm),” 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.*](https://www.drupal.org/node/2408013)”
- Jess (xjm), et al.: “[*\[policy, no patch\] Define best practices for using and testing assertions and document them before adding assertions to core*](https://www.drupal.org/node/2548671)”
- Aki Tendo: “[*Runtime Assertions have been added to Drupal core*](https://www.drupal.org/node/2569701),” September 29, 2015.
- Drupal contributors: “[*Well Formed Errors Initiative*](https://www.drupal.org/node/2412507),” March 6, 2015.
- Stackoverflow contributors: “[*When should assertions stay in production code?*](http://stackoverflow.com/questions/17732)”
- Wikipedia contributors: “[*Assertion (software development)*](https://en.wikipedia.org/wiki/Assertion_%28software_development%29)”
- Wikipedia contributors: “[*Design by contract*](https://en.wikipedia.org/wiki/Design_by_contract)”
- Wikipedia contributors: “[*Exception handling*](https://en.wikipedia.org/wiki/Exception_handling)”

License
-------

[](#license)

[![MIT License](https://camo.githubusercontent.com/f8370bd5022685ccc4cdebd136a849a7692c5afd674e59ca8805d90f2aa29a72/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f632f63332f4c6963656e73655f69636f6e2d6d69742e7376672f343870782d4c6963656e73655f69636f6e2d6d69742e7376672e706e67)](https://opensource.org/licenses/MIT)

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 96.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~365 days

Total

3

Last Release

3047d ago

Major Versions

1.1.0 → 2.0.02018-01-09

PHP version history (2 changes)v1.0.0PHP &gt;=5.6.0

2.0.0PHP ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/6680298143064cd3ac63c0097ef724d46f47221ae0990db445081c4e8a923e05?d=identicon)[Fleshgrinder](/maintainers/Fleshgrinder)

---

Top Contributors

[![Fleshgrinder](https://avatars.githubusercontent.com/u/1059453?v=4)](https://github.com/Fleshgrinder "Fleshgrinder (57 commits)")[![lrobi2014](https://avatars.githubusercontent.com/u/8844201?v=4)](https://github.com/lrobi2014 "lrobi2014 (1 commits)")[![markuspoerschke](https://avatars.githubusercontent.com/u/1222377?v=4)](https://github.com/markuspoerschke "markuspoerschke (1 commits)")

---

Tags

assertionserror-handlingphptypes

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fleshgrinder-assertion/health.svg)

```
[![Health](https://phpackages.com/badges/fleshgrinder-assertion/health.svg)](https://phpackages.com/packages/fleshgrinder-assertion)
```

###  Alternatives

[symfony/stopwatch

Provides a way to profile code

2.8k387.2M918](/packages/symfony-stopwatch)[fruitcake/laravel-debugbar

PHP Debugbar integration for Laravel

19.1k662.9k29](/packages/fruitcake-laravel-debugbar)[spatie/ignition

A beautiful error page for PHP applications.

510147.6M69](/packages/spatie-ignition)[jokkedk/webgrind

Webgrind is a Xdebug profiling web frontend in PHP5. It implements a subset of the features of kcachegrind and installs in seconds and works on all platforms. For quick'n'dirty optimizations it does the job.

3.3k193.0k](/packages/jokkedk-webgrind)[koriym/printo

An object graph visualizer.

1421.8M2](/packages/koriym-printo)[soloterm/dumps

A Laravel command to intercept dumps from your Laravel application.

125285.7k3](/packages/soloterm-dumps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
