Rector - Upgrade your Legacy App to Modern Codebase
Rector is under development phase in 2018, to figure out the best way to use it in applications, polish API of Rector classes and get feedback from community. Please consider this while using it and report issues or post ideas you'll come up with while using. Thank you!
When you're gonna move from manual work to instant upgrades?
Rector upgrades your application for you, with focus on open-source projects:
Rector can:
- Rename classes
- Rename class' methods
- Rename partial namespace
- Rename pseudo-namespace to namespace
- Add, replace or remove arguments
- Add typehints based on new types of parent class or interface
- And much more...
Install
composer require --dev rector/rector:'dev-master'
Do you Have Conflicts?
Since Rector uses project's autoload to analyze type of elements, it cannot be installed as project in standalone directory but needs to be added as dependency. Here bamarni/composer-bin-plugin
becomes useful.
In case you have conflicts with different nikic/php-parser
version, e.g. while using PHPStan, you can:
- either use phpstan-shim that solves this for PHPStan
- or help with similar prefixed PHAR for Rector
Extra Autoloading
Rector relies on project and autoloading of its classes. To specify own autoload file, use --autoload-file
option:
vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.php
How to Reconstruct your Code
A. Prepared Sets
Featured open-source projects have prepared sets. You'll find them in /config/level
.
Do you need to upgrade to Symfony 4.0, for example?
-
Run rector on your
/src
directory:vendor/bin/rector process src --level symfony40
Which is a shortcut for using complete path with
--config
option:vendor/bin/rector process src --config vendor/rector/rector/src/config/level/symfony/symfony40.yml
You can also use your own config file:
vendor/bin/rector process src --config your-own-config.yml
-
Do you want to see the preview of changes first?
Use the
--dry-run
option:vendor/bin/rector process src --level symfony33 --dry-run
-
What levels are on the board?
vendor/bin/rector levels
B. Custom Sets
-
Create
rector.yml
with desired Rectors:services: Rector\Rector\Contrib\Nette\Application\InjectPropertyRector: ~
-
Try Rector on your
/src
directory:vendor/bin/rector process src --dry-run
-
Apply the changes if you like them:
vendor/bin/rector process src
Configure Rectors for your Case
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.
Coding Standards are Outsourced
This package has no intention in formatting your code, as coding standard tools handle this much better. We prefer EasyCodingStandard with Rector's prepared set:
# install
composer require --dev symplify/easy-coding-standard
# check
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.neon
# fix
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.neon --fix
More Detailed Documentation
- How Rector Works?
- How to Create Rector with Fluent Builder
- How to Create Own Rector
- Service Name to Type Provider
How to Contribute
Just follow 3 rules:
-
1 feature per pull-request
-
New feature needs tests
-
Tests, coding standards and PHPStan checks must pass:
composer complete-check
Don you need to fix coding standards? Run:
composer fix-cs
We would be happy to merge your feature then.