phpstan/phpstan-strict-rules
Opinionated extra rules for PHPStan to enforce strict, strongly typed PHP. Catches loose booleans in conditions, unsafe strict parameters, useless casts, non-numeric arithmetic, variable overwrites in loops, and switch/case type mismatches for safer defensive code.
empty(), or backticks) but allows granular disablement. This makes it ideal for teams with high coding standards but may require negotiation for teams with legacy codebases.phpstan/phpstan-strict-rules) and works with phpstan/extension-installer for zero-config setups. Manual inclusion via rules.neon is also straightforward.disallowedBacktick) may trigger false positives in Blade contexts (e.g., shell commands in {{ !! }} or @php). Mitigation: Exclude Blade directories from PHPStan analysis or disable conflicting rules.empty() checks). The package’s strict rules may break builds initially. Mitigation: Incremental adoption (disable rules per-file or per-class) or pair with PHPStan’s baseline to track violations over time.| Risk Area | Description | Mitigation Strategy |
|---|---|---|
| False Positives | Rules like disallowedLooseComparison or switchConditionsMatchingType may flag legitimate PHP idioms (e.g., if ($var == null) or switch ($type) with mixed types). |
Test rules against a representative code sample before full adoption. Disable selectively. |
| Build Breaks | Strict rules (e.g., requireParentConstructorCall) may fail in legacy Laravel components (e.g., older service providers, repositories). |
Use // @phpstan-ignore-line for known issues or refactor incrementally. |
| Toolchain Conflicts | Conflicts with other PHPStan extensions (e.g., phpstan/phpstan-doctrine) or custom rulesets. |
Audit existing PHPStan config for overlaps; test integration early. |
| Maintenance Burden | Strict rules may prohibit patterns that are hard to replace (e.g., empty() in validation logic). |
Document exceptions; phase out violations over time. |
allRules and enable rules by category)?disallowedShortTernary may require manual fixes).noVariableVariables) may affect test readability. Mitigation: Exclude test directories or document exceptions.phpstan/phpstan-strict-rules in dev dependencies.strictRules.allRules: false) to establish a baseline.disallowedLooseComparison in core logic vs. disallowedBacktick in Blade).uselessCast, closureUsesThis).// @phpstan-ignore-next-line).--generate-baseline).disallowedLooseComparison, requireParentConstructorCall).empty() with !isset() + !empty()).| Component | Compatibility Notes |
|---|---|
| Laravel Core | Most rules are compatible, but legacy service containers (e.g., dynamic method calls) may need updates. |
| Blade Templates | No analysis by default. Exclude from PHPStan or disable rules like disallowedBacktick. |
| Third-Party Packages | Packages using loose comparisons (e.g., if ($var == null)) may fail. Mitigation: Patch or exclude vendor code. |
| PHP 8.1+ Features | Rules like matchingInheritedMethodNames align with PHP 8.1’s stricter type system. |
| Custom Rulesets | Conflicts possible with existing PHPStan configs. Mitigation: Merge configs carefully or override defaults. |
phpstan/extension-installer).uselessCast, numericOperandsInArithmeticOperators (quick wins).disallowedEmpty, disallowedShortTernary (requires logic changes).disallowedLooseComparison, requireParentConstructorCall (may break legacy code).@phpstan-ignore-next-line usage).^2.0) and test upgrades.level system or a centralized config for exceptions.How can I help you explore Laravel packages today?