DeveloperJoy Logo DeveloperJoy

Rector: A Comprehensive Tool for PHP Code Transformation

July 8, 2024
Rector: A Comprehensive Tool for PHP Code Transformation

Rector is a powerful PHP tool that offers instant upgrades and automated refactoring for your application's code.

Whether you're transitioning from an old PHP version to a new one or simply aiming to maintain high code quality with minimal manual intervention, Rector is designed to make these processes smoother and more efficient.

Key Features of Rector

Instant Upgrades

Rector supports upgrades from PHP 5.3 to 8.2 and can handle major open-source projects like Symfony, PHPUnit, and Doctrine. This means you can take advantage of the latest PHP features and security improvements without spending hours or even days on manual refactoring .

Automated Refactoring

Rector can continuously refactor your code to keep quality high, even with a team of new developers. By integrating Rector into your CI pipeline, you can ensure consistent code quality and receive smart code reviews around the clock .

Installation and Usage

To install Rector, you can add it as a development dependency using Composer:

composer require rector/rector --dev

Rector can be run using two primary approaches:

  1. Single Rule: Allows for changes to be controlled and reviewed individually.
  2. Group of Rules (Sets): Applies a predefined set of rules to achieve broader transformations.

To get started, create a rector.php configuration file in your project's root directory and modify it as shown:

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;

return RectorConfig::configure()
    ->withRules([
        TypedPropertyFromStrictConstructorRector::class
    ])
    ->withPreparedSets(
        deadCode: true,
        codeQuality: true,
        typeDeclarations: true,
        privatization: true,
        earlyReturn: true,
        strictBooleans: true,
    )
    ->withPhpSets();

You can then run Rector in dry-run mode to see the changes it would make without actually applying them:

vendor/bin/rector process src --dry-run

To apply the changes, simply drop the --dry-run option:

vendor/bin/rector process src

Prepared Rule Sets

The configuration file snippet above demonstrates how to use various prepared sets with Rector. These sets include:

  • Dead Code: Removes redundant code.
  • Code Quality: Improves the overall quality of the code.
  • Type Declarations: Adds or improves type declarations.
  • Privatization: Converts public properties and methods of internal classes to private ones where possible.
  • Early Return: Transforms code to use early return mechanisms for better readability.
  • Strict Booleans: Enforces strict boolean checks in conditional statements .

Community and Documentation

Rector has strong community backing and active maintainers who continuously contribute to its enhancements. If you're interested in contributing or need detailed documentation, you can visit the Rector Documentation page.

Additionally, Rector offers a book titled "Rector - The Power of Automated Refactoring," which takes you through the setup process and explains how to create your own rules .


Rector is an invaluable tool for PHP developers looking to automate repetitive coding tasks and ensure their codebase maintains high quality and compatibility with the latest PHP standards.

By leveraging Rector, teams can save countless hours on manual code reviews and upgrades, focusing more on building features and improving their applications.

For further exploration, you can refer to the full documentation and community resources available through Rector's official channels.

← Go back to the blog

Work with me

Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.

Get 1 month free with yearly plan during checkout.

Technical Advisor

$ 3 ,995

/m

What's included:

  • Lead your team to a better code architecture, testing mentality, clean code, and more.
  • Lead knowledge-sharing sessions depending on current company status and requirements.
  • Help with product and technical decisions.
  • Pause or cancel anytime.

Integrated Senior Developer

$ 5 ,995

/m

Not available

What's included:

  • Includes all Technical Advisor services.
  • Engages as a team member in daily tasks.
  • Participates actively in day-to-day development.
  • Direct communication:
    Slack, and meetings included.
  • Pause or cancel anytime.

Want to talk first?

Ok, just book a call now.

FAQ