Rector, also known as rector/rector, is an exceptional tool designed to instantly upgrade and refactor your PHP code. It effectively serves two main purposes. Firstly, it offers instant upgrades from PHP 5.3 to 8.2 and supports major open-source projects such as Symfony, PHPUnit and Doctrine. This ensures that you can have your code constantly at the cutting-edge level of PHP and frameworks without exerting any effort. Secondly, Rector allows for automated refactoring. If you want to maintain high code quality, especially when introducing new developers to your team, Rector can streamlines this process. Simply add Rector to your CI and let it continuously refactor your code to maintain a high code quality.
To use Rector/rector, start by installing it using composer with the following command: composer require rector/rector --dev
. After installing, there are two main ways to use Rector, either by a single rule for controlled changes, or by groups of rules known as sets. This can be done by creating a rector.php
file in your root directory with the command vendor/bin/rector
, and modifying it as follows:
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
return static function (RectorConfig $rectorConfig): void {
// register single rule
$rectorConfig->rule(TypedPropertyFromStrictConstructorRector::class);
// here we can define, what sets of rules will be applied
// tip: use "SetList" class to autocomplete sets with your IDE
$rectorConfig->sets([
SetList::CODE_QUALITY
]);
};
You can then run Rector using the command: vendor/bin/rector process src
. If you prefer to verify the changes that would be made first, use the --dry-run
option as follows: vendor/bin/rector process src --dry-run
.
For complete documentation and additional details on how to use Rector/rector, you can visit the official documentation. You can also explore the find variety of Rector Rules through the Rector Rules overview page. For a more in-depth understanding of how Rector works, creating your own rules, and more, consider reading Rector - The Power of Automated Refactoring book. This book guides you step by step through the Rector setup and how to create your own rules.