zheltikov / php-invariant
Installs: 1 933
Dependents: 5
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- zheltikov/php-exceptions: ^1.0
Requires (Dev)
Suggests
- zheltikov/php-type-assert: Provides type checking/assertion functionality.
This package is auto-updated.
Last update: 2024-10-25 16:38:00 UTC
README
Yet another assertion library.
Installation
You can install this library via composer:
$ composer require zheltikov/php-invariant
Usage
The main function to be used in this library is invariant
. This function is intended to indicate a programmer error
for a condition that should never occur, similar
to assertions. For example:
<?php require_once(__DIR__ . '/../vendor/autoload.php'); use function Zheltikov\Invariant\invariant; $obj = new DateTime(); invariant($obj instanceof DateTime, 'Object must have type DateTime'); $p = 123; invariant($p !== null, 'Value can\'t be null'); $max = 100; invariant($p !== null && $p <= $max, '$p\'s value %d must be <= %d', $p, $max);
If the first argument value evaluates to true, the program continues execution; otherwise, the
function invariant_violation
is called. That function either throws an InvariantException
, or calls the handler
previously registered by the function invariant_callback_register
.
The first argument is evaluated as a boolean expression. The second argument is a string that can contain text and/or
optional formatting information as understood by the function sprintf
. The optional
comma-separated list of values following the string must match the set of types expected by the optional formatting
information inside that string.
TODO
- Build a functional-style assertion library
- Build an object-oriented interface for these functions