- How do I install timeweb/phpstan-enum for Laravel 9+ projects?
- Run `composer require --dev timeweb/phpstan-enum` and add `includes: - vendor/timeweb/phpstan-enum/extension.neon` to your `phpstan.neon` config. For easier management, use `phpstan/extension-installer` to auto-load extensions.
- Does this package work with Laravel’s Eloquent models using enums?
- Yes, it validates enum usage in Eloquent models (e.g., `StatusEnum::ACTIVE` in migrations or model attributes) by catching invalid assignments or comparisons during static analysis.
- Will this break existing code if I add it to my Laravel project?
- Unlikely, but run PHPStan without the extension first to establish a baseline. The extension primarily catches invalid enum operations, so it may reveal pre-existing issues like deprecated enum cases or incorrect comparisons.
- Can I use this with PHP 8.1 enums (not MyCLabs/Enum) in Laravel?
- Yes, this extension supports PHP 8.1+ native enums. It provides type inference and rules for both backed and unit enums, making it ideal for Laravel 9+ projects using modern PHP enums.
- How do I configure PHPStan to avoid false positives with this extension?
- Start with PHPStan’s level 7 (default) to balance strictness. For dynamic enum access (e.g., `enum::{$key}`), suppress warnings with `@phpstan-ignore-next-line` or adjust your code to use static cases.
- Does this replace Laravel’s built-in validation rules for enums?
- No, this is a static analysis tool for CI/CD, not runtime validation. Use Laravel’s `Validation::enum()` or `Rule::in()` alongside this extension for runtime safety.
- What Laravel versions are compatible with timeweb/phpstan-enum?
- Laravel 9+ (PHP 8.1+) is fully supported. Laravel 8 can work if upgraded to PHP 8.1, but test thoroughly as some features may not be backward-compatible.
- Can I use this extension with PestPHP for testing enums?
- Yes, integrate it into your CI pipeline or PestPHP pre-commit hooks. It won’t interfere with tests but will catch enum-related issues early, reducing flaky test failures.
- Are there alternatives to this package for enum validation in Laravel?
- For runtime validation, use Laravel’s `Validation::enum()` or `Rule::in()`. For static analysis, alternatives include custom PHPStan rules or `vimeo/psalm-plugin-enums`, but this package is tailored specifically for PHPStan.
- How do I handle custom enums with methods or inheritance in PHPStan?
- This extension supports basic inheritance and methods, but complex cases may require manual PHPStan rules. Audit your enums pre-integration and extend the extension’s config if needed.