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.
Install the package in your Laravel project:
composer require --dev thecodingmachine/phpstan-strict-rules
Enable the rules in your phpstan.neon (located in phpstan.neon or phpstan.neon.dist):
includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
(If using phpstan/extension-installer, no manual inclusion is needed.)
First use case: Run PHPStan on a single file to see immediate feedback:
vendor/bin/phpstan analyse app/Http/Controllers/ExampleController.php
Focus on exception handling and superglobal usage first, as these are the most critical rules.
CI/CD Pipeline Add PHPStan to your Laravel CI (e.g., GitHub Actions):
- name: Run PHPStan
run: vendor/bin/phpstan analyse --level=5
Start with --level=5 (default) and gradually increase strictness.
Team Onboarding
$_GET directly").@codingmachine-ignore annotations for edge cases (e.g., legacy code):
/** @codingmachine-ignore Exception.CatchThrowableNotRethrown */
try { ... } catch (Throwable $e) { ... }
Laravel-Specific Patterns
$_POST['field'] with Laravel’s request()->input('field').throw_if or throw_unauthorized helpers to auto-wrap exceptions:
throw new \RuntimeException('Failed', 0, $previousException);
Gradual Adoption
paths in phpstan.neon).catch blocks) in CI.default in switch statements for all files.Superglobals in index.php
index.php), but not in classes/controllers.app() helper.Empty catch Blocks
catch blocks unless annotated:
try { ... } catch (\Exception $e) {
// @codingmachine-ignore Exception.EmptyCatchBlock
}
@codingmachine-ignore sparingly—log or rethrow exceptions instead.PHPStan Version Mismatch
^0.12 in composer.json or fork the package.False Positives in Legacy Code
phpstan.neon:
paths:
- app/Http
- !app/OldCode
--error-format=github for actionable PR comments:
vendor/bin/phpstan analyse --error-format=github
@phpstan-ignore-next-line:
// @phpstan-ignore-next-line
throw new \Exception(); // Allows base Exception (violates thecodingmachine rule)
Customize Rules
Override the included ruleset in phpstan.neon:
includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
parameters:
rules:
Exception.EmptyCatchBlock: false # Disable if needed
Add New Rules Extend the package by creating a custom PHPStan extension (see PHPStan docs).
Performance
paths:
- app
- !tests
How can I help you explore Laravel packages today?