Automating PHP Refactoring with Rector

Keeping code clean and updated is important in software development, but refactoring can take a lot of time, especially in big projects. PHP Rector is a tool that helps automate these changes, making sure your code follows best practices and modern PHP…


This content originally appeared on DEV Community and was authored by Aleson França

Keeping code clean and updated is important in software development, but refactoring can take a lot of time, especially in big projects. PHP Rector is a tool that helps automate these changes, making sure your code follows best practices and modern PHP standards.

What is PHP Rector?

Rector is a tool that automatically updates and improves PHP code based on predefined rules. it Can:

  • Upgrade PHP versions.
  • Apply coding standards automatically.
  • Remove old and unnecessary code.
  • Replace outdated function or method calls.
  • Add type hints to properties, function parameters, and return values.
  • Convert old docblocks into modern attributes.

Installation

You can install Rector using composer:
composer require --dev rector/rector

Configs

To configure Rector, create a rector.php file in your project's root folder:


<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\Php80\Rector\Class_\DoctrineAnnotationToAttributeRector;
use Rector\Php81\Rector\Class_\SpreadingVariadicArgumentsRector;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->rule(TypedPropertyRector::class);
    $rectorConfig->rule(DoctrineAnnotationToAttributeRector::class);
    $rectorConfig->rule(SpreadingVariadicArgumentsRector::class);
};

This example enables:

  • Typed properties (PHP 7.4+).
  • Conversion of Doctrine annotations to attributes (PHP 8+).
  • Modern variadic argument syntax (PHP 8.1+).

Running Rector

To apply Rector to your code, run:
vendor/bin/rector process src

This will analyze and update the code inside the src folder.

Examples

Adding Type Hints to Properties

Before:

<?php

class User {
    /** @var string */
    private $name;
}

After:

<?php

class User {
    private string $name;
}

Converting Annotations to Attributes

Before:

<?php

/**
 * @ORM\Entity()
 * @ORM\Table(name="users")
 */
class User {
    /** @ORM\Id */
    /** @ORM\GeneratedValue */
    /** @ORM\Column(type="integer") */
    private $id;
}

After:

<?php

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: "users")]
class User {
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: "integer")]
    private int $id;
}

Updating Variadic Argument Syntax

Before:

<?php

function sum(...$numbers) {
    return array_sum($numbers);
}

$values = [1, 2, 3];
echo sum(...$values);

After:

<?php

function sum(int ...$numbers): int {
    return array_sum($numbers);
}

$values = [1, 2, 3];
echo sum(...$values);

Conclusion

PHP Rector is a great tool for keeping your code clean and updated with less manual work. You can also add it to a CI/CD pipeline to make sure your project follows best practices automatically.

Do you use Rector in your projects? Share your experience in the comments!


This content originally appeared on DEV Community and was authored by Aleson França


Print Share Comment Cite Upload Translate Updates
APA

Aleson França | Sciencx (2025-03-10T21:13:49+00:00) Automating PHP Refactoring with Rector. Retrieved from https://www.scien.cx/2025/03/10/automating-php-refactoring-with-rector/

MLA
" » Automating PHP Refactoring with Rector." Aleson França | Sciencx - Monday March 10, 2025, https://www.scien.cx/2025/03/10/automating-php-refactoring-with-rector/
HARVARD
Aleson França | Sciencx Monday March 10, 2025 » Automating PHP Refactoring with Rector., viewed ,<https://www.scien.cx/2025/03/10/automating-php-refactoring-with-rector/>
VANCOUVER
Aleson França | Sciencx - » Automating PHP Refactoring with Rector. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/10/automating-php-refactoring-with-rector/
CHICAGO
" » Automating PHP Refactoring with Rector." Aleson França | Sciencx - Accessed . https://www.scien.cx/2025/03/10/automating-php-refactoring-with-rector/
IEEE
" » Automating PHP Refactoring with Rector." Aleson França | Sciencx [Online]. Available: https://www.scien.cx/2025/03/10/automating-php-refactoring-with-rector/. [Accessed: ]
rf:citation
» Automating PHP Refactoring with Rector | Aleson França | Sciencx | https://www.scien.cx/2025/03/10/automating-php-refactoring-with-rector/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.