PHPStan extension to find unused PHP code in your project with ease!
composer require --dev shipmonk/dead-code-detector
Use official extension-installer or just load the rules:
includes:
- vendor/shipmonk/dead-code-detector/rules.neon
vendor
is not reported as deadphpstan/phpstan-symfony
with containerXmlPath
must be used#[AsEventListener]
attribute#[AsController]
attribute#[AsCommand]
attribute#[Required]
attribute#[Route]
attributesonKernelResponse
, onKernelRequest
, etc#[AsEntityListener]
attributeDoctrine\ORM\Events::*
eventsDoctrine\Common\EventSubscriber
methods#[PreFlush]
, #[PostLoad]
, ...testXxx
methods@test
, @before
, @afterClass
etc#[Test]
, #[Before]
, #[AfterClass]
etchandleXxx
, renderXxx
, actionXxx
, injectXxx
, createComponentXxx
SmartObject
magic calls for @property
annotationsAll those libraries are autoenabled when found within your composer dependencies. If you want to force enable/disable some of them, you can:
# phpstan.neon.dist
parameters:
shipmonkDeadCode:
entrypoints:
phpunit:
enabled: true
shipmonk.deadCode.entrypointProvider
and implement ShipMonk\PHPStan\DeadCode\Provider\MethodEntrypointProvider
ShipMonk\PHPStan\DeadCode\Provider\SimpleMethodEntrypointProvider
# phpstan.neon.dist
services:
-
class: App\ApiOutputEntrypointProvider
tags:
- shipmonk.deadCode.entrypointProvider
use ReflectionMethod;
use ShipMonk\PHPStan\DeadCode\Provider\SimpleMethodEntrypointProvider;
class ApiOutputEntrypointProvider extends SimpleMethodEntrypointProvider
{
public function isEntrypointMethod(ReflectionMethod $method): bool
{
return $method->getDeclaringClass()->implementsInterface(ApiOutput::class));
}
}
------ ------------------------------------------------------------------------
Line src/App/Facade/UserFacade.php
------ ------------------------------------------------------------------------
26 Unused App\Facade\UserFacade::updateUserAddress
๐ชช shipmonk.deadMethod
๐ก Thus App\Entity\User::updateAddress is transitively also unused
๐ก Thus App\Entity\Address::setPostalCode is transitively also unused
๐ก Thus App\Entity\Address::setCountry is transitively also unused
๐ก Thus App\Entity\Address::setStreet is transitively also unused
๐ก Thus App\Entity\Address::setZip is transitively also unused
------ ------------------------------------------------------------------------
phpstan.neon.dist
:parameters:
shipmonkDeadCode:
reportTransitivelyDeadMethodAsSeparateError: true
removeDeadCode
error format:vendor/bin/phpstan analyse --error-format removeDeadCode
class UserFacade
{
- public function deadMethod(): void
- {
- }
}
$unknown->method()
) by marking all methods named method
as used
new $unknown()
will mark all constructors as usedphpstan.neon.dist
:parameters:
shipmonkDeadCode:
trackCallsOnMixed: false
-vvv
and you will see some diagnostics:Found 2 methods called over unknown type:
โข setCountry, for example in App\Entity\User::updateAddress
โข setStreet, for example in App\Entity\User::updateAddress
__get
, __set
etc) are never reported as dead
__construct
, __clone
parameters:
ignoreErrors:
- '#^Unused .*?::__construct$#'
composer check
composer fix:cs