- How do I install and configure shipmonk/dead-code-detector for Laravel?
- Install via Composer with `composer require --dev shipmonk/dead-code-detector`, then include the rules in your `phpstan.neon` by adding `includes: [- vendor/shipmonk/dead-code-detector/rules.neon]`. No additional Laravel-specific setup is needed unless you use Symfony components like DIC, which requires `phpstan/phpstan-symfony` or explicit container XML paths.
- Does this package work with Laravel’s dynamic features like route binding or `app()->make()`?
- Yes, but dynamic Laravel features may trigger false positives. Mitigate this by configuring `shipmonkDeadCode.usageProviders` to exclude problematic providers (e.g., vendor or reflection) or use the `MemberUsageExcluder` interface to manually exclude dynamic calls like `app()->make()` or `macro()` invocations.
- Can I detect dead code that’s only used in tests without false positives?
- Yes, the package supports detecting dead-tested code. Enable the `tests` excluder in your `phpstan.neon` to avoid false positives, but verify that test-only usages are explicitly marked (e.g., `Unused X (all usages excluded by tests excluder)`). This ensures you don’t accidentally remove code relied upon by tests.
- Will this slow down my Laravel CI pipeline if I analyze the entire codebase?
- For large Laravel monoliths, analysis may introduce overhead. Mitigate this by running PHPStan incrementally with `--generate-baseline` or excluding paths like `vendor/` and `storage/`. For CI/CD, consider parallelizing scans (PHPStan 1.10+) or splitting analysis by module to balance speed and coverage.
- How do I automatically remove dead code without breaking my Laravel app?
- Use the `--error-format removeDeadCode` flag, but gate this behind manual review in CI to avoid unintended breaks. Start with a dry run (e.g., `--error-format=json`) to review changes before enabling auto-removal. Always test in a staging environment first, especially for Laravel’s dynamic features like events or service containers.
- Does this package support Laravel’s new attributes like #[AsCommand] or #[ObservedBy]?
- Yes, the package natively supports Laravel’s attributes like `#[AsCommand]`, `#[ObservedBy]`, and Symfony’s attributes (e.g., `#[Route]`, `#[AsEventListener]`). However, if you use newer Laravel attributes (e.g., `#[AsPeriodicTask]`), monitor the package’s release notes for updates, as PHPStan’s API evolves and may require adjustments.
- How do I exclude specific Laravel components (e.g., Livewire, Forge) from dead code detection?
- Use the `MemberUsageExcluder` interface to create a custom provider for unsupported components. For example, exclude Livewire’s magic methods or Forge’s dynamic class loading by configuring `shipmonkDeadCode.usageProviders` to ignore paths or patterns specific to those libraries. Document exclusions in your `phpstan.neon` for clarity.
- Can I integrate dead code reports into GitHub Actions or GitLab CI for PR checks?
- Yes, the package outputs results in standard formats (e.g., JSON, GitHub Actions annotations) when used with PHPStan’s CLI. Configure your CI to run `vendor/bin/phpstan analyze --error-format=github` and link results to PRs. For deeper integration, use tools like GitHub Advanced Security or IDE plugins (e.g., PHPStan’s VSCode extension) to visualize dead code.
- What Laravel versions and PHPStan versions are supported?
- The package is framework-agnostic but optimized for Laravel 10+ and PHPStan 1.10+. Check the [GitHub repository](https://github.com/shipmonk-rnd/dead-code-detector) for the latest compatibility matrix. For older Laravel versions (e.g., 9.x), ensure PHPStan’s version supports your project’s PHP version (e.g., PHP 8.1+).
- Are there alternatives to shipmonk/dead-code-detector for Laravel?
- Alternatives include PHPStan’s built-in `DeadCode` rule (less feature-rich) or standalone tools like `pdepend` or `phpmd`. However, this package stands out for its Laravel-specific support (e.g., Eloquent, Events) and transitive dead code detection. For Symfony-heavy projects, `phpstan/phpstan-symfony` may offer tighter integration, but lacks Laravel’s dynamic feature handling.