PHPackages                             jlaswell/assert-php - 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. jlaswell/assert-php

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

jlaswell/assert-php
===================

Simple assertion library

v1.0.2(10mo ago)03MITPHPPHP ^8.3CI passing

Since Nov 8Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/jlaswell/assert-php)[ Packagist](https://packagist.org/packages/jlaswell/assert-php)[ GitHub Sponsors](https://github.com/jlaswell)[ RSS](/packages/jlaswell-assert-php/feed)WikiDiscussions main Synced 1mo ago

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

Assert
======

[](#assert)

Assert is a simple set of assertions to help validate inputs, results, and business logic. Inspired by the thoughts of [TigerBeetle](https://tigerbeetle.com/blog/2023-12-27-it-takes-two-to-contract)and [NASA’s Power of 10 Rules for Safety-Critical Code](https://en.wikipedia.org/wiki/The_Power_of_10:_Rules_for_Developing_Safety-Critical_Code), this library aims to painfully enforce the validity in order to fail without the ability to subvert the issue.

Why not just `assert()`?
------------------------

[](#why-not-just-assert)

`assert()` is a (*deprecated as of 8.3*) built-in PHP method, designed for debugging, that will either throw an exception or omit a warning. `assert-php`is designed to throw exceptions only and to purposefully fail your production code in the cases of invalid expectations.

Usage
-----

[](#usage)

Usage of this library is simple: call one of the methods from the `Assert` class to validate your expectations. Do not attempt to try/catch these assertions, and instead, allow your program to truly fail in these cases. Using the `that()`method provides the best readability in my opinion.

```
// assert that the size of the $records array is greater than zero
Assert::that('array' === gettype($records));
Assert::that(sizeof($records) > 0);
```

### Examples

[](#examples)

```
public function fill(array $values): array
{
    Assert::that(sizeof($values) > 0);
    Assert::that(sizeof($values) === sizeof(array_filter($values, fn ($value) => is_int($value))));

    // logic can now perform on an array with values of integers
}

/**
 * @param Contact[] $contacts
 */
public function save(array $contacts): int
{
    foreach ($contacts as $contact) {
        Assert::that($contact->isValid());
        Assert::that(null === $contact->id);
    }

    // logic can now perform on an array of valid Contacts without a record id
}
```

### All Assertions

[](#all-assertions)

```
