PHPStan rules to find dead code in your project.
composer require --dev shipmonk/dead-code-detector
Use official extension-installer or just load the rules:
includes:
- vendor/shipmonk/dead-code-detector/rules.neon
- All entrypoints of your code (controllers, consumers, commands, ...) need to be known to the detector to get proper results
- By default, all overridden methods which declaration originates inside vendor are considered entrypoints
- Also, there are some basic entrypoint providers for
symfony
andphpunit
- For everything else, you can implement your own entrypoint provider, just tag it with
shipmonk.deadCode.entrypointProvider
parameters:
deadCode:
entrypoints:
vendor:
enabled: true # enabled by default
symfony:
enabled: true
phpunit:
enabled: true
services:
-
class: App\MyEntrypointProvider
tags:
- shipmonk.deadCode.entrypointProvider
use ReflectionMethod;
use ShipMonk\PHPStan\DeadCode\Provider\EntrypointProvider;
class MyEntrypointProvider implements EntrypointProvider
{
public function isEntrypoint(ReflectionMethod $method): bool
{
return $method->getDeclaringClass()->implementsInterface(ApiOutput::class));
}
}
- Only method calls are detected
- Including static methods, trait methods, interface methods, first class callables, etc.
- Callbacks like
[$this, 'method']
are mostly not detected; prefer first class callables$this->method(...)
- Any calls on mixed types are not detected, e.g.
$unknownClass->method()
- Expression method calls are not detected, e.g.
$this->$methodName()
- Anonymous classes are ignored
- Does not check constructor calls
- Does not check magic methods
- No transitive check is performed (dead method called only from dead method)
- No dead cycles are detected (e.g. dead method calling itself)
- Check your code by
composer check
- Autofix coding-style by
composer fix:cs
- All functionality must be tested
- PHP 7.4 - 8.3