webmozart/assert

Assertions to validate method input/output with nice error messages.

Installs: 696 395 200

Dependents: 1 111

Suggesters: 2

Security: 0

Stars: 7 558

Watchers: 32

Forks: 145

Open Issues: 40

1.11.0 2022-06-03 18:03 UTC

README

Latest Stable Version Total Downloads

This library contains efficient assertions to test the input and output of your methods. With these assertions, you can greatly reduce the amount of coding needed to write a safe implementation.

All assertions in the Assert class throw an Webmozart\Assert\InvalidArgumentException if they fail.

FAQ

What's the difference to beberlei/assert?

This library is heavily inspired by Benjamin Eberlei's wonderful assert package, but fixes a usability issue with error messages that can't be fixed there without breaking backwards compatibility.

This package features usable error messages by default. However, you can also easily write custom error messages:

Assert::string($path, 'The path is expected to be a string. Got: %s');

In beberlei/assert, the ordering of the %s placeholders is different for every assertion. This package, on the contrary, provides consistent placeholder ordering for all assertions:

  • %s: The tested value as string, e.g. "/foo/bar".
  • %2$s, %3$s, ...: Additional assertion-specific values, e.g. the minimum/maximum length, allowed values, etc.

Check the source code of the assertions to find out details about the additional available placeholders.

Installation

Use Composer to install the package:

composer require webmozart/assert

Example

use Webmozart\Assert\Assert;

class Employee
{
    public function __construct($id)
    {
        Assert::integer($id, 'The employee ID must be an integer. Got: %s');
        Assert::greaterThan($id, 0, 'The employee ID must be a positive integer. Got: %s');
    }
}

If you create an employee with an invalid ID, an exception is thrown:

new Employee('foobar');
// => Webmozart\Assert\InvalidArgumentException:
//    The employee ID must be an integer. Got: string

new Employee(-10);
// => Webmozart\Assert\InvalidArgumentException:
//    The employee ID must be a positive integer. Got: -10

Assertions

The Assert class provides the following assertions:

Type Assertions

Comparison Assertions

String Assertions

You should check that a value is a string with Assert::string() before making any of the following assertions.

File Assertions

Object Assertions

Array Assertions

Function Assertions

Collection Assertions

All of the above assertions can be prefixed with all*() to test the contents of an array or a \Traversable:

Assert::allIsInstanceOf($employees, 'Acme\Employee');

Nullable Assertions

All of the above assertions can be prefixed with nullOr*() to run the assertion only if the value is not null:

Assert::nullOrString($middleName, 'The middle name must be a string or null. Got: %s');

Extending Assert

The Assert class comes with a few methods, which can be overridden to change the class behaviour. You can also extend it to add your own assertions.

Overriding methods

Overriding the following methods in your assertion class allows you to change the behaviour of the assertions:

  • public static function __callStatic($name, $arguments)
    • This method is used to 'create' the nullOr and all versions of the assertions.
  • protected static function valueToString($value)
    • This method is used for error messages, to convert the value to a string value for displaying. You could use this for representing a value object with a __toString method for example.
  • protected static function typeToString($value)
    • This method is used for error messages, to convert a value to a string representing its type.
  • protected static function strlen($value)
    • This method is used to calculate string length for relevant methods, using the mb_strlen if available and useful.
  • protected static function reportInvalidArgument($message)
    • This method is called when an assertion fails, with the specified error message. Here you can throw your own exception, or log something.

Static analysis support

Where applicable, assertion functions are annotated to support Psalm's Assertion syntax. A dedicated PHPStan Plugin is required for proper type support.

Authors

Contribute

Contributions to the package are always welcome!

License

All contents of this package are licensed under the MIT license.