PHPackages                             dgame/php-ensurance - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. dgame/php-ensurance

ActiveLibrary[Testing &amp; Quality](/categories/testing)

dgame/php-ensurance
===================

php ensurance

v2.3.1(1y ago)1139.6k—8.1%18MITPHPPHP &gt;=7.1

Since Aug 11Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Dgame/php-ensurance)[ Packagist](https://packagist.org/packages/dgame/php-ensurance)[ Docs](https://github.com/php-ensurance)[ RSS](/packages/dgame-php-ensurance/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (21)Used By (8)

php-ensurance
=============

[](#php-ensurance)

[![CircleCI](https://camo.githubusercontent.com/9c4e068d77548b13d03468d85353960ea28c9a4ca9b5104275867a23285359b1/68747470733a2f2f636972636c6563692e636f6d2f67682f4467616d652f7068702d656e737572616e63652f747265652f6d61737465722e7376673f7374796c653d737667)](https://circleci.com/gh/Dgame/php-ensurance/tree/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7a52dad1f5337bc1616d2a320e22cfa2fdecfac59c5ae43985ac37da0e9425aa/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4467616d652f7068702d656e737572616e63652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Dgame/php-ensurance/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/829b912f087c0db9fe359974d87cbfb43c1b5bb4f4c167f9a66fc519f66a2a6a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4467616d652f7068702d656e737572616e63652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Dgame/php-ensurance/?branch=master)[![Build Status](https://camo.githubusercontent.com/70e48d17d2cbd302bb76806cf0e6a438fc380574e41147548f461f0febff5243/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4467616d652f7068702d656e737572616e63652f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Dgame/php-ensurance/build-status/master)[![StyleCI](https://camo.githubusercontent.com/2d2c01c60243873c95ea34a9608b1207e2087ddff8495e80bc3b61a705791446/68747470733a2f2f7374796c6563692e696f2f7265706f732f36333439333737352f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/63493775)

design by contract for PHP
--------------------------

[](#design-by-contract-for-php)

If your check fails, an Exception is thrown

Strings
-------

[](#strings)

### equality

[](#equality)

```
ensure('foo')->isString()->isEqualTo('foo');
ensure('foo')->isString()->isNotEqualTo('bar');
```

### pattern

[](#pattern)

```
ensure('test@foo')->isString()->matches('#^[a-z]+@\w{3}$#i');
ensure('FooBar')->isString()->beginsWith('Fo');
ensure('FooBar')->isString()->endsWith('ar');
```

### size

[](#size)

```
ensure('foo')->isString()->hasLengthOf(3);
ensure('foo')->isString()->isShorterThan(4);
ensure('foo')->isString()->isLongerThan(2);
```

and more

Numerics
--------

[](#numerics)

### type check

[](#type-check)

```
ensure(42)->isInt();
ensure('42')->isInt();

ensure(4.2)->isFloat();
ensure('4.2')->isFloat();
```

### value check

[](#value-check)

```
ensure(42)->isNumeric()->isGreaterThan(23);
ensure(23)->isNumeric()->isLessThan(42);
ensure(42)->isEqualTo(42);
```

### positive / negative

[](#positive--negative)

```
foreach (range(0, 100) as $n) {
    ensure($n)->isPositive();
}
```

```
foreach (range(-1, -100) as $n) {
    ensure($n)->isNegative();
}
```

### even / odd

[](#even--odd)

```
for ($i = 0; $i < 42; $i += 2) {
    ensure($i)->isEven();
}
```

```
for ($i = 1; $i < 42; $i += 2) {
    ensure($i)->isOdd();
}
```

### between range

[](#between-range)

```
ensure(2)->isNumeric()->isBetween(1, 3);
```

array
-----

[](#array)

### check for a key

[](#check-for-a-key)

```
ensure(['a' => 'b'])->isArray()->hasKey('a');
```

### check for a value

[](#check-for-a-value)

```
ensure(['a', 'b'])->isArray()->hasValue('a');
```

### check length

[](#check-length)

```
ensure([])->isArray()->hasLengthOf(0);
ensure(range(0, 99))->isArray()->hasLengthOf(100);
```

```
ensure([1, 2, 3])->isArray()->isShorterThan(4);
ensure([1, 2, 3])->isArray()->isLongerThan(2);
```

### check if associativ or not

[](#check-if-associativ-or-not)

```
ensure(['a' => 'b'])->isArray()->isAssociative();
```

ensure not empty / not null
---------------------------

[](#ensure-not-empty--not-null)

```
ensure('')->isNotNull()->isNotEmpty();
```

ensure identity (`===`) / equality (`==`)
-----------------------------------------

[](#ensure-identity---equality-)

```
ensure(42)->isEqualTo('42');
```

```
ensure(42)->isIdenticalTo(42);
```

bool
----

[](#bool)

### is true / false

[](#is-true--false)

```
ensure((2 * 3) === (3 * 2))->isTrue();
ensure((2 * 3) === (3 * 3))->isFalse();
```

---

You can also specify your own Exception messages:

```
ensure(1 === 1)->isTrue()->orThrow('You will never see this error');
```

Enforcement
===========

[](#enforcement)

If you want to enforce that some condition is true, use `enforce`:

```
enforce(true)->orThrow('That is not true...');
```

If you don't specify a Throwable, an `AssertionError` will be used:

```
enforce(0); // throws AssertionError
```

Expectations
============

[](#expectations)

Bind expectations to your values and offer default values if the expectation don't apply. You can either use `else` or `then` to evaluate if an Throwable was thrown. The usage of `else` or `then` will disregard and invalidate the Throwable internally:

```
$this->assertEquals('foo', ensure(42)->isEven()->then('foo'));
$this->assertEquals(23, ensure(42)->isOdd()->else(23));
```

also you can use `either ... or` to set values for both outcomes:

```
$this->assertTrue(ensure(42)->isOdd()->either(false)->or(true));
$this->assertFalse(ensure(23)->isOdd()->either(false)->or(true));
```

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance50

Moderate activity, may be stable

Popularity33

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 98.1% 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 ~168 days

Recently: every ~640 days

Total

20

Last Release

365d ago

Major Versions

v0.5.1 → v1.0.02017-11-22

v1.1.0 → v2.0.02018-04-04

PHP version history (4 changes)v0.1.0PHP &gt;=7.0

v1.0.0PHP ^7

v2.1.0PHP ^7.1

v2.3.0PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/2a9fa98c1a3e70a521430fc2fba266657b2c981b5d8a36bf236fad01f9846dcd?d=identicon)[Dgame](/maintainers/Dgame)

---

Top Contributors

[![Dgame](https://avatars.githubusercontent.com/u/2406877?v=4)](https://github.com/Dgame "Dgame (104 commits)")[![christianhaberland](https://avatars.githubusercontent.com/u/22856528?v=4)](https://github.com/christianhaberland "christianhaberland (1 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

---

Tags

constraintsdesign-by-contractsensurancephpassertionEnforcementEnsuranceDesign by contract

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/dgame-php-ensurance/health.svg)

```
[![Health](https://phpackages.com/badges/dgame-php-ensurance/health.svg)](https://phpackages.com/packages/dgame-php-ensurance)
```

###  Alternatives

[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[zenstruck/assert

Standalone, lightweight, framework agnostic, test assertion library.

8214.9M8](/packages/zenstruck-assert)[fr3d/swagger-assertions

Test your API requests and responses against your swagger definition

138850.9k5](/packages/fr3d-swagger-assertions)[elliotchance/concise

Concise is test framework for using plain English and minimal code, built on PHPUnit.

45223.8k4](/packages/elliotchance-concise)[matthiasnoback/phpunit-asynchronicity

Library for asserting things that happen asynchronously with PHPUnit

36229.0k7](/packages/matthiasnoback-phpunit-asynchronicity)[respect/assertion

The power of Respect\\Validation into an assertion library

2828.0k](/packages/respect-assertion)

PHPackages © 2026

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