PhpStorm 2018.1 Help

PHP Code Sniffer

In this section:

Introduction

In addition to built-in coding assistance, PhpStorm 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 PhpStorm instead of from a command line, you need to register it in PhpStorm and configure it as a PhpStorm code inspection. Once installed and enabled in PhpStorm, 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 Results tool window. Anyway, each message has the phpcs prefix to distinguish it from PhpStorm internal inspections.

Before you start

Make sure the PHP plugin is enabled. The plugin is activated by default. If the plugin is disabled, enable it on the Plugins settings page as described in Enabling and Disabling Plugins.

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 / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or PhpStorm | Preferences for macOS. Expand the Languages & Frameworks node, and 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 PhpStorm 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 PhpStorm:
    1. Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or PhpStorm | Preferences for macOS. Expand the Languages & Frameworks node, and 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 Browse 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 PhpStorm and Code Sniffer, that is, the tool can be launched from PhpStorm and PhpStorm will receive problem reports from it, click the Validate button. This validation is equal to running the phpcs --version command. If validation passes successfully, PhpStorm displays the information on the detected Code Sniffer version.

Configuring a Code Sniffer associated with a PHP interpreter

  1. Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or PhpStorm | Preferences for macOS. Expand the Languages & Frameworks node, and 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 new 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 CLI Interpreters dialog box as described in Configuring Remote PHP Interpreters.

    When you click OK, PhpStorm 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

PhpStorm 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 / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or PhpStorm | Preferences for macOS. Expand the Languages & Frameworks node, and 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 PhpStorm 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 PhpStorm 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 PhpStorm inspection

  1. Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or PhpStorm | Preferences for macOS, and click Inspections under Editor.
  2. On the Inspections page that opens, select the PHP Code Sniffer validation checkbox 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 PhpStorm 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 checkbox 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 checkbox under the PHP node.
  2. From the Coding standard drop-down list, choose Custom.
  3. Click Browse 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 Browse 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 PhpStorm inspection.
  3. Appoint your coding standard.
  4. At the top of the Inspections page, select the Share Profile checkbox.
  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 Results Tool Window. Errors and warnings reported by PHP Code Sniffer have the phpcs prefix to distinguish them from PhpStorm 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, PhpStorm 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, PhpStorm 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.
Last modified: 27 July 2018

See Also