rector
Automated code refactoring and upgrades
TLDR
Process a specific directory
Process a directory without applying changes (dry run)
Process a directory and apply coding standards
Display a list of available levels
Process a directory with a specific level
SYNOPSIS
rector [options]
PARAMETERS
--config
Path to the rector.php configuration file. If not specified, Rector will look for it in the current directory and its parent directories.
--dry-run
Perform a dry run, showing the changes without applying them.
--autoload-file
Path to the autoload file.
--clear-cache
Clears the cache directory.
--debug
Enable debug mode for more verbose output.
--set
Run a specific set of rules (e.g., php74, symfony5).
--level
Run a specific rule level (e.g., LevelSetList::UP_TO_PHP_81).
--no-progress-bar
Disable the progress bar during execution.
--no-diffs
Hides diffs output.
--output-format
Set the output format (e.g., json, pretty).
--parallel
Run Rector in parallel, significantly speeding up processing on large codebases.
--memory-limit
Set PHP memory limit.
DESCRIPTION
Rector is a command-line tool for automated code refactoring, instant upgrades, and overall code modernization. It's designed to make large-scale code changes quick and efficient, reducing the need for manual search and replace operations. Rector parses your code, understands its structure, and applies predefined or custom rules to automatically modify it. It supports various frameworks and technologies, including PHP, and provides a flexible system for creating custom refactoring rules. This allows developers to easily improve code quality, upgrade to newer versions of libraries and frameworks, and enforce coding standards across a large codebase. Rector focuses on deterministic transformations, ensuring that the changes are predictable and reliable, while also providing an option to review changes before applying them.
CAVEATS
Rector's effectiveness depends heavily on the quality and completeness of its rule set. Complex or custom code might require custom rules to be written. Always run Rector with `--dry-run` and carefully review the changes before applying them. Version updates may introduce backward compatibility breaks in the rules.
CONFIGURATION
Rector is configured using a `rector.php` file in the root of your project. This file defines the rules to be applied, the paths to be analyzed, and other options. It's a PHP file that allows you to configure Rector programmatically.
Example:
```php
paths([
__DIR__ . '/src',
]);
$rectorConfig->sets([
SetList::PHP_82,
]);
};
```
RULE SETS
Rector provides predefined rule sets for various tasks, such as upgrading to newer PHP versions, applying specific coding standards, and refactoring code for improved maintainability. These rule sets are a convenient way to quickly apply a set of related refactorings.
Example:
```bash
rector --set php74
```
HISTORY
Rector was originally developed as a PHP-focused tool to automate refactoring tasks that were previously tedious and error-prone. Its initial goal was to simplify upgrades and modernize legacy codebases. Over time, it has evolved into a more general-purpose code transformation tool, supporting a wider range of use cases and providing a flexible API for creating custom rules. The development is still very active, with frequent releases and new rules being added continuously. The usage has steadily increased as more developers recognize the benefits of automated refactoring for improving code quality and reducing technical debt.
SEE ALSO
codesniffer(1), phpstan(1)