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.
Installation:
composer require symplify/phpstan-rules --dev
composer require phpstan/extension-installer --dev
Add to composer.json:
{
"extra": {
"phpstan": {
"extension-classes": ["Symplify\\PHPStanRules\\PHPStan\\Extension\\SymplifyPHPStanRulesExtension"]
}
}
}
First Configuration:
Add to phpstan.neon:
includes:
- vendor/symplify/phpstan-rules/config/naming-rules.neon
- vendor/symplify/phpstan-rules/config/static-rules.neon
First Use Case: Run PHPStan on a single file to test:
vendor/bin/phpstan analyse src/Controller/SomeController.php
Incremental Adoption:
Rule Grouping:
includes in phpstan.neon to enable entire rule sets:
includes:
- vendor/symplify/phpstan-rules/config/code-complexity-rules.neon
- vendor/symplify/phpstan-rules/config/symfony-rules.neon
Custom Rule Configuration:
ParamNameToTypeConventionRule for type hints:
services:
- class: Symplify\PHPStanRules\Rules\Convention\ParamNameToTypeConventionRule
arguments:
paramNamesToTypes:
userId: int
Symfony/Doctrine Integration:
doctrine-rules for Doctrine-specific checks (e.g., NoEntityOutsideEntityNamespaceRule).symfony-rules for Symfony best practices (e.g., SingleRequiredMethodRule).Test-Specific Rules:
NoTestMocksRule to enforce direct class usage in tests:
rules:
- Symplify\PHPStanRules\Rules\PHPUnit\NoTestMocksRule
CI/CD Pipeline:
Add PHPStan with symplify/phpstan-rules to your CI workflow to enforce consistency.
Example (GitHub Actions):
- name: PHPStan
run: vendor/bin/phpstan analyse --level=5
IDE Integration:
Configure your IDE (PHPStorm) to use the same phpstan.neon for real-time feedback.
Rector Pairing:
Use Rector alongside PHPStan to auto-fix violations (e.g., ForbiddenNewArgumentRule).
Rule Overlap:
NoConstructorOverrideRule) may conflict with legacy code. Disable them temporarily:
rules:
Symplify\PHPStanRules\Rules\Complexity\NoConstructorOverrideRule: false
Performance Impact:
naming-rules + static-rules).False Positives:
StringFileAbsolutePathExistsRule may fail in test environments. Exclude test directories:
paths:
- src
- config
ignorePaths:
- tests
Configuration Overrides:
forbiddenTypes in ForbiddenNewArgumentRule) must be explicitly defined in services; omitting them disables the rule.Doctrine/ORM Quirks:
NoEntityOutsideEntityNamespaceRule may trigger for legacy projects. Use @phpstan-ignore-next-line for exceptions:
#[phpstan-ignore-next-line]
#[ORM\Entity]
class LegacyEntity {}
Rule-Specific Errors:
Symplify\PHPStanRules\Rules\ForbiddenFuncCallRule) and refer to its documentation in the README.Extension Loading:
phpstan/extension-installer is configured in composer.json and run:
composer dump-autoload
Ignoring Specific Violations:
@phpstan-ignore-line or @phpstan-ignore-next-line for one-off cases:
#[phpstan-ignore-line]
$result = @file_get_contents($path);
Custom Rules:
Symplify\PHPStanRules\Rules\BaseRule).Dynamic Configuration:
configurable-rules.neon to enable/disable rules dynamically based on environment variables:
# phpstan.neon
parameters:
enabledRules:
- Symplify\PHPStanRules\Rules\ForbiddenFuncCallRule
Rule Prioritization:
level setting to prioritize critical rules:
level: max
rules:
Symplify\PHPStanRules\Rules\Complexity\NoConstructorOverrideRule: error
Symfony Flex Integration:
symfony-config-rules.neon to validate config files (e.g., YAML/JSON):
includes:
- vendor/symplify/phpstan-rules/config/symfony-config-rules.neon
How can I help you explore Laravel packages today?