Rector
IntelliJ IDEA supports PHP code upgrade and refactoring functionality based on the Rector plugin.
Rector provides code transformation rules for automated refactoring and upgrade of applications to a newer framework or PHP version. With IntelliJ IDEA, you can run Rector rules against a selected folder or file right from the Project tool window, compare the source and modified code in the diff viewer, and pick which changes to apply.
Rector requires PHP version 7.2 and later and uses the local or remote PHP interpreter set for the project. Make sure the PHP interpreter is configured in IntelliJ IDEA on the PHP page, as described in Configuring Local PHP Interpreters and Configuring Remote PHP Interpreters.
For Docker Compose-based remote interpreters, make sure to use docker-compose exec
mode to avoid spawning additional containers.
tip
Skip this step if you already have Rector installed and the rector.php file configured in your project.
tip
Before you start, make sure Composer is installed on your machine and initialized in the current project as described in Composer dependency manager.
Inside composer.json, add the
rector/rector
dependency record to therequire
orrequire-dev
section. Press CtrlSpace to get code completion for the package name and version."require-dev": { "rector/rector": "^0.15.13" },
Do one of the following:
Click the Install shortcut link on top of the editor panel.
If the Non-installed Composer packages inspection is enabled, IntelliJ IDEA will highlight the declared dependencies that are not currently installed. Press AltEnter and select whether you want to install a specific dependency or all dependencies at once.
For more information about installing Rector, refer to Rector documentation.
Add a rector.php template file to the project root by either of the following:
Right-click the root folder in the Project tool window and select New |
rector.php File from the context menu.
IntelliJ IDEA invokes a rector init action, which creates a rector.php file in the same way as Rector's
vendor/bin/rector init
command executed from the command line would do.Search and apply the rector init action from the search window (CtrlShift0A).
Run the
rector init
command from the Terminal tool window.vendor/bin/rector init
Edit the generated rector.php file to specify which Rector rule or rule sets you need. Press CtrlSpace to get code completion suggestions.
You can run Rector on a folder, a file, a selection of multiple folders or files, or from within the rector.php config file. IntelliJ IDEA creates a default run configuration, runs Rector, and outputs the list of files that have suggested changes in them.
As with any other run/debug configuration in IntelliJ IDEA, you can save as permanent, share, create from a template, group in folders, or save for later use a run configuration for Rector.
In the Project tool window, select the file or folder to run Rector on and choose Run | <File or folder name> (Rector) from the context menu of the selection:
IntelliJ IDEA generates a default run configuration and runs the
vendor/bin/rector process --dry-run
command on the scope specified in the configuration.
Open rector.php in the editor.
Click
in the gutter and select Run '<project name>' (Rector) from the context menu. Alternatively, press CtrlShiftF10 from anywhere in the file.
If you have
$rectorConfig->paths
defined in your rector.php file, then Rector runs on the specified scope of folders. Otherwise, the run configuration path is automatically set to the src/ folder in the project root.
You can create a custom run configuration or edit an existing default one—for example, to pass additional arguments to the vendor/bin/rector process
command.
In the Project tool window, select the file or folder and choose More Run/Debug | Modify Run Configuration | <File or folder name> (Rector) from the context menu.
Alternatively, choose Run | Edit Configurations from the main menu, then click
and choose Rector from the list.
In the Rector configuration dialog that opens, edit the fields as required.
IntelliJ IDEA shows Rector execution results—that is, a list of files with suggested changes—in the Changes tab of the Run tool window. The Console tab contains the same execution output, but in the JSON format.

In the Changes tab, click a file from the list to open it and preview the suggested changes in the diff viewer.
To apply the changes, select checkboxes for the relevant files in the Run tool window and click Apply.
Thanks for your feedback!