phpstan/phpstan-webmozart-assert
PHPStan extension for webmozart/assert that teaches PHPStan how Assert::* calls narrow types. Supports many assertions (including nullOr*/all*) so values become non-null, specific scalars, instances, arrays, etc. after validation, improving static analysis accuracy.
Begin by installing the package with Composer as a development dependency:
composer require --dev phpstan/phpstan-webmozart-assert
If you use phpstan/extension-installer, no further configuration is needed—the extension registers automatically. Otherwise, manually include it in your PHPStan config (phpstan.neon):
includes:
- vendor/phpstan/phpstan-webmozart-assert/extension.neon
Your first use case: enforce non-nullability and type precision after assertions. For example, after Assert::integer($a), PHPStan now knows $a is definitely an int, not ?int—preventing false positives in strict comparisons like $a === 10.
nullOr* and all* prefixes for flexible validation:
Assert::allInteger($ids); // $ids is narrowed to non-empty list<int> if non-empty
Assert::nullOrString($name); // $name narrowed to string|null
isList, isNonEmptyList, isMap, and all* methods to refine array shapes:
Assert::isNonEmptyList($items); // $items is non-empty list<any>
Assert::allStringNotEmpty($strings); // $strings is array<string>
isInstanceOf, classExists, interfaceExists:
Assert::isInstanceOf($obj, SomeInterface::class); // $obj is typed as SomeInterface
Assert::interfaceExists($interface); // Later `new $interface()` becomes safe if checked first
Assert::minLength($s, 1); // $s becomes non-empty-string
Assert::email($email); // $s refined to email-string
phpstan/phpstan and phpstan/phpstan-webmozart-assert versions are compatible (e.g., PHPStan 2.x requires extension ≥2.0). Check the changelog for breaking changes on major version bumps.Assert::numeric() doesn’t narrow to numeric-string, but to int|float|string. Check the actual supported type specs in the README for precision.keyExists() and keyNotExists() narrow associative array access in later code—use them before array index access to silence offset errors.files: or paths: to keep CI fast.interfaceExists, methodExists, propertyExists in recent releases). Watch releases for emerging coverage gaps you may need to validate manually—or contribute back!How can I help you explore Laravel packages today?