rector README

The project's README from github.com/rectorphp/rector. As one markdown file: README.md.

Rector - Instant Upgrades and Automated Refactoring

Downloads


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.5 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:

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 src --dry-run

Rector will show you diff of files that it would change. To make the changes, drop --dry-run:

vendor/bin/rector 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 :heart:

The Rector community is powerful thanks to active maintainers who take care of Rector sets for particular projects.

Among there projects belong:


Hire us to get Job Done :muscle:

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 src/Controller --dry-run --debug

Or with Xdebug:

  1. Make sure Xdebug is installed and configured
  2. Add --xdebug option when running Rector
vendor/bin/rector 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

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 files with PHP + HTML content, you may need to manually verify the changed file after apply the changes.

rector (rectorphp/rector)

Searching Database…

10.4K ·GitHub
这是什么?Rector 是一个 PHP 工具,可以即时升级和重构 PHP 代码库:它自动完成从 PHP 5.3 到 8.5 的升级以及框架迁移,还可以通过 CI 持续保持代码质量。
它是我用来搭建项目的框架,还是对我现有代码运行的工具?它是一个命令行开发工具,不是库:你把它作为开发依赖安装,在项目根目录的 rector.php 配置文件里描述要做的改动,然后在终端里对整个代码库运行它。
我怎么把它跑在我自己的项目上?先用 Composer 安装,创建一个定义了规则的 rector.php,然后先用 dry-run 试运行——Rector 会打印出它打算做的改动的 diff。去掉 --dry-run 就会真正应用这些改动。
我的电脑上需要装什么才能运行它?只需要一个带 Composer 的 PHP 环境——它在 Linux、macOS 和 Windows 上都能从终端运行,不需要额外的服务或数据库。
在让它改写我的代码之前,有什么要注意的坑吗?有几个:因为 AST 会忽略空白字符,输出可能丢失原有格式(建议搭配 ECS 这类代码规范工具一起用);PHP 和 HTML 混写的文件需要人工检查;在 Windows 上请用 cmd 或 bash 运行——并行模式在 PowerShell 7 下会出问题。
Rector 是免费的吗,还是收费的?工具本身免费,而且公开开发。团队的收入来自可选的付费商业支持,面向那些宁愿雇他们来应用 Rector、也不愿自己钻研抽象语法树的团队。
有没有针对我的框架的现成规则集?很可能有——社区维护者为 Laravel、Drupal、CakePHP、TYPO3、Craft CMS、Shopware、Sylius、Sulu、Nette、Contao、Yii2 和 SilverStripe 等项目维护着专门的规则集。
当某个规则的行为不符合预期时,我该怎么调试?用 --debug 运行可以打印嵌套的异常信息,如果你用 Xdebug 就加上 --xdebug 选项,还可以使用内置的辅助工具来美化打印 AST 节点。
What are similar projects?ast-grep, PHP-Parser, infer, fallow, phpstan, rust-clippy, pyright, perch
Creator
rectorphp
Size
372.8 MB
Stars
10.4K
Age
9y
Commits · 90 Days