phpstan/phpstan-beberlei-assert
PHPStan beberlei/assert extension
Maintainers
Package info
github.com/phpstan/phpstan-beberlei-assert
Type:phpstan-extension
pkg:composer/phpstan/phpstan-beberlei-assert
Requires
- php: ^7.4 || ^8.0
- phpstan/phpstan: ^2.0
Requires (Dev)
- beberlei/assert: ^3.3.0
- php-parallel-lint/php-parallel-lint: ^1.4
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^9.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
MIT 8699be838df954190b657c8defc2777c74ebb44e
This package is auto-updated.
Last update: 2026-06-15 21:52:34 UTC
README
👁 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!
