← 返回命令列表

Linux command

rector 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Run rector

vendor/bin/rector process [src/]

Dry run

vendor/bin/rector process [src/] --dry-run

Use a specific configuration file

vendor/bin/rector process [src/] --config [rector.php]

Run only a single rule

vendor/bin/rector process [src/] --only [TypedPropertyFromAssignsRector]

Show all loaded and skipped rules

vendor/bin/rector list-rules

Initialize a rector.php configuration file

vendor/bin/rector init

Clear cache before processing

vendor/bin/rector process --clear-cache

Process a single file

vendor/bin/rector process [src/File.php]

Generate CI workflow for GitHub Actions or GitLab CI

vendor/bin/rector setup-ci

Generate a custom rule with boilerplate

vendor/bin/rector custom-rule

说明

Rector performs automated refactoring of PHP codebases, applying rule-based transformations to upgrade syntax, modernize framework usage, and enforce code quality standards. It parses PHP files into an abstract syntax tree and applies configured rules to transform old constructs into their modern equivalents, handling PHP version upgrades (e.g., PHP 7.4 to 8.2) and framework migration (Symfony, Laravel, PHPUnit) automatically. The --dry-run flag previews all changes without modifying files, making it safe to review transformations before applying them. Rules are organized into sets for common upgrade paths, and custom rules can be written for project-specific refactoring patterns. The tool is typically configured through a rector.php file that specifies which rule sets and individual rules to apply.

参数

process
Run refactoring on the specified paths.
init
Create a starter rector.php configuration file.
list-rules
Show all loaded and skipped rules. Accepts --config and --output-format json.
setup-ci
Generate a CI workflow file (GitHub Actions or GitLab CI) for automated Rector runs.
custom-rule
Generate a custom rule with boilerplate structure and PSR-4 autoload paths.
--dry-run
Show changes without applying.
--config _FILE_
Configuration file.
--clear-cache
Clear cache before processing.
--only _RULE_
Run only a single specified rule class.
--output-format _FORMAT_
Output format (e.g., json) for piping results to other tools.
-v, --verbose
Verbose output.
--debug
Print nested exception traces.

FAQ

What is the rector command used for?

Rector performs automated refactoring of PHP codebases, applying rule-based transformations to upgrade syntax, modernize framework usage, and enforce code quality standards. It parses PHP files into an abstract syntax tree and applies configured rules to transform old constructs into their modern equivalents, handling PHP version upgrades (e.g., PHP 7.4 to 8.2) and framework migration (Symfony, Laravel, PHPUnit) automatically. The --dry-run flag previews all changes without modifying files, making it safe to review transformations before applying them. Rules are organized into sets for common upgrade paths, and custom rules can be written for project-specific refactoring patterns. The tool is typically configured through a rector.php file that specifies which rule sets and individual rules to apply.

How do I run a basic rector example?

Run `vendor/bin/rector process [src/]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does process do in rector?

Run refactoring on the specified paths.