Installation Add the package via Composer in your Laravel project:
composer require --dev wyrihaximus/phpstan-no-safe
Configure PHPStan
Ensure your phpstan.neon (or phpstan.neon.dist) includes the extension:
includes:
- vendor/wyrihaximus/phpstan-no-safe/extension.neon
First Use Case
Run PHPStan with the extension to detect thecodingmachine/safe usage:
vendor/bin/phpstan analyse app
The tool will flag any classes or methods from thecodingmachine/safe (e.g., Safe\*, Safe::*), enforcing a strict "no-safe" policy.
Enforcing Zero-Tolerance for Safe
Use this package to block thecodingmachine/safe entirely, aligning with projects where defensive programming via Safe is discouraged (e.g., strict type safety, performance-critical code, or teams preferring manual null checks).
Integration with CI/CD
Add PHPStan with this extension to your CI pipeline to fail builds on Safe usage:
# Example GitHub Actions step
- name: PHPStan (No-Safe)
run: vendor/bin/phpstan analyse --level=max app
Customizing Exceptions
Override the extension’s rules in phpstan.neon to allow specific Safe usages (if needed):
parameters:
wyrihaximus/phpstan-no-safe:
allowedClasses: ['Safe\Exceptions\SafeException']
allowedFunctions: ['Safe\someFunction']
Pairing with Other Extensions
Combine with phpstan/extension-installer for seamless dependency management:
extends: phpstan/extension-installer.neon
config/app.php or AppServiceProvider for Safe registrations:
// Flagged: Avoid this
$this->app->bind('Safe\Foo', function () { ... });
Safe wrappers around database operations (e.g., Safe\Db::select()).False Positives
Safe internally (e.g., Safe\Arrays, Safe\Strings).phpstan.neon under allowedClasses or exclude vendor directories:
excludeFiles: ['vendor/*/Safe/*']
Dynamic Class Loading
Safe classes are loaded dynamically (e.g., via class_alias or eval), PHPStan may miss them.--generate-report=html to debug missed cases.IDE Integration
Safe violations until PHPStan is fully configured.phpstan.neon as your CLI.--debug to trace rule application:
vendor/bin/phpstan analyse --debug app
vendor/bin/phpstan analyse app/Http/Controllers/SomeController.php
Custom Rules Extend the extension by creating a custom PHPStan rule to handle edge cases:
// Example: Allow Safe in tests only
class AllowSafeInTestsRule extends Rule
{
public function getNodeType(): string { return 'Php\Node\Stmt\Namespace_'; }
public function process(Node $node): array { ... }
}
Configuration Inheritance
Use extends in phpstan.neon to inherit rules from other configs:
extends: ['vendor/wyrihaximus/phpstan-no-safe/extension.neon', 'custom-rules.neon']
Performance
vendor/bin/phpstan analyse --memory-limit=1G --cache-result
How can I help you explore Laravel packages today?