3.6 KiB
Rector - Reconstruct your Legacy Code to Modern Codebase
This tool will upgrade your application for you.
All Reconstructors
At the moment these packages are supported:
Install
composer require rector/rector --dev
Install on PHP < 7.1
You must have separated environment with PHP 7.1 (for example in Docker container). When you have it then run following command.
composer create-project rector/rector your-path-to-rector
When do you have it then you can run all commands like
your-path-to-rector/bin/rector process /var/www/old-project --level=nette24
your-path-to-rector/bin/rector process /var/www/another-old-project --level=symfony40
How To Reconstruct your Code?
- Create
rector.yml
with desired Rectors
rectors:
- Rector\Rector\Contrib\Nette\Application\InjectPropertyRector
- Run rector on your
/src
directory
vendor/bin/rector process src
- Check the Git
git diff
6 Steps to Add New Rector
Just extend Rector\Rector\AbstractRector
.
It will prepare 2 methods processing the node.
public function isCandidate(Node $node): bool
{
}
public function refactor(Node $node): ?Node
{
}
- Put it under
namespace Rector\Contrib\<set>;
namespace
<?php declare(strict_types=1);
namespace Rector\Contrib\Symfony;
use Rector\Rector\AbstractRector;
final class MyRector extends AbstractRector
{
// ...
}
-
Add a Test Case
-
Add to specific level, e.g.
/src/config/level/nette/nette24.yml
-
Submit PR
-
👍
Simpler setup with Dynamic Rectors
You don't have to always write PHP code. Many projects change only classes or method names, so it would be too much work for a simple task.
Instead you can use prepared Dynamic Rectors directly in *.yml
config:
You can:
-
replace class name
# phpunit60.yml rectors: Rector\Rector\Dynamic\ClassReplacerRector: # old class: new class 'PHPUnit_Framework_TestCase': 'PHPUnit\Framework\TestCase'
-
replace part of namespace
# better-reflection20.yml rectors: Rector\Rector\Dynamic\NamespaceReplacerRector: 'BetterReflection': 'Roave\BetterReflection'
-
or change method name
# nette24.yml rectors: Rector\Rector\Dynamic\MethodNameReplacerRector: # class: # old method: new method 'Nette\Utils\Html': 'add': 'addHtml'
READMEs for Subpackages
How to Contribute
Just follow 3 rules:
-
1 feature per pull-request
-
New feature needs tests
-
Tests, coding standard and PHPStan checks must pass
composer all
Don you need to fix coding standards? Run:
composer fix-cs
We would be happy to merge your feature then.