carlos-granados / rector
Instant Upgrade and Automated Refactoring of any PHP code (With extra features)
Fund package maintenance!
tomasvotruba
Requires
- php: ^7.2|^8.0
- phpstan/phpstan: ^1.12.5
Suggests
- ext-dom: To manipulate phpunit.xml via the custom-rule command
This package is auto-updated.
Last update: 2024-12-13 08:14:44 UTC
README
This is a fork of the Rector project which offers several new capabilities:
Ignore rector rules for particular lines in a file
You can now use some annotations in comments to ignore a rector rule at a particular point in any file, without having to ignore it for the whole file.
@rector-ignore-next-line
will let you skip any rector rule that might be applied to the node defined in the next line@rector-ignore RULE_NAME
will let you skip any particular rector rule that might be applied to the node defined in the next line.RULE_NAME
needs to be the name of the class for that particular rule (for exampleNumericReturnTypeFromStrictScalarReturnsRector
) and you need to add the corresponding use statement (or provide the FQN name of the rule). If you want to skip more than one rule you can list them separated with commas
Process dependent files
When using the cache, we also process dependent files of modified files to make sure we have covered all possible needed changes.
Add option to insert use imports in sorted order
Add a new importInsertSorted
parameter to the withImportNames()
config function.
When this option is set to true, any new use import statements will be inserted in sorted order, including sorting within already existing use statements.
Takes into account the different possible use statement types (normal, function and const). Also works for use groups, including mixed use groups.
This assumes that the existing use statements are already sorted. If that is not the case, the new use statements will be sorted but it is not guaranteed that they will be sorted within the existing use statements as we do not re-order those.
Rector - Instant Upgrades and Automated Refactoring
Rector instantly upgrades and refactors the PHP code of your application. It can help you in 2 major areas:
1. Instant Upgrades
Rector now supports upgrades from PHP 5.3 to 8.2 and major open-source projects like Symfony, PHPUnit, and Doctrine. Do you want to be constantly on the latest PHP and Framework without effort?
Use Rector to handle instant upgrades for you.
2. Automated Refactoring
Do you have code quality you need, but struggle to keep it with new developers in your team? Do you want to see smart code-reviews even when every senior developers sleeps?
Add Rector to your CI and let it continuously refactor your code and keep the code quality high.
Read our blogpost to see how to set up automated refactoring.
Install
composer require rector/rector --dev
Running Rector
There are 2 main ways to use Rector:
- a single rule, to have the change under control
- or group of rules called sets
To use them, create a rector.php
in your root directory:
vendor/bin/rector
And modify it:
use Rector\Config\RectorConfig; use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector; return RectorConfig::configure() // register single rule ->withRules([ TypedPropertyFromStrictConstructorRector::class ]) // here we can define, what prepared sets of rules will be applied ->withPreparedSets( deadCode: true, codeQuality: true );
Then dry run Rector:
vendor/bin/rector process src --dry-run
Rector will show you diff of files that it would change. To make the changes, drop --dry-run
:
vendor/bin/rector process src
Documentation
Learn Faster with a Book
Are you curious, how Rector works internally, how to create your own rules and test them and why Rector was born? Read Rector - The Power of Automated Refactoring that will take you step by step through the Rector setup and how to create your own rules.
Empowered by Community ❤️
The Rector community is powerful thanks to active maintainers who take care of Rector sets for particular projects.
Among there projects belong:
- palantirnet/drupal-rector
- craftcms/rector
- FriendsOfShopware/shopware-rector
- sabbelasichon/typo3-rector
- sulu/sulu-rector
- efabrica-team/rector-nette
- Sylius/SyliusRector
- CoditoNet/rector-money
- laminas/laminas-servicemanager-migration
- cakephp/upgrade
- driftingly/rector-laravel
- contao/contao-rector
- php-static-analysis/rector-rule
Hire us to get Job Done 💪
Rector is a tool that we develop and share for free, so anyone can automate their refactoring. But not everyone has dozens of hours to understand complexity of abstract-syntax-tree in their own time. That's why we provide commercial support - to save your time.
Would you like to apply Rector on your code base but don't have time for the struggle with your project? Hire us to get there faster.
How to Contribute
See the contribution guide or go to development repository rector/rector-src.
Debugging
You can use --debug
option, that will print nested exceptions output:
vendor/bin/rector process src/Controller --dry-run --debug
Or with Xdebug:
- Make sure Xdebug is installed and configured
- Add
--xdebug
option when running Rector
vendor/bin/rector process src/Controller --dry-run --xdebug
To assist with simple debugging Rector provides 2 helpers to pretty-print AST-nodes:
use PhpParser\Node\Scalar\String_; $node = new String_('hello world!'); // prints node to string, as PHP code displays it print_node($node);
Known Drawbacks
-
Rector uses nikic/php-parser, built on technology called an abstract syntax tree (AST). An AST doesn't know about spaces and when written to a file it produces poorly formatted code in both PHP and docblock annotations.
-
Rector in parallel mode will work most of the times for most OS. On Windows, you may encounter issues unresolvable despite of following the Troubleshooting Parallel guide. In such case, check if you are using Powershell 7 (pwsh). Change your terminal to command prompt (cmd) or bash for Windows.
How to Apply Coding Standards?
Your project needs to have a coding standard tool and a set of formatting rules, so it can make Rector's output code nice and shiny again.
We're using ECS with this setup.
May cause unexpected output on File with mixed PHP+HTML content
When you apply changes to File(s) thas has mixed PHP+HTML content, you may need to manually verify the changed file after apply the changes.