DEV Community

Aleson França
Aleson França

Posted on

1

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 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);
};
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

After:

<?php

class User {
    private string $name;
}
Enter fullscreen mode Exit fullscreen mode

Converting Annotations to Attributes

Before:

<?php

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

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;
}
Enter fullscreen mode Exit fullscreen mode

Updating Variadic Argument Syntax

Before:

<?php

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

$values = [1, 2, 3];
echo sum(...$values);
Enter fullscreen mode Exit fullscreen mode

After:

<?php

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

$values = [1, 2, 3];
echo sum(...$values);
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (1)

Collapse
 
cavo789 profile image
Christophe Avonture

Rector isn't a tool it's a coach. Never judge, always teach you on how to better code.

I love it since years.