PHPStan beberlei/assert extension

February 19, 2026 ยท View on GitHub

Build Latest Stable Version License

Description

The main scope of this extension is to help PHPStan to detect the type of object after the Assert\Assertion validation.

<?php declare(strict_types = 1);
use Assert\Assertion;

function demo(?int $a) {
	// ...

	Assertion::integer($a);
	// PHPStan is now aware that $a can no longer be `null` at this point

	return ($a === 10);
}

Supported Assertions

This extension understands the following Assertion static methods and narrows types accordingly:

integer, string, float, numeric, boolean, scalar, objectOrClass, isResource, isCallable, isArray, isInstanceOf, notIsInstanceOf, true, false, null, notNull, same, notSame, subclassOf, integerish, keyExists, keyNotExists, propertyExists, methodExists, classExists, interfaceExists, notBlank, isJsonString

nullOr* Prefix

Every supported assertion can be prefixed with nullOr to accept null in addition to the asserted type:

Assertion::nullOrString($value);
// $value is string|null

all* Prefix

Every supported assertion can be prefixed with all to narrow the item type of arrays and iterables:

/** @var mixed[] $values */
Assertion::allInteger($values);
// $values is array<int>

The allNot* prefix is also supported for allNotNull, allNotIsInstanceOf, allNotSame, and allNotBlank.

Fluent Chain API

The extension supports Assert::that() chains including ->nullOr() and ->all() modifiers:

Assert::that($value)->string();
Assert::that($value)->nullOr()->string(); // string|null
Assert::thatNullOr($value)->string();     // string|null
Assert::that($values)->all()->string();   // array<string>
Assert::thatAll($values)->string();       // array<string>

The function-style API (Assert\that(), Assert\thatNullOr(), Assert\thatAll()) is also supported.

Installation

To use this extension, require it in Composer:

composer require --dev phpstan/phpstan-beberlei-assert

If you also install phpstan/extension-installer then you're all set!

Manual installation

If you don't want to use phpstan/extension-installer, include extension.neon in your project's PHPStan config:

includes:
    - vendor/phpstan/phpstan-beberlei-assert/extension.neon