- What is shipmonk/dead-code-detector and how does it work?
- It is a PHPStan extension that analyzes your codebase for unused class members and dead cycles, including transitive dead code and members referenced only by tests. It works by loading PHPStan rules and usage providers, then reports dead code during a normal PHPStan run. For Laravel projects it adds framework-aware usage detection so container calls, routes, events, and attributes are not flagged incorrectly.
- How do I install shipmonk/dead-code-detector in a Laravel project?
- Install it as a dev dependency with composer require --dev shipmonk/dead-code-detector. Then include vendor/shipmonk/dead-code-detector/rules.neon in phpstan.neon.dist, or rely on PHPStan's official extension-installer. Run vendor/bin/phpstan against both src and tests so all usages can be discovered.
- Does shipmonk/dead-code-detector support Laravel?
- Yes, it is designed to fit Laravel codebases and includes Laravel-aware usage detection for Eloquent, routes, events, console commands, and attributes. For dynamic Laravel patterns it misses, you can add or adjust usage providers. It runs as part of PHPStan, so it does not modify your application runtime.
- How do I configure dead-code-detector in phpstan.neon?
- Add the rules.neon include and configure the shipmonkDeadCode parameters. You can toggle detection of deadMethods, deadConstants, deadEnumCases, and deadProperties with neverRead and neverWritten options, and you can configure usageProviders. Keep the default providers unless you need to exclude vendor or reflection-related usage.
- Can shipmonk/dead-code-detector detect code that is only used in tests?
- Yes. If you analyze both src and tests, it can distinguish members used only by tests and report them as dead tested code. You can enable the tests usage excluder so test-only references are not treated as production usage. This helps surface code that exists only to satisfy tests.
- Will it produce false positives for Laravel dynamic features like app()->make() or magic methods?
- It can, because Laravel relies on dynamic calls, magic methods, route binding, and container resolution that static analysis cannot always see. The package mitigates this with Laravel and library usage providers plus configurable exclusions. If you hit false positives, implement a custom MemberUsageProvider or disable problematic providers, and review baseline changes carefully.
- Can shipmonk/dead-code-detector automatically remove dead code?
- Yes, it supports an --error-format removeDeadCode mode that can automatically remove unused PHP code. Automated removal should be gated behind manual review or a dedicated PR because it can break tests or dynamic behavior. Prefer running it on a clean branch and verifying with your full test suite.
- Which libraries and class members does shipmonk/dead-code-detector support?
- It detects unused methods, constants, properties with never read and never written modes, enum cases, dead cycles, and transitive dead members. It supports Laravel plus libraries such as Symfony, Doctrine, Twig, and PHPUnit, with integrations auto-enabled when those dependencies are present. Supported library features include attributes for commands, listeners, routes, and scheduled tasks where applicable.
- How does shipmonk/dead-code-detector perform on large Laravel codebases?
- Performance depends on codebase size and PHPStan configuration; large Laravel monoliths can be slower because PHPStan analyzes the whole codebase. Run analysis incrementally, use PHPStan parallelism where available, and exclude vendor, storage, and generated paths. Start with a baseline and address new dead code in CI rather than blocking on the whole legacy backlog.
- Is shipmonk/dead-code-detector actively maintained, and what should I watch for with PHPStan updates?
- It is actively maintained and tracks PHPStan's evolving extension API, so pin the package version and review release notes before upgrading PHPStan. There is no exact Laravel version matrix in the package description, so check Composer constraints for PHP and PHPStan compatibility. For alternatives, you can combine PHPStan strict rules with other dead-code tools, but this package's Laravel-aware providers and auto-removal are its differentiators.