| name | codebase-cleanup |
| description | Codebase cleanup patterns. Technical debt identification, dead code removal, dependency audit. Use when cleaning up large codebases or reducing technical debt. |
Codebase Cleanup
Systematically reduce technical debt and improve codebase health.
When to Use
- Cleaning up accumulated technical debt
- Removing dead code and unused dependencies
- Preparing codebase for major feature work
- Improving performance and maintainability
- Before major Laravel upgrades
1. Technical Debt Categories
| Category | Examples | Impact |
|---|
| Code Debt | Duplication, complexity, bad naming | Slow development |
| Test Debt | Low coverage, flaky tests | Bugs in production |
| Dependency Debt | Outdated packages, vulnerabilities | Security risks |
| Documentation Debt | No docs, outdated comments | Onboarding pain |
| Architecture Debt | God classes, tight coupling | Hard to change |
2. Dead Code Removal
Find Unused Classes
grep -r "class \w\+" app/ | cut -d: -f2 | while read class; do
name=$(echo $class | grep -oP 'class \K\w+')
count=$(grep -r "$name" app/ --include="*.php" | wc -l)
if [ $count -eq 1 ]; then
echo "Possibly unused: $name"
fi
done
Find Unused Routes
php artisan route:list --json | jq '.[] | .uri'
grep -r "route(" resources/views/ --include="*.blade.php"
Find Unused Config
php artisan config:clear
php artisan about
Remove Unused Dependencies
composer show --direct
composer remove package/name
composer audit
3. Dependency Health
Check Outdated Packages
composer outdated
composer outdated --direct
composer update
composer update vendor/package
Priority Updates
| Priority | Criteria | Action |
|---|
| 🔴 Critical | Security vulnerabilities | Update immediately |
| 🟠 High | Major version behind | Plan update sprint |
| 🟡 Medium | Minor version behind | Update in next release |
| 🟢 Low | Patch version behind | Update when convenient |
Laravel-Specific Updates
php artisan --version
composer update laravel/framework
composer update laravel/*
4. Code Quality Metrics
Complexity Analysis
./vendor/bin/phpstan analyse app/ --level=5
./vendor/bin/rector process app/ --dry-run
Duplication Detection
composer require --dev sebastian/phpcpd
./vendor/bin/phpcpd app/
Test Coverage
php artisan test --coverage
php artisan test --coverage-html coverage/
5. Laravel-Specific Cleanup
Clean Up Routes
Route::get('/users', [UserController::class, 'index']);
Route::get('/users/{id}', [UserController::class, 'show']);
Route::post('/users', [UserController::class, 'store']);
Route::apiResource('users', UserController::class);
Route::prefix('admin')->middleware('auth:admin')->group(function () {
Route::apiResource('users', Admin\UserController::class);
Route::apiResource('orders', Admin\OrderController::class);
});
Clean Up Migrations
php artisan migrate:status
php artisan schema:dump
Clean Up Models
Clean Up Config
6. Quick Wins Checklist
Immediate Actions (< 1 hour each)
Short Term (< 1 day each)
Medium Term (< 1 week each)
7. Cleanup Workflow
Phase 1: Assessment
composer audit
./vendor/bin/phpstan analyse
./vendor/bin/pint --test
php artisan test --coverage
Phase 2: Prioritize
Create a cleanup backlog with impact scores:
| Item | Impact | Effort | Priority |
|------|--------|--------|----------|
| Security vuln in package X | High | Low | 🔴 Do now |
| Duplicate validation logic | Medium | Medium | 🟠 This sprint |
| Missing tests for OrderService | Medium | High | 🟡 Next sprint |
| Old commented code | Low | Low | 🟢 When available |
Phase 3: Execute
- One cleanup task per PR
- Run full test suite after each cleanup
- Document significant changes
Phase 4: Maintain
- Add quality gates to CI
- Regular dependency audits
- Periodic cleanup sprints
8. Prevention
CI Quality Gates
- name: Security check
run: composer audit
- name: Code style
run: ./vendor/bin/pint --test
- name: Static analysis
run: ./vendor/bin/phpstan analyse --level=5
- name: Tests
run: php artisan test
Pre-Commit Hooks
./vendor/bin/pint app/ --test
Commands Reference
composer audit
./vendor/bin/pint
./vendor/bin/phpstan analyse
php artisan test --coverage
composer outdated --direct
php artisan route:list
php artisan migrate:status
Remember: Cleanup is ongoing work, not a one-time event. Small, regular cleanup is better than large, occasional cleanups.