PHP Code Sniffer
PhpStorm provides code style check through integration with the PHP Code Sniffer tool, which validates your code for consistency 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.
To use PHP Code Sniffer from PhpStorm instead of 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, 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 Code Sniffer on-the-fly are displayed as popup 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
phpcs prefix to distinguish it from PhpStorm internal inspections. PhpStorm also integrates with the PHP Code Beautifier and Fixer tool, which lets you fix many of the detected issues.
Before you start
Installing and configuring PHP Code Sniffer
PHP Code Sniffer 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 Code Sniffer 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 Code Sniffer with Composer, PhpStorm automatically downloads the necessary scripts, registers them in the IDE, and, optionally, enables and configures the corresponding code inspection.
From the context menu of composer.json, choose . Alternatively choose from the main menu.
In the Manage Composer Dependencies Dialog that opens, select the squizlabs/php_codesniffer package from the Available Packages list, possibly using the search field.
Configuring PHP Code Sniffer 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.
Choose a PHP Code Sniffer script to use
On the Quality Tools page that opens, expand the Code Sniffer area. From the Configuration list,
choose the PHP Code Sniffer 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 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 PHP Code Sniffer because PhpStorm runs a script (phpcs.bat for Windows or phpcs 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/Preferences dialog, choose By default project interpreter.
Configure a local PHP Code Sniffer script
Download and install the PHP Code Sniffer scripts.
To check the PHP Code Sniffer installation, switch to the installation directory 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. (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.
Register the local PHP Code Sniffer script in PhpStorm:
On the Quality Tools page that opens, expand the Code Sniffer area and click next to the Configuration list.
In the PHP Code Sniffer dialog that opens, specify the location of the phpcs.bat or phpcs PHP Code Sniffer executable in the PHP Code Sniffer path field. Type the path manually or click and select the relevant folder in the dialog that opens.
To check that the specified path to phpcs.bat or phpcs ensures interaction between PhpStorm and PHP 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 --versioncommand. If validation passes successfully, PhpStorm displays the information on the detected PHP Code Sniffer version.
Configure a PHP Code Sniffer script associated with a PHP interpreter
On the Quality Tools page that opens, expand the Code Sniffer area and click next to the Configuration list. The PHP Code Sniffer dialog opens showing the list of all the configured PHP 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 on the toolbar.
In the PHP Code Sniffer 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 and configure a remote interpreter in the CLI Interpreters dialog as described in Configuring Remote PHP Interpreters.
When you click OK, PhpStorm brings you back to the PHP Code Sniffer dialog where the new PHP Code Sniffer configuration is added to the list and the right-hand pane shows the chosen remote PHP interpreter, the path to the PHP Code Sniffer associated with it, and the advanced PHP Code Sniffer options.
Configuring advanced PHP Code Sniffer options
PhpStorm lets you 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.
On the Quality Tools page that opens, expand the Code Sniffer 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 PhpStorm will display the warning message at top of the editor tab or in the Inspection Results Tool Window after the inspection is performed.
In the Tool process timeout field, 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.
If necessary, in the PHP Code Beautifier and Fixer Settings area provide the path to the PHP Code Beautifier and Fixer tool in the Path to phpcbf field. This will let you automatically fix many of the errors detected by PHP Code Sniffer. If you install PHP Code Sniffer with Composer, PHP Code Beautifier and Fixer will be detected and set up automatically.
Configuring PHP Code Sniffer as a PhpStorm inspection
Configure the PHP Code Sniffer inspection manually
On the Inspections page that opens, expand the node and select the checkbox next to PHP Code Sniffer validation.
On the right-hand pane of the page, configure the PHP Code Sniffer tool using the controls in the Options area:
From the Severity list, choose the severity degree for the PHP Code Sniffer inspection. The selected value determines how serious the detected discrepancies will be treated by PhpStorm and presented in the inspection results.
From the Scope list, choose the scope to limit the inspection application to.
In the Check files with extensions field, provide the comma-separated list of file extensions that should be checked by PHP Code Sniffer.
To have PHP Code Sniffer report warnings in addition to errors, select the Show warnings as... checkbox and choose the severity degree from the list. To have only errors reported and suppress reporting warnings, clear Show warnings as... checkbox.
If you are relying on a custom third-party coding standard (for example, Coder or Joomla Coding Standards ), you need to integrate it with PHP Code Sniffer prior to using it. To do this, select the Installed standard paths checkbox, click , and select the custom standard installation directory in the dialog that opens.
Select the Show sniff name checkbox to have the corresponding sniff's name displayed in the editor or the inspection results in addition to the inspection summary.
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 installed inside the main php_codesniffer directory structure.
To have your code checked against your own previously defined coding standard, choose Custom. Click and in the Custom Coding Standard dialog that opens, specify the path to the ruleset.xml file for your own coding standard in the Path to ruleset field. Type the path manually or click and choose the relevant folder in the dialog that opens.
Configuring the PHP Code Sniffer inspection with Composer
You can include information on the default and custom PHP Code Sniffer rulesets inside the scripts section of composer.json. When you install or update project dependencies, the specified rulesets will be detected and the PHP Code Sniffer validation inspection will be enabled automatically.
If no ruleset is specified in the
scripts section of composer.json, PhpStorm will additionally check the project root to locate the ruleset with the phpcs.xml default name. If the file is present, it will be automatically selected as the inspection's Custom ruleset.
Configure the PHP Code Sniffer inspection with Composer
scripts section of composer.json, add the
phpcs PHP Code Sniffer launch command into one of the leaf elements.
--standard argument to denote the coding standard used. For example, adding the following record will set the coding standard to PSR2:
Adding the following record will set the coding standard to Custom and the path to the ruleset to <project root>/phpcs.xml:
Additionally, you can provide a custom non-PSR standard dependency inside the
require-dev section of composer.json to have it detected automatically. Currently, the following standards are supported:
Fixing issues detected by PHP Code Sniffer
PhpStorm integrates with the PHP Code Beautifier and Fixer tool, which lets you fix many of the detected issues. If you install PHP Code Sniffer with Composer, PHP Code Beautifier and Fixer will be detected and set up automatically. Otherwise, you need to set it up manually.
Fix issues by using a quickfix
Upon detecting an issue, PhpStorm highlights it in accordance with the PHP Code Sniffer inspection settings.
Place the caret at the detected issue and click , or press Alt+Enter.
Select PHP Code Beautifier and Fixer: fix the whole file from the popup menu. Note that this will fix all issues detected in the current file.
Fix issues automatically with Code Cleanup
From the main menu, select Code | Code Cleanup.
In the Specify Code Cleanup Scope dialog that opens, select ths scope to which you want the inspection profile to be applied.
Select the inspection profile from the list, or click to configure a new profile in the Code Cleanup Inspections dialog that opens. You can also click to check, which fixes will be applied and make sure that the PHP Code Sniffer validation inspection is enabled.
Click OK to launch code cleanup.
Running PHP Code Sniffer in the batch mode
Excluding files from PHP Code Sniffer inspection
When waiting for PHP Code Sniffer response exceeds the limit specified in the Tool process timeout field in the PHP Code Sniffer dialog, PhpStorm suggests adding the file to the ignore list.
Manage ignored files
On the Quality Tools page that opens, expand the Code Sniffer area and click the Show ignored files link.
To add a file, click and locate the desired file in the dialog that opens.
To delete a file from the list and have PHP Code Sniffer process it again, select the file and click .
To remove all the files from the list, click the Clean the list button .