explaingit

rectorphp/rector

10,300PHPAudience · developerComplexity · 3/5Setup · moderate

TLDR

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.

Mindmap

mindmap
  root((Rector))
    What It Does
      Automated code upgrade
      Rule-based rewriting
      CI enforcement
    Supported Targets
      PHP 5.3 to 8.5
      Symfony and Doctrine
      PHPUnit and Laravel
    How It Works
      AST parsing
      Dry-run preview
      rector.php config
    Known Limits
      Whitespace loss after rewrite
      Windows parallel mode issues
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Automatically update a PHP 7 codebase to PHP 8 syntax by running Rector with the appropriate upgrade rule set.

USE CASE 2

Add Rector to a CI pipeline to enforce code quality rules and reject PRs that introduce deprecated patterns.

USE CASE 3

Migrate a Symfony, PHPUnit, or Doctrine project to a newer major version by applying the relevant named rule sets.

USE CASE 4

Preview every change Rector will make as a diff using dry-run mode before any files are modified.

Tech stack

PHPComposer

Getting it running

Difficulty · moderate Time to first run · 30min

Run a formatter like ECS after Rector because AST re-serialization may alter whitespace and code style.

In plain English

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.

Copy-paste prompts

Prompt 1
I have a PHP 7.4 project and want to upgrade it to PHP 8.2. Write a rector.php config file that applies all relevant upgrade rules and enables the new PHP 8 syntax features.
Prompt 2
Help me add Rector to a GitHub Actions CI pipeline so pull requests are blocked if they introduce patterns that my rector.php rules flag.
Prompt 3
Show me how to write a custom Rector rule that automatically replaces all calls to a deprecated function with its recommended replacement.
Prompt 4
After running Rector my code formatting is inconsistent. Show me how to integrate ECS (Easy Coding Standard) as a follow-up step to restore consistent style.
Open on GitHub → Explain another repo

← rectorphp on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.