- How do I install phpstan/phpstan-strict-rules in a Laravel project?
- Run `composer require --dev phpstan/phpstan-strict-rules` and add it to your PHPStan config (`rules.neon`) under `[rules]`. For Laravel, ensure PHPStan is already integrated via `phpstan/extension-installer` or manually in `phpstan.neon`.
- Which Laravel versions are compatible with this package?
- This package works with Laravel 9+ (PHP 8.1+) and Laravel 10 (PHP 8.2+). It’s a static analysis tool, so it doesn’t modify runtime behavior—just enforce stricter coding standards during development.
- Can I enable only specific rules instead of all strict rules?
- Yes. In your `rules.neon`, disable `allRules: true` and enable individual rules like `booleansInConditions: true` or `strictFunctionCalls: true`. This lets you adopt strictness incrementally.
- Will this break my existing Laravel codebase?
- It may flag violations in loosely typed code (e.g., `if ($var)` when `$var` isn’t boolean). Start with a baseline run (`phpstan analyse --level=max`) and enable rules gradually to avoid massive refactoring.
- How do I suppress false positives, like for legacy array functions?
- Use `@suppress` PHPDoc comments or configure suppressions in `phpstan.neon` under `[suppressionFiles]`. For example, suppress `strictFunctionCalls` for `in_array` calls where strict mode isn’t needed.
- Does this package slow down Laravel’s CI/CD pipeline?
- Minimally. Static analysis adds <1s to builds for 10k LOC. Cache results with `phpstan --generate-baseline` or run in parallel. For large projects, prioritize critical paths first.
- How does this compare to PHPStan’s default rules?
- This package adds opinionated rules (e.g., `numericOperandsInArithmeticOperators`, `switchConditionsMatchingType`) that PHPStan’s default level (`--level=max`) doesn’t enforce. It’s for teams prioritizing defensive programming.
- Can I use this with Laravel’s built-in PHPStan integration?
- Yes. Laravel’s `phpstan` facade or `phpstan.neon` config works seamlessly. Just ensure your `composer.json` includes PHPStan as a dev dependency and extend its config with the strict rules.
- What’s the best way to test this in a Laravel project?
- Start with `phpstan analyse --level=max` to catch existing issues, then enable strict rules incrementally. Use `phpstan --error-format=github` for CI-friendly output and fail builds on violations.
- Are there alternatives for stricter PHPStan rules in Laravel?
- Other PHPStan extensions like `phpstan/extension-installer` or `phpstan/phpstan-doctrine` focus on framework-specific rules. However, `phpstan-strict-rules` is unique for its opinionated, defensive coding focus (e.g., boolean conditions, type safety).