symplify/phpstan-rules
Extra PHPStan rules by Symplify to catch bugs, improve code quality, and enforce consistent conventions. Easy to install and configure, with a broad set of checks for Symfony/Laravel and modern PHP features to keep your codebase clean.
Pros:
NoEntityOutsideEntityNamespaceRule, ForbiddenNewArgumentRule) directly address Laravel’s common anti-patterns (e.g., manual service instantiation, misplaced Doctrine entities).ClassNameRespectsParentSuffixRule, ExplicitClassPrefixSuffixRule, and CheckRequiredInterfaceInContractNamespaceRule enforce Laravel’s PSR-4 and Domain-Driven Design patterns, reducing technical debt.phpstan.neon configurations.ForbiddenFuncCallRule or PreferredClassRule allow granular control, aligning with Laravel’s principle of convention over configuration while permitting exceptions.Cons:
SingleRequiredMethodRule) may not directly apply to Laravel’s dependency injection (Laravel’s container vs. Symfony’s autowiring). Requires customization or exclusion.NoGlobalConstRule) may conflict with Laravel’s existing conventions (e.g., config('app.name')). Requires careful tuning.composer require with no breaking changes to Laravel’s core.phpstan/extension-installer (commonly used via phpstan:install Artisan command) supports this package out-of-the-box.phpstan.neon (e.g., including rule sets) with no impact on Laravel’s routing, service container, or Blade templates.NoTestMocksRule) may clash with Laravel’s testing conventions (e.g., Mockery or PHPUnit mocks). Requires explicit exclusion in phpstan.neon.Illuminate\Contracts or Laravel\Nova) would need to be added via custom PHPStan extensions.array<int, string>) align well with PHPStan’s expectations.naming-rules.neon, doctrine-rules.neon) to avoid overwhelming developers.--error-format=github for actionable feedback.ForbiddenNewArgumentRule be configured to exclude Laravel’s AppServiceProvider or Bootstrap classes?Illuminate\Foundation\Application) that should be contributed upstream?phpstan.neon (e.g., level: 5 vs. custom rule levels)?level: 3) or in PR checks (e.g., level: 5)?parallel mode be enabled to offset the rule overhead?vendor/ paths) to reduce analysis time?^1.0). No conflicts with Laravel’s phpstan.neon or rector.php.Symfony\Component\HttpKernel, Symfony\Component\Routing, etc., align with Laravel’s Symfony-based foundations (e.g., Illuminate\Routing, Illuminate\Http).NoEntityOutsideEntityNamespaceRule and ForbiddenNewArgumentRule directly address Laravel Eloquent’s common pitfalls.NoTestMocksRule and NoArrayMapWithArrayCallableRule improve test quality, complementing Laravel’s testing tools.Laravel\Nova, Laravel\Fortify, or Laravel\Vapor would require custom extensions.nunomaduro/collision or [barryvdh/laravel-ide-helper] for static analysis).Phase 1: Setup and Validation
composer.json:
composer require --dev symplify/phpstan-rules ^8.0
phpstan:install command handles this automatically).phpstan.neon to include core rule sets:
includes:
- vendor/symplify/phpstan-rules/config/naming-rules.neon
- vendor/symplify/phpstan-rules/config/static-rules.neon
- vendor/symplify/phpstan-rules/config/doctrine-rules.neon
vendor/bin/phpstan analyse --level=5
Phase 2: Incremental Enforcement
--level=3 to avoid build breaks.symfony-rules.neon, complexity-rules.neon) based on team feedback.- name: PHPStan (Symplify Rules)
run: vendor/bin/phpstan analyse --level=3 --error-format=github
Phase 3: Customization
phpstan.neon:
parameters:
maximumIgnoredErrorCount: 100
services:
- Symplify\PHPStanRules\Rules\ForbiddenNewArgumentRule:
arguments:
forbiddenTypes:
- Illuminate\Foundation\Application # Exclude Laravel container
ForbiddenRouteAnnotationRule for Route::get()).Phase 4: Documentation and Training
CONTRIBUTING.md or a DEVELOPMENT.md file.phpstan-fixes.md guide with examples of how to resolve common rule violations (e.g., converting new App\Models\User to dependency injection).^0.12 (deprecated).^5.4|^6.0, which aligns with Laravel’s Symfony 5/6 components.NoArrayMapWithArrayCallableRule may suggest refactors that conflict with code style tools. Prioritize PHPStan’s suggestions.How can I help you explore Laravel packages today?