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.
Install via Composer:
composer require --dev thecodingmachine/phpstan-safe-rule
Enable the rule in your phpstan.neon:
includes:
- vendor/thecodingmachine/phpstan-safe-rule/extension.neon
Run PHPStan as usual — the rule activates immediately and flags unsafe calls like array_key_exists() without prior checks, or strlen() on potentially null values. Start with a low error level (e.g., level: 1) and gradually tighten as you fix violations.
phpstan.neon for legacy code (ignoreErrors: [...]) while enforcing rules for new code.thecodingmachine/safe — detects missing use of safe alternatives (e.g., file_get_contents() instead of Safe\file_get_contents()).@phpstan-ignore-line for edge cases (e.g., when a null check is logically guaranteed but hard to express statically).func_get_args() in variadic contexts) may be overly cautious. Override specific rules in your config using parameters.rulesWithErrors, or whitelist via ignoreErrors.phpstan analyse --diff).phpstan-strict-rules or similar — this package is complementary, not redundant. Prefer enabling only safe-specific rules here.SafeRule classes (public API) and registering in your phpstan.neon.extension.neon is included after neon/config.neon — loading order matters for rule priority.How can I help you explore Laravel packages today?