Checking your code with PHP Code Sniffer in PhpStorm 4.0

PhpStorm 4.0 allows you to check your code on the fly with PHP Code Sniffer (phpcs) just by opening a PHP file. The check is triggered every time you change something in the file thus making it extremely easy to get rid of problems reported by PHP Code Sniffer.

Initial Software Requirements

First you need to set up PHP Code Sniffer. You can find more information on site. Make sure you can actually run PHP Code Sniffer from command line: if you type “phpcs --version“, you should get something like this:

PHP_CodeSniffer version 1.3.3 (stable) by Squiz Pty Ltd. (http://www.squiz.net)

Setting Up PHP Code Sniffer Validation Inspection

  • Open Settings and navigate to Inspections|PHP|PHP Code Sniffer validation and check the inspection box.
  • Now select a Code Sniffer path like it is shown below, for example:
  • Click “Validate”. This is necessary to ensure that phpcs can be launched from IDE and will report problems. PhpStorm performs the validation by attempting to run “phpcs --version“. On success it displays obtained phpcs version message and updates a list of coding standards (“Coding standard” combo box):
  • Choose a coding standard you want (passed as a value of “--standard” option) and optionally set the flag “Ignore warnings” (equivalent to “-n” option).
  • Click OK.

Obtaining Validation Results

Now open a PHP file. Once phpcs will be run, you will see a number of warning messages reported by PHP Code Sniffer. For example:

Note that each message is prefixed with “phpcs” to distinguish it from PhpStorm’s own inspections.

Note that validating the file takes some time, so you may not see all the results instantly but in some cases, especially if the file is big, in few seconds. That doesn’t prevent though from editing the file.

Let us know what you think and please submit your feedback (bug reports, improvement suggestions) in YouTrack. This will help us to make phpcs tool integration even better.

This entry was posted in Cool Feature, PhpStorm and tagged , . Bookmark the permalink.