tomasvotruba/class-leak
Finds class leaks in PHP apps: detects classes unintentionally pulled into your runtime via autoloading, service container or tests. Helps trim dependencies, speed up CI, and keep boundaries clean by reporting unexpected class usage with clear, actionable output.
Install the package via Composer in your Laravel project’s dev dependencies:
composer require --dev tomasvotruba/class-leak
Run the analyzer on your codebase:
vendor/bin/class-leak
Start with high-impact areas like app/Services, app/Http/Controllers, or app/Models—these often hide legacy or unused classes. The output lists potential leaks with file paths and usage context. Prioritize reviewing classes flagged in production-critical paths first.
vendor/bin/class-leak --error-format=checkstyle > class-leaks.xml to CI. Fail builds only if new leaks are introduced (use --diff or -v to compare against baseline).--output-diff=rector.yaml, then apply using Rector (ensure tests pass before committing).new Classname, app()->make(), config(), routes, event listeners, and facade usage.--generate-baseline) to track new leaks over time—critical for long-term maintainability SLAs.Laravel-Specific False Negatives: The tool performs static analysis and won’t detect usage in dynamic contexts like:
app()->call([NewClass(), 'method'])EventServiceProvider'UserController@index')config('services.driver.class'))
→ Fix: Use @Leaking annotations on intentionally unused classes, or wrap with @phpstan-ignore where appropriate.Autoloading Misconfiguration: If Composer’s autoload config (psr-4, classmap) is incorrect, the tool may miss references. Verify composer dump-autoload --optimize works and composer show -o matches your app/ structure.
Exclude Legacy Code: Add a class-leak.neon config to silence known leaks:
parameters:
paths:
- app
excludePaths:
- app/Legacy/*
- app/Infrastructure/Bridge/*
Debug Traces: Run with -vvv to see why a class is considered leaking—including which references were found (or not). This helps distinguish missing usage from reflection-based usage.
False Positives with Factories: Laravel’s model factories (Factory::new()->create()) may trigger leaks on factory classes—mark them with @Leaking or add database/factories/* to excludes.
How can I help you explore Laravel packages today?