← All posts

Build Tools

Rector: Automated PHP Upgrades and Refactoring at Scale

Rector automatically rewrites PHP - upgrading versions, fixing deprecations, and refactoring across a whole codebase.

R Build Tools

Framework and language upgrades produce the same miserable work every time: dozens of files with the same deprecated call, the same renamed method, the same signature that changed - each edit mechanical, all of them tedious, and easy to get subtly wrong by hand. Rector automates it. It parses your PHP into an abstract syntax tree, applies rules that transform the code, and writes back correct, formatted PHP across the entire codebase at once.

Rules That Rewrite Code

Rector ships hundreds of rules grouped into sets: upgrade PHP 8.1 to 8.3, apply a framework's version migration, or modernize patterns (add types, simplify conditionals). You enable the sets you want and Rector does the edits.

Drupal Upgrades, Automated

For this world specifically, the drupal-rector ruleset encodes Drupal's API deprecations as transformations - so when a core update renames or removes an API, Rector rewrites your calls to the new form automatically. It turns the worst part of a major Drupal upgrade into a reviewable diff.

Dry-Run First

Rector's --dry-run shows you every change as a diff without touching a file, so you review before you commit:

# Preview the changes - writes nothing vendor/bin/rector process web/modules/custom --dry-run # Apply them vendor/bin/rector process web/modules/custom

Rough Edges and Limitations

  • Always review the diff. Automated rewrites are usually right, but you own what lands - read the changes before committing.
  • Config has a learning curve. Choosing rule sets and scoping paths takes an initial investment.
  • Big rewrites want a clean tree. Run it on a committed state so you can diff and revert easily.
  • Formatting may need a follow-up. Pair it with a code-style fixer to normalize the output.

For upgrades and large-scale refactors, Rector converts days of careful manual editing into a diff you review in an afternoon.

Internal link suggestion: link "deprecations" to the PHPStan post, which finds the upgrade blockers Rector then rewrites.