thecodingmachine/phpstan-safe-rule
PHPStan rule set that flags calls to “unsafe” PHP functions that can return false on failure and suggests using the thecodingmachine/safe equivalents that throw exceptions, helping enforce safer, exception-based error handling in your codebase.
Architecture fit
thecodingmachine/safe may limit flexibility if alternative null-safety strategies (e.g., custom wrappers) are preferred.Illuminate\Support\Collection methods). Test for false positives in Laravel-specific code (e.g., Eloquent queries, Blade templates).Integration feasibility
phpstan.neon configuration (1–2 lines). No runtime dependencies or Laravel service provider required.thecodingmachine/safe (v1.2+ recommended). If not already used, requires parallel adoption.str_* functions in helpers). Use ignoreErrors to exclude vendor/Laravel code.@php $var ?? 'default').Technical risk
preg_match type inference or JSON_THROW_ON_ERROR may misclassify Laravel-specific use cases (e.g., JSON API responses).level: 1 (weak) and whitelist known false positives in phpstan.neon.thecodingmachine/safe is actively maintained, but phpstan-safe-rule has low stars (62) and no Laravel-specific tests. Validate against Laravel’s codebase before full adoption.Key questions
thecodingmachine/safe, or will this require parallel adoption?Str::* helpers, Eloquent)?@phpstan-ignore-line annotations?phpstan-safe-rule stagnates (last release: 2026-06-23)?try-catch)?Stack fit
return_type declarations (e.g., Collection::pluck()).@phpstan-ignore-line for dynamic code.phpstan analyse (exit code 1 on errors).Migration path
composer require --dev thecodingmachine/phpstan-safe-rule thecodingmachine/safe
phpstan.neon minimally:
includes:
- vendor/thecodingmachine/phpstan-safe-rule/extension.neon
level: 1 # Start with weak checks
ignoreErrors:
- '#.*vendor/laravel.*#' # Exclude Laravel core
app/Http/Controllers):
phpstan analyse --memory-limit=1G app/Http
paths:
- app/Http/NewFeature
strlen(null)), suppress others with @phpstan-ignore-line.level to 5 (max).Compatibility
composer.json).file_get_contents).thecodingmachine/safe v1.2+ (required for rule suggestions).Sequencing
thecodingmachine/safe incrementally (e.g., replace file_get_contents with Safe\file_get_contents).@phpstan-ignore-next-line for known edge cases).Maintenance
phpstan-safe-rule annually (check changelogs for breaking changes).phpstan.neon as they arise (e.g., for Laravel’s Str::* helpers).ignoreErrors and rule exclusions.PHPSTAN_RULES.md file.Support
phpstan debug to isolate rule-specific issues.thecodingmachine/safe docs for edge cases (e.g., Safe\json_decode with JSON_THROW_ON_ERROR).Unsafe function call: strlen()).@phpstan-ignore-line judiciously.array_key_exists with Safe\array_key_exists).Scaling
phpstan analyse --parallel).--generate-baseline).actions/cache).Failure modes
| Scenario | Impact | Mitigation |
|---|---|---|
| False positives in CI | PRs blocked unnecessarily. | Pre-populate ignoreErrors with known patterns. |
| PHPStan version conflict | Rule breaks with new PHPStan. | Pin versions in composer.json. |
| Laravel core flagged | False errors in vendor code. | Exclude vendor/ in phpstan.neon. |
| Rule stagnation | Package no longer maintained. | Fork or replace with custom PHPStan rules. |
| Overly strict rules | Developers bypass static analysis. | Start with level: 1; gradually tighten. |
Ramp-up
How can I help you explore Laravel packages today?