Prerequisites
Note
-
The PEAR
package manager is installed on your machine.
-
PHP Code Sniffer
is installed on your machine.
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.
- Open the project settings, and click Code Sniffer under the PHP node.
-
In the PHP Code Sniffer path text box, specify the location of the Code Sniffer executable phpcs.bat.
Type the path manually or click the Browse button
and select the path in the dialog box, that opens.
To check that the specified path to phpcs.bat 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.
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.
- Open the project settings, and click Code Sniffer under the PHP node.
-
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 - 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.
- Open the project settings, and click Inspections.
- On the Inspections page that opens, select the PHP Code Sniffer validation check box under the PHP node.
-
On the right-hand pane of the page, configure the PHP Code Sniffer tool using the controls in the Options area:
- 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.
-
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.
- 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.
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.
- Open the project settings, click Inspections. The Inspections page opens. Select the PHP Code Sniffer validation check box under the PHP node.
- From the Coding standard drop-down list, choose Custom.
-
Click the Browse button
.
-
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
and choose the relevant folder in the dialog that opens.
Note
The selected root directory should contain the ruleset.xml file that points to the rules.
- Put the root directory of your coding standard under the project root.
- Configure Code Sniffer as a PhpStorm inspection.
- Appoint your coding standard.
- At the top of the Inspections page, select the Share Profile check box.
- On the Version Control page of the Settings dialog box, put the root directory of your coding standard under version control.
- To run the inspection, choose on the main menu. Specify the inspection scope and profile.
- 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 PhpStorm internal inspections.
When the amount of messages reported in a file exceeds the limit specified in the Maximum number of messages per file field on the Mess Detector page or waiting for Code Sniffer response exceeds the limit specified in the Tool process timeout field on the same 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
.
-
To remove all the files from the list, click the Clean the list button
.
