IntelliJ IDEA 14.1.0 Help

Using PHP Code Sniffer Tool

In addition to built-in coding assistance, IntelliJ IDEA provides code style check through integration with the PHP Code Sniffer tool, which validates your code for consistence with a coding standard of your choice. You can appoint one of the predefined coding standards or use your own previously defined coding standard with the root directory outside the default PHP Code Sniffer’s Standards directory. Moreover, you can share your custom coding style with your team fellows.

To use PHP Code Sniffer right from IntelliJ IDEA instead of from a 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, so no steps are required from your side to launch it. The on-the-fly code check is activated upon every update in the file thus making it extremely easy to get rid of problems reported by PHP Code Sniffer.

Errors and warnings reported by PHP Code Sniffer 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 tool window. Anyway, each message has the phpcs prefix to distinguish it from IntelliJ IDEA internal inspections.

On this page:

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 Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins. Once enabled, the plugin is available at the IDE level, that is, you can use it in all your IntelliJ IDEA projects.

Choosing the Code Sniffer Script to Use

You can use PHP Code Sniffer local 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 dialog box is considered Project Default. Learn more about configuring PHP interpreters in Configuring Remote PHP Interpreters or in Configuring Local PHP Interpreters.

  1. Open the Settings dialog box by choosing File | Settings and expand the Languages & Frameworks node. Then click Code Sniffer under PHP.
  2. From the Configuration drop-down list in the Development Environment area, choose the script to use:
    • To use the script associated with a specific remote PHP interpreter, choose the name of this interpreter.
    • To use the script associated with the default project interpreter, that is, the one chosen on the PHP page of the Settings dialog box, choose By default project interpreter.
    • To use a local script, choose Local. In this case the local Code Sniffer 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 Code Sniffer because IntelliJ IDEA runs a script (phpcs.bat for Windows or phpcs for Linux) which contains a path to a PHP engine.

Configuring a Local Code Sniffer Script

  1. Make sure the PEAR package manager is installed on your machine.
  2. Download and install the PHP Code Sniffer. To check it, switch to the directory with the pear.bat file and run the following command: phpcs --version

    If the tool is available, you will get a message in the following format: PHP_CodeSniffer version <version> (stable) by Squiz Pty Ltd. (http://www.squiz.net)

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

  3. Register the local PHP Code Sniffer script in IntelliJ IDEA:
    1. Open the Settings dialog box by choosing File | Settings and expand the Languages & Frameworks node. Then click Code Sniffer under PHP.
    2. On the Code Sniffer page that opens, click browseButton next to the Configuration drop-down list.
    3. In the Code Sniffer dialog box that opens, specify the location of the Code Sniffer executable phpcs.bat or phpcs in the PHP Code Sniffer path text box. Type the path manually or click the Browse button browseButton.png and select the path in the dialog box, that opens.

      To check that the specified path to phpcs.bat or phpcs ensures interaction between IntelliJ IDEA and Code Sniffer, 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 phpcs --version command. If validation passes successfully, IntelliJ IDEA displays the information on the detected Code Sniffer version.

Configuring a Code Sniffer Associated with a PHP Interpreter

  1. Open the Settings dialog box by choosing File | Settings and expand the Languages & Frameworks node. Then click Code Sniffer under PHP.
  2. On the Code Sniffer page that opens, click browseButton next to the Configuration drop-down list. The Code Sniffer dialog box opens showing a list of all the configured Code Sniffer 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 add on the toolbar.
  3. In the Code Sniffer by Remote Interpreter dialog box 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 Interpreters dialog box as described in Configuring Remote PHP Interpreters.

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

Configuring Advanced PHP Code Sniffer Options

IntelliJ IDEA provides the ability to specify advanced PHP Code Sniffer options and thus fine tune the PHP Code Sniffer process behavior depending on the configuration of your computer and the rule sets used.

  1. Open the Settings dialog box by choosing File | Settings and expand the Languages & Frameworks node. Then click Code Sniffer under PHP.
  2. In the Maximum number of messages per file text box, 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 following warning right in the code:
    Too many PHP Code Sniffer messages
  3. In the Tool process timeout text box, specify how long you want IntelliJ IDEA to wait for a result from PHP Code Sniffer, whereupon the process is killed to prevent excessive CPU and memory usage.

Configuring PHP Code Sniffer as a IntelliJ IDEA inspection

  1. Open the Settings dialog box by choosing File | Settings or pressing Ctrl+Alt+S and click Inspections under Editor.
  2. On the Inspections page that opens, select the PHP Code Sniffer validation check box under the PHP node.
  3. On the right-hand pane of the page, configure the PHP Code Sniffer tool using the controls in the Options area:
    1. From the Severity drop-down list, choose the severity degree for the Code Sniffer inspection. The selected value determines how serious the detected discrepancies will be treated by IntelliJ IDEA and presented in the inspection results.
    2. In the Coding standard drop-down list, appoint the coding style to check your code against. The list contains all the coding standards installed inside the main PHP_CodeSniffer directory structure.

      Use one of the predefined coding standards or choose Custom to appoint your own standard.

    3. Optionally, select the Ignore warnings check box to have only errors reported and suppress reporting warnings. This option is equal to the -n command line argument.

Choosing a Custom Coding Style to Check Your Code Against

You can have code checked against your own previously defined coding standard with the root directory outside the default PHP Code Sniffer’s Standards directory. This root directory should contain the rules themselves and the ruleset.xml file that points to them.

  1. Open the Settings dialog box, and click Inspections. The Inspections page opens. Select the PHP Code Sniffer validation check box under the PHP node.
  2. From the Coding standard drop-down list, choose Custom.
  3. Click the Browse button browseButton.png.
  4. In the Custom Coding Standard dialog box that opens, specify the path to the root directory of your own coding standard in the Root directory. Type the path manually or click the Browse button browseButton.png and choose the relevant folder in the dialog that opens.

    The selected root directory should contain the ruleset.xml file that points to the rules.

Sharing a Custom Coding Style with the Team

  1. Put the root directory of your coding standard under the project root.
  2. Configure Code Sniffer as a IntelliJ IDEA inspection.
  3. Appoint your coding standard.
  4. At the top of the Inspections page, select the Share Profile check box.
  5. On the Version Control page of the Settings dialog box, put the root directory of your coding standard under version control.

Running Code Sniffer in the Batch Mode

  1. To run the inspection, choose Code | Inspect code on the main menu. Specify the inspection scope and profile.
  2. View the inspection results in the Inspection tool window. Errors and warnings reported by PHP Code Sniffer have the phpcs prefix to distinguish them from IntelliJ IDEA internal inspections.

Excluding Files from Code Sniffer Inspection

When waiting for Code Sniffer response exceeds the limit specified in the Tool process timeout field on Code Sniffer page, IntelliJ IDEA suggests adding the file to the ignore list. This list is shown on the Code Sniffer page in the Ignored files area. For each file, IntelliJ IDEA displays its name and location.

  • To delete a file from the list and have Code Sniffer process it again, select the file and click the Remove file button delete.png.
  • To remove all the files from the list, click the Clean the list button close.png.

See Also

Last modified: 14 July 2015