gitmyhub

rector

PHP ★ 10k updated 4h ago

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code

Rector is a command-line tool that automatically upgrades and rewrites PHP code, point it at your project and it applies the changes needed to move to a newer PHP or framework version without manual editing.

PHPComposersetup: moderatecomplexity 3/5

Rector is a command-line tool for PHP developers that automatically upgrades and rewrites PHP code. Instead of manually tracking deprecations or breaking changes when moving to a newer version of PHP or a framework like Symfony, PHPUnit, or Doctrine, you point Rector at your codebase and it applies the necessary transformations for you. It supports PHP versions from 5.3 up to 8.5.

The tool works by parsing your PHP files into an abstract syntax tree, applying transformation rules, and writing the modified files back. You configure it with a rector.php file at the root of your project, where you specify which individual rules or named rule sets to run. A dry-run mode lets you preview every change as a diff before anything is actually written, so you can inspect the proposed modifications first.

Beyond version upgrades, Rector can be added to a continuous integration pipeline to enforce code quality rules automatically. If your team introduces patterns you want to avoid, a Rector rule can catch and rewrite them on every commit, acting as a kind of automated code review for structural issues.

There is an active community that maintains rule sets for specific PHP frameworks and platforms beyond the core ones, including Drupal, Laravel, Shopware, TYPO3, CakePHP, Silverstripe, and others. The company behind Rector also offers paid consulting for teams that want help applying it to large or complex codebases.

A known limitation is that because the tool parses and re-serializes PHP through an AST library, the output may lose original whitespace and formatting. The recommendation is to run a separate code formatter like ECS after Rector to clean up the style. On Windows, there can also be issues with parallel processing mode that may require switching to a command prompt terminal.

Where it fits