Rector cheat sheet

A quick reference for Rector with the most important commands and options. Read it online below, or download the printable PDF.

Download PDF

Installation

composer require rector/rector --dev

Usage

vendor/bin/rector [command] [options] [<paths>...]

Available Commands

Command Description
process (default) Upgrade or refactor source code
list-rules Show a list of all loaded rules
custom-rule Create a skeleton for a custom rule
setup-ci Set up a CI workflow for Rector

Process Command Options

Flag Description
--dry-run Only display the diff of changes without saving them
--no-progress-bar Hide the progress bar
--no-diffs Hide diffs
--only=RULE_CLASS Only run a specific rule
--debug Enable debug verbosity
--help Display help information

Example rector.php Configuration File

<?php

use Rector\Config\RectorConfig;

return RectorConfig::configure()
    ->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
    ->withPhpSets()
    ->withTypeCoverageLevel(10)
    ->withPreparedSets(deadCode: true, codeQuality: true, codingStyle: true)
    ->withComposerBased(symfony: true, doctrine: true, phpunit: true);

Configuration: Paths

Method Description
withPaths($paths) Define which files or directories to refactor
withRootFiles() Include .php files in the root directory
withFileExtensions($extensions) Define file extensions to process (default: php)

Configuration: Progressive Refactoring Levels

Method Description
withPhpLevel($level) Apply PHP rules up to $level
withTypeCoverageLevel($level) Apply type coverage rules up to $level
withTypeCoverageDocblockLevel($level) Apply type coverage docblock rules up to $level
withDeadCodeLevel($level) Apply dead code rules up to $level
withCodeQualityLevel($level) Apply code quality rules up to $level
withCodingStyleLevel($level) Apply coding style rules up to $level

Configuration: Rule Sets

Method Description
withPreparedSets(...) Enable all rules from one or more built-in Rector sets
withPhpSets() Enable PHP rules from the composer.json version
withPhpSets(php80: true) Enable all PHP rules for a specific PHP version
withAttributesSets() Enable rules to migrate annotations to attributes
withComposerBased(...) Enable rules based on installed package versions

Other Configuration Options

Method Description
withSkip($array) Skip certain rules and/or paths
withPhpVersion($version) Specify a PhpVersion constant
withImportNames() Import class names instead of using FQCNs
withFluentCallNewLine() Break fluent calls over multiple lines
withConfiguredRule($rule, $config) Pass configuration to configurable rules