Insights:PHP Legacy System Refactoring: When to Rewrite and When Not To
Five Decision Signals
- Every requirement change touches multiple places, with extremely high regression costs.
- Security vulnerabilities recur repeatedly, and patches only make things messier.
- Performance issues can't be solved by localized optimization.
- Dependencies are obsolete — they can't be upgraded or their maintainers are gone.
- The business model has changed, and the old structure can't support the new workflow.
"The code just looks old" is not sufficient grounds for a rewrite.
Three Approaches
Approach 1: Patch
Suitable when:
- The system is generally functional.
- Issues are concentrated in a small number of modules.
- The business cannot afford prolonged downtime.
Actions: add testing, fix security issues, add logging, and partially decouple.
Approach 2: Incremental Refactoring
Suitable when:
- The system runs, but the structure is gradually spiraling out of control.
- Modules can be replaced one at a time.
- You have time and personnel for sustained improvement efforts.
Actions: isolate boundaries first, then migrate module by module while keeping old and new systems running in parallel.
Approach 3: Full Rewrite
Suitable when:
- The business model has fundamentally changed.
- The old architecture cannot support the new direction.
- You have clear requirements and a migration budget.
- You can tolerate months of parallel maintenance.
A rewrite must be accompanied by data migration, dual-run, and rollback plans.
Comparison Table
| Dimension | Patch | Incremental Refactoring | Rewrite |
|---|---|---|---|
| Risk | Low | Medium | High |
| Timeline | Short | Medium-long | Long |
| Cost | Low | Moderate | High |
| Business interruption | Minimal | Controllable | Requires planning |
| Suitable for | Localized issues | Structural issues | Pattern changes |
Preparation Before Rewriting
- Clearly document existing features and data models.
- List all entry points and external integrations.
- Prepare data migration scripts and verification procedures.
- Design dual-run and rollback plans.
- Define acceptance criteria and go-live windows.
- Keep the old system in read-only mode for a period.
Common Failure Causes
- Underestimating hidden logic in the legacy system.
- Lack of data migration validation.
- Switching before old and new features are equivalent.
- No rollback plan.
- Changing business requirements during development.
- Focusing only on code while ignoring operations.
Boundaries
Refactoring or rewriting can resolve structural issues but cannot guarantee business success. Decisions should be based on business value, risk tolerance, and maintenance capability—not simply “new technology is better.”
Related Reading: