thecodingmachine/phpstan-strict-rules
Extra-strict PHPStan ruleset that flags risky or inconsistent PHP patterns beyond the default checks. Helps enforce cleaner, safer code by catching edge cases and enforcing best practices, with easy opt-in configuration for existing PHPStan setups.
Architecture Fit: The package extends PHPStan’s static analysis capabilities, aligning perfectly with Laravel’s PHP-centric stack. It enforces best practices (e.g., exception handling, superglobal avoidance) without runtime overhead, making it ideal for CI/CD-driven quality gates. The rules are framework-agnostic but particularly relevant for Laravel’s dependency injection and PSR-7 request patterns.
Integration Feasibility:
Low technical debt for adoption—requires only Composer installation and phpstan.neon configuration. However, the 2021 release date and PHPStan 1.0+ compatibility (vs. Laravel’s typical PHPStan 0.x/1.x usage) introduce risks. The package’s focus on superglobals (e.g., $_GET, $_POST) directly conflicts with Laravel’s legacy Request facade usage, necessitating opt-outs or refactoring.
Technical Risk:
better-reflection).default in switch) may conflict with Laravel’s convention-heavy patterns (e.g., case in Illuminate\Support\Enum).Key Questions:
phpstan/extension-installer.Request facade (e.g., $request->input() vs. $_GET)?phpstan/extension-installer + custom rules for more control.Stack Fit:
Request facade, dynamic properties).phpstan --level=5) or GitHub Actions step with incremental enforcement.--generate-baseline to exclude violations during transition.Migration Path:
composer require --dev thecodingmachine/phpstan-strict-rules.phpstan.neon to include only non-breaking rules (e.g., exception handling, switch defaults)../vendor/bin/phpstan analyse) to identify critical violations.@phpstan-ignore-next-line for temporary opt-outs (e.g., legacy controllers).roave/security-advisories to monitor dependency risks.phpstan/parallel-lint for faster analysis on large codebases.Compatibility:
^1.0 to avoid breaking changes (e.g., rule IDs).Illuminate\Http\Request methods in phpstan.neon:
parameters:
checkGenericClassInNonGenericContext: false
checkPropertyTypeInNonObjectClass: false
@var annotations for dynamic properties (e.g., /** @var array<string, mixed> */).Sequencing:
| Priority | Rule Category | Action |
|---|---|---|
| 1 | Exception Handling | Enforce catch rethrows, no empty catch. |
| 2 | switch Default Cases |
Add default: throw for enums/strings. |
| 3 | Superglobals (Opt-Out) | Replace $_GET with Request facade. |
| 4 | Type Hints (Legacy Code) | Use @phpstan-param for backward compatibility. |
Maintenance:
Illuminate\Database\Eloquent\Model dynamic properties).better-reflection for CVEs (e.g., CVE-2023-XXXX).Collection methods).Support:
Exception?").@phpstan-ignore-line).spatie/phpstan-laravel).Scaling:
phpstan --memory-limit=1G for large projects.phpstan/parallel-lint).Failure Modes:
| Risk | Mitigation Strategy |
|---|---|
| CI Blockages | Use --level=5 initially; escalate fixes. |
| False Positives | Whitelist Laravel classes in neon. |
| Rule Incompatibility | Test with phpstan --test before enforcement. |
| Dependency Vulnerabilities | Pin better-reflection to ^2.0 in composer.json. |
Ramp-Up:
Request facade workarounds).CONTRIBUTING.md with PHPStan rule examples.phpstan-rules.md to the repo explaining opt-outs.phpstan-strict-rules.phpstan.neon with Laravel exceptions.phpstan analyse --level=5 locally.How can I help you explore Laravel packages today?