IntelliJ IDEA 2018.3 Help

PHP CS Fixer

In addition to built-in coding assistance, IntelliJ IDEA provides checking the source code through integration with the PHP CS Fixer tool, which detects coding standards problems in your code.

To use PHP CS Fixer from IntelliJ IDEA instead of command line, you need to register it in IntelliJ IDEA and configure it as a IntelliJ IDEA code inspection. Once installed and enabled in IntelliJ IDEA, the tool is available in any opened PHP file, and no additional steps are required to launch it. The on-the-fly code check is activated upon every update in the file thus making it easy to get rid of discovered problems.

Errors and warnings reported by PHP CS Fixer on-the-fly are displayed as pop-up messages. When the tool is run in the batch mode, the errors and warnings are displayed in the Inspection Results tool window. Each message has the PHP CS Fixer prefix to distinguish it from IntelliJ IDEA internal inspections.

You can have predefined rules applied or define your own custom set of rules.

Before you start

Make sure the PHP plugin is enabled. The plugin is not bundled with IntelliJ IDEA, but it can be installed from the JetBrains plugin repository as described in Managing Plugins.

Installing and configuring PHP CS Fixer

PHP CS Fixer scripts can be used as local scripts, the scripts associated with PHP interpreters, or scripts declared as project dependencies and installed via Composer, which is the preferable and recommended way.

Installing PHP CS Fixer with Composer

Before you start, make sure Composer is installed on your machine and initialized in the current project as described in Composer Dependency Manager.

When you install PHP CS Fixer with Composer, IntelliJ IDEA automatically downloads the necessary scripts, registers them in the IDE, and, optionally, enables and configures the corresponding code inspection.

  1. On the context menu of composer.json, choose Composer | Manage Dependencies. Alternatively choose Tools | Composer | Manage Dependencies from the main menu.

  2. In the Manage Composer Dependencies Dialog that opens, select the friendsofphp/php-cs-fixer package from the Available Packages list, possibly using the search field.

    Choose the relevant version from the Version to install list.

  3. If necessary, expand the Settings hidden area and specify the advanced installation options. In the Command line parameters field, type the additional command line parameters. It is recommended to provide the --dev, option: the package in this case is added to the require-dev section of the composer.json file instead of the default require section.

  4. Click Install.

Configuring PHP CS Fixer manually

You can use the manually downloaded local PHP code quality tool scripts or scripts associated with PHP interpreters. There can be a number of local and remote PHP interpreters, the one specified on the PHP page of the Settings/Preferences dialog is considered Project Default. Learn more about configuring PHP interpreters in Configuring Remote PHP Interpreters or in Configuring Local PHP Interpreters.

To choose a PHP CS Fixer script to use

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), navigate to Languages & Frameworks | PHP | Quality Tools.

  2. On the Quality Tools page that opens, expand the PHP CS Fixer area. From the Configuration list,

    choose the PHP CS Fixer script:
    • To use the script associated with a specific remote PHP interpreter, choose the name of this interpreter.

    • To use a local script, choose Local. In this case the local PHP CS Fixer will be executed no matter which PHP interpreter - local or remote - is used in the project. Note that there can be only one Local configuration for PHP CS Fixer because IntelliJ IDEA runs a script (php-cs-fixer.bat for Windows or php-cs-fixer for Linux and macOS) that contains a path to a PHP engine.

    • To use the script associated with the default project interpreter, that is, the one chosen on the PHP page of the Settings dialog, choose By default project interpreter.

To configure a local PHP CS Fixer script

  1. Download and install the PHP CS Fixer scripts.

    To check the PHP CS Fixer installation, switch to the installation directory and run the following command:

    php-cs-fixer --version

    If the tool is available, you will get a message in the following format:

    PHP CS Fixer 2.13.0 Yogi's BBQ by Fabien Potencier and Dariusz Ruminski

    To have code checked against your own custom coding standard, create it. Store the rules and the .php_cs or php_cs.dist file that points to them in the rulesets root directory.

  2. Register the local PHP CS Fixer script in IntelliJ IDEA:
    • In the Settings/Preferences dialog (Ctrl+Alt+S), navigate to Languages & Frameworks | PHP | Quality Tools.

    • On the Quality Tools page that opens, expand the PHP CS Fixer area and click browseButton next to the Configuration list.

    • In the PHP CS Fixer dialog that opens, specify the location of the php-cs-fixer.bat or php-cs-fixer PHP CS Fixer executable in the PHP CS Fixer path field. Type the path manually or click Browse browseButton and select the path in the dialog that opens.

      To check that the specified path to php-cs-fixer.bat or php-cs-fixer ensures interaction between IntelliJ IDEA and PHP CS Fixer, that is, the tool can be launched from IntelliJ IDEA and IntelliJ IDEA will receive problem reports from it, click the Validate button. This validation is equal to running the php-cs-fixer --version command. If validation passes successfully, IntelliJ IDEA displays the information on the detected PHP CS Fixer version.

To configure a PHP CS Fixer script associated with a PHP interpreter

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), navigate to Languages & Frameworks | PHP | Quality Tools.

  2. On the Quality Tools page that opens, expand the PHP CS Fixer area and click browseButton next to the Configuration list. The PHP CS Fixer dialog opens showing the list of all the configured PHP CS Fixer scripts in the left-hand pane, one of them is of the type Local and others are named after the PHP interpreters with which the scripts are associated. Click icons general add svg on the toolbar.

    Note that currently IntelliJ IDEA only supports PHP CS Fixer version 2.8.0 and later.

  3. In the PHP CS Fixer by Remote Interpreter dialog that opens, choose the remote PHP interpreter to use the associated script from. If the list does not contain a relevant interpreter, click browseButton and configure a remote interpreter in the CLI Interpreters dialog as described in Configuring Remote PHP Interpreters.

    When you click OK, IntelliJ IDEA brings you back to the PHP CS Fixer dialog where the new PHP CS Fixer configuration is added to the list and the right-hand pane shows the chosen remote PHP interpreter, the path to the PHP CS Fixer associated with it, and the advanced PHP CS Fixer options.

Configuring advanced PHP CS Fixer options

IntelliJ IDEA lets you specify advanced PHP CS Fixer options and thus fine-tune the PHP CS Fixer process behavior depending on the configuration of your computer and the rule sets used.

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), navigate to Languages & Frameworks | PHP | Quality Tools.

  2. On the Quality Tools page that opens, expand the PHP CS Fixer area. In the Maximum number of messages per file field set the upper limit for the total number of messages to be reported for a file. All the messages above this limit will be rejected and IntelliJ IDEA will display the warning message at top of the editor tab or in the Inspection Results Tool Window after the inspection is performed.

  3. In the Tool process timeout field, specify how long you want IntelliJ IDEA to wait for a result from PHP CS Fixer, whereupon the process is killed to prevent excessive CPU and memory usage.

Configuring PHP CS Fixer as a IntelliJ IDEA inspection

To configure the PHP CS Fixer inspection manually

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), click Inspections under Editor.

  2. On the Inspections page that opens, expand the PHP | Quality tools node and select the checkbox next to PHP CS Fixer validation.

  3. On the right-hand pane of the page, configure the PHP CS Fixer tool using the controls in the Options area:

    1. From the Severity list, choose the severity degree for the PHP CS Fixer inspection. The selected value determines how serious the detected discrepancies will be treated by IntelliJ IDEA and presented in the inspection results.

    2. From the Scope list, choose the scope to limit the inspection application to.

    3. To allow risky rules, that is, the rules that can change code behavior, select the Allow risky rules checkbox. By default, risky rules are not allowed.

    4. Appoint the coding standard to apply.

      • To use one of the predefined coding standards, select it the Coding standard list, appoint the coding style to check your code against. The list contains all the coding standards declared inside the main php-cs-fixer directory structure.

      • To have your code checked against your own previously defined coding standard, choose Custom. Click Browse browseButton.png and in the Custom Coding Standard dialog box that opens, specify the path to the .php_cs or .php_cs.dist file for your own coding standard in the Path to ruleset field. Type the path manually or click Browse browseButton.png and choose the relevant folder in the dialog that opens.

Configuring the PHP CS Fixer inspection with Composer

You can include information on the default and custom PHP CS Fixer rulesets inside the scripts section of composer.json. When you install or update project dependencies, the specified rulesets will be detected and the PHP CS Fixer validation inspection will be enabled automatically.

If no ruleset is specified in the scripts section of composer.json, IntelliJ IDEA will additionally check the project root to locate the ruleset with the .php_cs or .php_cs.dist default name. If the file is present, it will be automatically selected as the inspection's Custom ruleset.

To configure the PHP CS Fixer inspection with Composer

  • In the scripts section of composer.json, add the php-cs-fixer PHP CS Fixer launch command into one of the leaf elements.

    Provide the --rules argument to denote the coding standard used. For example, adding the following record will set the coding standard to PSR1:

    "scripts": { "php-cs-fixer": "php-cs-fixer --rules=@PSR1" }

    Alternatively, provide the --config argument and the path to the ruleset file, which will set the coding standard to Custom:

    "scripts": { "php-cs-fixer": "php-cs-fixer --config=./.php_cs" }

Sharing a custom coding style with the team

  1. Put the root directory of your coding standard under the project root.

  2. Configure PHP CS Fixer as a IntelliJ IDEA inspection.

  3. Appoint your coding standard.

  4. Make sure that a project profile is selected at the top of the Inspections page. Such profiles are saved in a particular project's .idea directory (for example, $PROJECT_DIR/.idea/inspectionProfiles). See Customizing Profiles for details.

  5. On the Version Control page of the Settings/Preferences dialog, make sure that the .idea directory is put under version control.

Running PHP CS Fixer in the batch mode

  1. Run the inspection, by choosing Code | Inspect code on the main menu. Specify the inspection scope and profile.

  2. View the inspection results in the Inspection Results Tool Window. Errors and warnings reported by PHP CS Fixer are prefixed with PHP CS Fixer to distinguish them from IntelliJ IDEA internal inspections.

Excluding files from PHP CS Fixer inspection

When waiting for PHP CS Fixer response exceeds the limit specified in the Tool process timeout field in the PHP CS Fixer dialog, IntelliJ IDEA suggests adding the file to the ignore list.

Manage ignored files

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), navigate to Languages & Frameworks | PHP | Quality Tools.

  2. On the Quality Tools page that opens, expand the PHP CS Fixer area and click the Show ignored files link.

    • To add a file, click icons general add svg and locate the desired file in the dialog that opens.

    • To delete a file from the list and have PHP CS Fixer process it again, select the file and click the Remove file buttonicons general remove svg.

    • To remove all the files from the list, click the Clean the list buttonicons actions close svg.

Last modified: 14 December 2018

See Also