PHP 7: Maintaining and Upgrading Legacy Apps · Working in a Legacy PHP Codebase
Reading and Mapping Legacy Code
Build a mental map of an unfamiliar PHP 7 application before changing it.
Before changing a legacy app, map it: entry points (index.php, cron scripts, CLI tools), routing (framework, .htaccess rewrites or one file per page), includes and globals, database access, and external integrations. Use IDE navigation, grep, Xdebug step-debugging, and tools like PhpMetrics or PHPStan at low levels to find hotspots. Write down the architecture as you learn it; focus changes where business value and bug density are highest.
# Find entry points and includes
grep -rln --include='*.php' "\$_GET\|\$_POST" public/ | head
grep -rn --include='*.php' "require_once\|include " src/ | wc -l
grep -rn --include='*.php' "mysql_query\|mysqli_query" . | head # raw SQL hotspots
# Churn x complexity: files changed most often
git log --since=1.year --name-only --pretty=format: | sort | uniq -c | sort -rn | head -20
Common pitfalls
- Rewriting from scratch before understanding behavior (the 'big rewrite' trap).
- Ignoring cron jobs and CLI scripts that depend on the same code.
- Assuming dead code is dead without checking logs or usage.
Key points
- Map entry points, routing, data access and integrations
- Prioritize by churn and business value
- Document what you learn
Further reading: Martin Fowler: Strangler Fig Application
Practice questions
How can git history help prioritize refactoring?
Answer
Files that change most often (high churn) and are complex are where bugs and costs concentrate; improving them yields the most value.
Why is a full rewrite of a legacy app risky?
Answer
It takes long, delivers no value until finished, and must rediscover undocumented behavior; incremental replacement (strangler fig) reduces risk.
Open in the app to mark lessons complete, take notes and track your progress.