PHP 7: Maintaining and Upgrading Legacy Apps · Upgrading from PHP 7 to PHP 8

Automating the Upgrade with Rector

Use Rector set lists to upgrade syntax and fix breaking patterns automatically.

Rector parses your code and applies rule sets: upgrade sets (withPhpSets(php80: true) …) modernize syntax and fix incompatibilities, while framework sets handle Symfony/Laravel/PHPUnit upgrades. Run it in small batches (per directory), review diffs, run tests, and commit. Configure withPhpVersion() to match the minimum PHP your production supports during the transition.

<?php
// rector.php
use Rector\Config\RectorConfig;

return RectorConfig::configure()
    ->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
    ->withPhpSets(php74: true)          // first: modernize up to 7.4 syntax
    ->withPreparedSets(deadCode: true, typeDeclarations: true)
    ->withSkip([__DIR__ . '/src/Legacy/Generated']);
// Later: ->withPhpSets(php83: true) once production runs PHP 8.3
vendor/bin/rector process src/Billing --dry-run   # preview
vendor/bin/rector process src/Billing             # apply, then run tests

Common pitfalls

  • Applying PHP 8 syntax sets while production still runs 7.4.
  • Running Rector across the whole codebase in one unreviewable commit.
  • Not re-running the code style fixer after Rector.

Key points

  • Rector applies upgrade rule sets
  • Match the set to production's PHP version
  • Small batches, review, test

Further reading: Rector documentation

Practice questions

Why not run withPhpSets(php83: true) while production still runs 7.4?

Answer

Rector would introduce PHP 8 syntax (match, promotion, union types) that fails to parse on 7.4. Only target the oldest version still deployed.

What's a safe workflow for Rector changes?

Answer

Run with --dry-run on one directory, review the diff, apply, run tests and static analysis, commit, and repeat for the next area.

Open in the app to mark lessons complete, take notes and track your progress.