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

Rector — Automated PHP Upgrades & Refactoring

Style PHP 7.1+ Intermediate
debt(d5/e3/b3/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches' (d5), Rector itself (listed in detection_hints.tools) detects upgradeable patterns via --dry-run; PHPStan/PHPCS also flag many of the same legacy patterns.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3), the quick_fix is running 'rector process' with configured rules — automated but requires reviewing and committing changes across files, slightly more than a one-line patch.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3), Rector is a dev-tooling commitment (rector.php config, custom rules) that one part of the workflow pays for; doesn't shape runtime architecture.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5), the misconception that Rector is only for PHP version upgrades is a documented gotcha — developers miss its broader refactoring capability, and automated changes can silently introduce bugs if not reviewed.

About DEBT scoring →

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 71
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 0 pings W 1 ping T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 1 ping W 2 pings T 5 pings F 1 ping S 5 pings S 5 pings M 1 ping T 0 pings W 2 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 2 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Scrapy 19 Amazonbot 7 Perplexity 7 Google 6 ChatGPT 5 SEMrush 5 Ahrefs 4 Unknown AI 2 Claude 2 Bing 1 Meta AI 1 PetalBot 1
crawler 56 crawler_json 4
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