← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Rector — Automated PHP Upgrades & Refactoring

style PHP 7.1+ Intermediate

Also Known As

Rector PHP automated refactoring PHP upgrade automation

TL;DR

Rector applies rule-based AST transformations to upgrade PHP versions, migrate frameworks, and enforce coding patterns — automatically.

Explanation

Rector (Reconstruct, Refactor) uses PHP-Parser to build an AST, applies transformation rules, and rewrites the source files. Primary uses: automated PHP version upgrades (replace deprecated functions, add typed properties, convert to match expressions, add return types), framework migrations (Symfony 4→5→6, Laravel upgrades, PHPUnit version migrations), and custom rule sets for enforcing architectural patterns. Run rector process --dry-run to preview changes; rector process to apply them. Commit Rector output as a separate commit for clean diffs. Write custom Rector rules to automate project-specific refactors — extracting a recurring pattern into a method or renaming a class across the codebase. Pair with PHPStan to validate the output is type-correct.

Common Misconception

Rector is only useful for upgrading PHP version syntax. Rector applies any rule expressible as an AST transformation — coding standard migrations, framework upgrades, deprecation fixes, custom architectural refactors, and type annotation upgrades are all automatable with custom Rector rules.

Why It Matters

Rector automates PHP code upgrades and refactoring — applying hundreds of rules to modernise syntax, upgrade to new PHP versions, and fix deprecated patterns in seconds across thousands of files.

Common Mistakes

  • Running Rector on the whole codebase without reviewing changes — automated changes can be wrong.
  • Not committing before running Rector — no clean rollback if a rule causes issues.
  • Using only the 'upgrade' rule set and ignoring code quality rules that reduce complexity.
  • Not creating a custom Rector rule for project-specific repetitive refactors.

Code Examples

✗ Vulnerable
# Running Rector without review — risky:
rector process src/ --no-diffs  # Applies changes silently

# Safe workflow:
git stash  # Clean state
rector process src/ --dry-run   # Preview changes
# Review output, adjust rector.php rules
rector process src/             # Apply after review
git diff                        # Review all changes before committing
✓ Fixed
// Rector — automated PHP refactoring and upgrades

// Install:
$ composer require --dev rector/rector

// rector.php config
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;
use Rector\Set\ValueObject\LevelSetList;

return RectorConfig::configure()
    ->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
    ->withSets([
        LevelSetList::UP_TO_PHP_83,          // upgrade deprecated syntax
        SetList::DEAD_CODE,                   // remove dead code
        SetList::CODE_QUALITY,                // simplify expressions
        SetList::TYPE_DECLARATION,            // add missing type hints
    ]);

// Dry run first:
$ vendor/bin/rector process --dry-run

// Apply:
$ vendor/bin/rector process
$ composer test  // verify nothing broke

Added 15 Mar 2026
Edited 22 Mar 2026
Views 28
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 2 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T
No pings yesterday
Perplexity 7 Amazonbot 6 SEMrush 3 ChatGPT 2 Google 2 Unknown AI 2 Ahrefs 1
crawler 22 crawler_json 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Run 'vendor/bin/rector process --dry-run' to preview automated refactoring changes; configure rector.php with the PHP version upgrade rules for your target
📦 Applies To
PHP 7.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Old PHP syntax patterns (create_function strpos===false each()) that Rector can automatically upgrade
Auto-detectable: ✓ Yes rector phpstan phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: File Tests: Update

✓ schema.org compliant