Automate PHP Upgrades with Rector: A Step‑by‑Step Guide

This article explains how the Rector tool can automatically refactor and upgrade legacy PHP code—from installation and configuration to real‑world migration scenarios and CI/CD integration—helping developers modernize projects with minimal manual effort.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Automate PHP Upgrades with Rector: A Step‑by‑Step Guide

Overview

In PHP development, maintaining legacy code is difficult; Rector (Refactoring Rector) is an automated tool that can upgrade and refactor code from PHP 5.3 up to 8.5.

Core Features

Rector focuses on refactoring via AST parsing, can replace deprecated functions, add type declarations, adjust inheritance, and offers rule sets for dead‑code removal, code quality, and more. It provides a dry‑run mode to preview changes as diffs.

Quick Start

Install with Composer: composer require rector/rector --dev Create a rector.php configuration file defining rules, for example:

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;

return RectorConfig::configure()
    ->withPreparedSets(codeQuality: true, deadCode: true)
    ->withRules([TypedPropertyFromStrictConstructorRector::class])
    ->paths([__DIR__ . '/src', __DIR__ . '/tests']);

Run a dry‑run to preview: vendor/bin/rector src --dry-run Apply changes with vendor/bin/rector src Additional options --debug and --xdebug provide detailed reports and AST output.

Tip: Use Easy Coding Standard (ECS) after Rector to format the generated code.

Real‑World Scenarios

Rector can upgrade a PHP 5.6 e‑commerce system using the php74 or php81 rule sets, handling deprecated create_function() and array‑syntax changes, often achieving over 90 % test pass rate. It also supports framework migrations—e.g., Symfony controllers via rector-symfony and PHPUnit API updates—and can be integrated into CI/CD pipelines (GitHub Actions, Jenkins) to refactor code on each push.

Community & Support

The GitHub repository is highly active; core logic lives in the rector-src sub‑repo, with extensions such as rector-symfony. Project founder Tomas Votruba authored an e‑book “Rector – The Power of Automated Refactoring.” Commercial support is offered at getrector.com.

Conclusion

By 2025 Rector has become a standard tool in the PHP ecosystem, turning code maintenance from a burden into an asset and allowing developers to focus on new features rather than manual refactoring.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPrefactoringcode migrationRector
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.