A tested PHP upgrade workflow with Rector, PHPStan and your test suite
Upgrade a PHP application one version at a time: pin the baseline, let Rector rewrite syntax, let PHPStan find what Rector cannot, and prove it with tests before you switch runtimes.
On this page
Rector rewrites PHP code automatically: it can add types, replace removed functions and adopt new syntax. It does not know your business logic, so the workflow below wraps it in checks. It works for any jump, from PHP 7.4 to 8.6.
Before you start
- Tests on the current version must pass. If coverage is thin, write characterization tests for the most important paths first: tests that assert today’s behaviour, even if it is odd.
- Pin the current runtime in CI, then add a second job for the target version that is allowed to fail.
- Generate your list of version-specific changes with the PHP upgrade checklist. Keep it open; you will tick items off as you go.
Step 1: upgrade dependencies first
composer require php:^8.4 --no-update
composer update --with-all-dependencies --dry-run
Old packages are the most common blocker. Upgrade framework majors separately from the PHP upgrade where you can, and read each package’s changelog for breaking changes.
Step 2: configure Rector for one version step
composer require --dev rector/rector
<?php
// rector.php
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
->withPhpSets(php81: true); // one step at a time: php80, then php81, ...
withPhpSets() applies every rule up to that version. Move one minor version per pull request when the jump is large; the diffs stay reviewable.
Step 3: dry run, review, apply
vendor/bin/rector process --dry-run
vendor/bin/rector process
Read the diff like any pull request. Typical changes: constructor property promotion, str_contains() instead of strpos() !== false, explicit nullable types (?Foo $x = null, required in 8.4), and match instead of simple switch blocks.
Step 4: run PHPStan against the target version
Set phpVersion in phpstan.neon to the target and run it. It finds calls to removed functions, wrong argument counts and signature changes that Rector did not touch. If the existing code produces a lot of noise, start from a baseline.
Step 5: run the tests with every notice visible
php -d error_reporting=-1 -d display_errors=1 vendor/bin/phpunit
Fix deprecations now, not later: today’s deprecation is the next version’s error. In PHPUnit 10+, use failOnDeprecation="true" and displayDetailsOnTestsThatTriggerDeprecations="true" in phpunit.xml.
Step 6: switch the runtime
- Deploy to staging on the new PHP version, with the same extensions (
php -m) and ini settings. - Watch the error log under real traffic for a day.
- Switch production, keeping the old runtime available for a quick rollback.
- Remove the old version from CI once you are confident.
What Rector will not catch
Behaviour changes with the same syntax: string-to-number comparisons in 8.0, locale-insensitive strtolower() in 8.2, stricter session cookie defaults in 8.6. These are why the checklist and the tests matter more than the automated diff.