Download the PHP package shipmonk/dead-code-detector without Composer
On this page you can find all versions of the php package shipmonk/dead-code-detector. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Table of contents
Download shipmonk/dead-code-detector
More information about shipmonk/dead-code-detector
Files in shipmonk/dead-code-detector
Download shipmonk/dead-code-detector
More information about shipmonk/dead-code-detector
Files in shipmonk/dead-code-detector
Vendor shipmonk
Package dead-code-detector
Short Description Dead code detector to find unused PHP code via PHPStan extension.
License MIT
Package dead-code-detector
Short Description Dead code detector to find unused PHP code via PHPStan extension.
License MIT
Please rate this library. Is it a good library?
Informations about the package dead-code-detector
Dead code detector for PHP
PHPStan extension to find unused PHP code in your project with ease!
Summary:
- โ PHPStan extension
- โป๏ธ Dead cycles detection
- ๐ Transitive dead method detection
- ๐งน Automatic removal of unused code
- ๐ Popular libraries support
- โจ Customizable entrypoints
Installation:
Use official extension-installer or just load the rules:
Supported libraries
- Any overridden method that originates in
vendor
is not reported as dead - We also support many magic calls in following libraries:
Symfony:
- constructor calls for DIC services!
phpstan/phpstan-symfony
withcontainerXmlPath
must be used
#[AsEventListener]
attribute#[AsController]
attribute#[AsCommand]
attribute#[Required]
attribute#[Route]
attributesonKernelResponse
,onKernelRequest
, etc
Doctrine:
#[AsEntityListener]
attributeDoctrine\ORM\Events::*
eventsDoctrine\Common\EventSubscriber
methods- lifecycle event attributes
#[PreFlush]
,#[PostLoad]
, ...
PHPUnit:
- data provider methods
testXxx
methods- annotations like
@test
,@before
,@afterClass
etc - attributes like
#[Test]
,#[Before]
,#[AfterClass]
etc
PHPStan:
- constructor calls for DIC services (rules, extensions, ...)
Nette:
handleXxx
,renderXxx
,actionXxx
,injectXxx
,createComponentXxx
SmartObject
magic calls for@property
annotations
All those libraries are autoenabled when found within your composer dependencies. If you want to force enable/disable some of them, you can:
Customization:
- If your application does some magic calls unknown to this library, you can implement your own entrypoint provider.
- Just tag it with
shipmonk.deadCode.entrypointProvider
and implementShipMonk\PHPStan\DeadCode\Provider\MethodEntrypointProvider
- You can simplify your implementation by extending
ShipMonk\PHPStan\DeadCode\Provider\SimpleMethodEntrypointProvider
Dead cycles & transitively dead methods
- This library automatically detects dead cycles and transitively dead methods (methods that are only called from dead methods)
-
By default, it reports only the first dead method in the subtree and the rest as a tip:
- If you want to report all dead methods individually, you can enable it in your
phpstan.neon.dist
:
Automatic removal of dead code
- If you are sure that the reported methods are dead, you can automatically remove them by running PHPStan with
removeDeadCode
error format:
Calls over unknown types
-
In order to prevent false positives, we support even calls over unknown types (e.g.
$unknown->method()
) by marking all methods namedmethod
as used- Such behaviour might not be desired for strictly typed codebases, because e.g. single
new $unknown()
will mark all constructors as used - Thus, you can disable this feature in your
phpstan.neon.dist
:
- Such behaviour might not be desired for strictly typed codebases, because e.g. single
- If you want to check how many of those cases are present in your codebase, you can run PHPStan analysis with
-vvv
and you will see some diagnostics:
Comparison with tomasvotruba/unused-public
- You can see detailed comparison PR
- Basically, their analysis is less precise and less flexible. Mainly:
- It cannot detect dead constructors
- It does not properly detect calls within inheritance hierarchy
- It does not offer any custom adjustments of used methods
- It has almost no built-it library extensions
- It ignores trait methods
- Is lacks many minor features like class-string calls, dynamic method calls, array callbacks, nullsafe call chains etc
- It cannot detect dead cycles nor transitively dead methods
- It has no built-in dead code removal
Limitations:
- Methods of anonymous classes are never reported as dead (PHPStan limitation)
- Abstract trait methods are never reported as dead
- Most magic methods (e.g.
__get
,__set
etc) are never reported as dead- Only supported are:
__construct
,__clone
- Only supported are:
Other problematic cases:
Constructors:
- For symfony apps & PHPStan extensions, we simplify the detection by assuming all DIC classes have used constructor.
- For other apps, you may get false-positives if services are created magically.
- To avoid those, you can easily disable consructor analysis with single ignore:
Private constructors:
- Those are never reported as dead as those are often used to deny class instantiation
Future scope:
- Dead class constant detection
- Dead class property detection
- Dead class detection
Contributing
- Check your code by
composer check
- Autofix coding-style by
composer fix:cs
- All functionality must be tested
Supported PHP versions
- PHP 7.4 - 8.3
All versions of dead-code-detector with dependencies
PHP Build Version
Package Version
The package shipmonk/dead-code-detector contains the following files
Loading the files please wait ....