phpstan / phpstan-beberlei-assert
PHPStan beberlei/assert extension
Installs: 6 286 921
Dependents: 118
Suggesters: 2
Security: 0
Stars: 43
Watchers: 3
Forks: 14
Open Issues: 5
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
- 2.0.x-dev
- 2.0.2
- 2.0.1
- 2.0.0
- 1.1.x-dev
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- 0.12.6
- 0.12.5
- 0.12.4
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.2
- 0.11.1
- 0.11
- 0.10
- dev-readme-document-supported-features
- dev-react-on-issue-opened
- dev-add-php-85-to-ci
- dev-gitattributes-export-ignore
- dev-add-claude-md
- dev-renovate/major-github-actions
- dev-renovate/major-root-composer
This package is auto-updated.
Last update: 2026-02-19 21:29:12 UTC
README
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