Skip to content

Lesson 8 of 9 · 24m

Migrating Legacy PHP Without a Big-Bang Rewrite

A battle-tested upgrade path using dependency pinning, characterization tests, Rector, static analysis, and incremental runtime jumps.

The teams that successfully migrate decade-old PHP do not “rewrite it properly.” They make the application boringly testable, automate mechanical language changes, and move one constraint at a time. The rewrite crowd discovers eighteen months later that the old system encoded payroll exceptions, fraud heuristics, and customer-specific behavior nobody remembered. Incremental migration is slower to start and much faster to finish.

First, freeze the facts

Record the current runtime, extensions, OS dependencies, and deployment topology before touching composer:

php -v
php -m
composer show --direct
composer show --locked

Use .platform.php in composer.json when production temporarily differs from developer machines:

{
  "config": {
    "platform": {
      "php": "7.4.33"
    }
  }
}

This prevents dependency resolution from installing packages that cannot run in production. It is a lie you tell composer for operational reasons, so remove the pin when production catches up.

Characterization tests before elegance

Legacy code does not need beautiful tests first. It needs tests that capture what it currently does. Golden master tests are often the right first step: feed fixed inputs, snapshot outputs, and refuse unintended drift. For web systems, HTTP-level tests around critical flows beat unit tests that mock the entire framework.

vendor/bin/phpunit --testsuite critical-path

You are not proving correctness in the abstract; you are creating a safety rail around behavior customers may depend on accidentally.

Rector for syntax, static analysis for truth

Rector can automate large mechanical moves: type declarations, visibility upgrades, null coalescing, constructor promotion where safe, and framework-specific migrations. Run rules incrementally and review diffs like any other risky change.

vendor/bin/rector process src --dry-run
vendor/bin/rector process src/Legacy/Billing --dry-run

PHPStan or Psalm then becomes the ratchet. Start at level 0 or 1, fix or baseline existing violations, and raise the level only after the baseline stops moving.

parameters:
    level: 3
    paths:
        - src
    treatPhpDocTypesAsCertain: false

Upgrade in controlled jumps

A common sane path is: modernize on the current runtime, jump to the newest PHP minor supported by dependencies, deploy, observe, then continue. Do not combine a PHP upgrade, framework major upgrade, database migration, and infrastructure move in one release unless downtime is part of the plan. Each variable you add destroys your ability to attribute failure.

The dependency wall is the real project

Composer packages abandoned since 2016 often matter more than language syntax. Decide whether to upgrade, replace, fork, or isolate each one. Pay particular attention to session handlers, cryptography, PDF generation, image processing, and anything monkey-patching core behavior. These are rarely visible in feature lists but fatal in migration week.

Field notes

  • Never run broad automated refactors and hand edits in one commit. Mechanical changes should be boring to review.
  • Baselines are a migration tool, not a permanent landfill. Burn them down module by module.
  • Test sessions, output buffering, headers, and file uploads; legacy behavior often hides in side effects outside pure logic.
  • Keep a compatibility matrix of PHP version, framework version, extensions, and deployment image. “Works locally” is not a matrix.
  • The migration is done when new features are written in the new style by default, not when the version number changes.

Was this lesson helpful?

Sign in to rate