Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Phpstan Beberlei Assert Laravel Package

phpstan/phpstan-beberlei-assert

PHPStan extension for beberlei/assert. Adds improved static analysis and type inference for Assert::that and related assertions, helping PHPStan understand refined types after checks and reducing false positives in codebases using beberlei/assert.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install the package via Composer: composer require --dev phpstan/phpstan-beberlei-assert
  2. Enable the extension in your phpstan.neon config:
    extends:
      - vendor/phpstan/phpstan-beberlei-assert/extension.neon
    
  3. Start using beberlei/assert in your code: the extension automatically picks up Assert::*() calls (e.g., Assert::string($input)) and informs PHPStan of the narrowed type.

👉 First use case: Wrap potentially mixed/array|object inputs with assertions (e.g., Assert::keyExists($data, 'user_id')) — PHPStan will then treat $data['user_id'] as non-nullable and strongly typed after the assertion.

Implementation Patterns

  • Type narrowing after assertions: Use Assert::notNull(), Assert::integer(), Assert::isOfAnyType([...]) to guide PHPStan through union-type refinement.
    Assert::integer($value); // PHPStan now treats $value as int below
    
  • Array key assertions: Assert::keyExists() and Assert::keyIsset() allow PHPStan to eliminate ?T or nullability in accessing array keys.
    Assert::keyExists($config, 'db');
    $dbConfig = $config['db']; // No null check needed
    
  • Combining with @param/@return: Pair assertion contracts with PHPDoc for clarity — the extension reinforces assertions, making PHPDoc a source of truth for runtime behavior.
  • Integration with Laravel requests: Use Assert::all() or Assert::keys() on request()->all() after validation to narrow validated shapes for downstream logic.

Gotchas and Tips

  • ⚠️ Extension only supports beberlei/assert syntax — does not work with webmozart/assert or ralouphie/multibyte. Don’t expect Assert::startsWith(...) from other libraries to be recognized.
  • ⚠️ Assert::true($x) only narrows to bool true, not bool — use Assert::boolean() if you want to accept both true/false.
  • 🔧 Custom assertion classes: If you extend Assert, add @phpstan-method annotations or use parameterNameCase workarounds — PHPStan may not resolve narrowed types for subclasses.
  • 🐞 Debug tip: Run phpstan --debug and look for Type-specifying extensions to verify the extension is active. If narrowing isn’t happening, ensure beberlei/assert is installed (composer require beberlei/assert).
  • 📦 Optional strictness: This extension enhances PHPStan — it doesn’t replace validating/asserting in production. Keep runtime assertions (or use assert() with zend.assertions=1) for production safety.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc
animalyoung/laravel-influxdb
onix-solutions/sms-telera
lonely-lights/prosetta
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log