LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

rector

Automated PHP code refactoring and upgrades

TLDR

Run rector
$ vendor/bin/rector process [src/]
copy
Dry run
$ vendor/bin/rector process [src/] --dry-run
copy
Use a specific configuration file
$ vendor/bin/rector process [src/] --config [rector.php]
copy
Run only a single rule
$ vendor/bin/rector process [src/] --only [TypedPropertyFromAssignsRector]
copy
Show all loaded and skipped rules
$ vendor/bin/rector list-rules
copy
Initialize a rector.php configuration file
$ vendor/bin/rector init
copy
Clear cache before processing
$ vendor/bin/rector process --clear-cache
copy
Process a single file
$ vendor/bin/rector process [src/File.php]
copy
Generate CI workflow for GitHub Actions or GitLab CI
$ vendor/bin/rector setup-ci
copy
Generate a custom rule with boilerplate
$ vendor/bin/rector custom-rule
copy

SYNOPSIS

rector command [--dry-run] [--config file] [options] [paths]

DESCRIPTION

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.

PARAMETERS

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.

CONFIGURATION

rector.php

Main configuration file defining rule sets, individual rules, paths to process, and skip patterns. Generated by rector init.

CAVEATS

Review dry-run output carefully. Complex code may need manual fixes. Git commit before running.

HISTORY

Rector was created by Tomas Votruba for automated PHP refactoring. It enables safe, large-scale codebase modernization.

SEE ALSO

Copied to clipboard
Kai