- How do I install phpstan/phpstan-beberlei-assert for Laravel projects?
- Run `composer require --dev phpstan/phpstan-beberlei-assert` in your project directory. Then, add `extensions: - phpstan/phpstan-beberlei-assert` to your `phpstan.neon` configuration file under the `[phpstan]` section. No additional setup is required.
- Which Laravel versions does this PHPStan extension support?
- This extension works with any Laravel version that supports PHPStan (typically PHP 7.4+). It focuses on static analysis, so Laravel-specific features don’t affect compatibility. Ensure your project’s PHPStan version is up-to-date for best results.
- Will this extension break existing PHPStan rules or extensions?
- No, it integrates seamlessly with existing PHPStan rules and other extensions. The extension only enhances type inference for `beberlei/assert` calls, so your current static analysis setup remains unchanged.
- How does type narrowing work with Assert::nonEmpty() or Assert::numeric()?
- After calling `Assert::nonEmpty($var)`, PHPStan will recognize `$var` as non-empty and adjust its type accordingly. For `Assert::numeric()`, it narrows the type to `int|float`. This reduces false positives by aligning analysis with the actual behavior of `beberlei/assert`.
- Can I use this extension in production? Does it affect runtime performance?
- This is a static analysis tool—it only runs during development (via PHPStan) and has zero runtime impact. Install it in `dev` dependencies only, as it’s purely for improving code quality and catching issues early.
- What if beberlei/assert updates its API? Will this extension break?
- The extension targets well-defined patterns in `beberlei/assert` (e.g., `Assert::*()` methods). While major API changes could require updates, the extension’s focus on common use cases minimizes risk. Monitor the package for updates if you rely on cutting-edge features.
- Does this work with Laravel’s built-in validation or only beberlei/assert?
- This extension is specifically for `beberlei/assert`. Laravel’s validation (e.g., `Validator` facade) uses a different pattern and isn’t supported. If you need static analysis for Laravel validation, consider other tools like `phpstan/phpstan-doctrine` or custom rules.
- How do I test if the extension is working correctly?
- Run `phpstan analyse` and check for reduced false positives in code using `beberlei/assert`. For example, after `Assert::nonEmpty($string)`, PHPStan should no longer flag `$string` as potentially null. Compare results before/after installation.
- Are there alternatives to this extension for type-safe assertions?
- For Laravel, alternatives include custom PHPStan rules or libraries like `spatie/laravel-assert`. However, this extension is the most direct solution for `beberlei/assert`, offering built-in support without extra configuration.
- Can I use this with PHPStan’s level 5 (most strict) without issues?
- Yes, this extension is designed to work with strict PHPStan levels. It reduces false positives by teaching PHPStan about `beberlei/assert`’s guarantees, making level 5 analysis more practical for projects using runtime assertions.