Advanced PHP Code Sniffer Options in PhpStorm 5.0

New version allows to specify your own coding standard with a root directory outside default PHP Code Sniffer’s Standards directory. In addition it provides a better control on PhpStorm integration with the tool to optimize its use in a particular environment.

Specfying Your Own Coding Standard

If you have created your own coding standard according to this tutorial, you can specify it in PHP Code Sniffer validation inspection settings. Choose Settings|Inspections|PHP|PHP Code Sniffer validation to get the following options panel:

Select “Custom” in coding standard combo box and click […] button next to the field. You will get “Custom Coding Standard” dialog which allows you to give a root directory of your coding standard. The directory must contain a ruleset.xml file.

Since there is no guarantee that ruleset.xml is valid or points to valid rules, you may start getting error notifications from PHP Code Sniffer every time you open or edit a PHP file. Here is an example:

If this is the case, you have to check your rules and make sure they work when used directly with phpcs command line tool.

A note on sharing a ruleset with the team via project (VCS)

If you would like to share the ruleset with your team, you can put it under project (and VCS) root and specify your coding standard root in inspection settings above. But in order to share these settings, make sure that “Share profile” flag of the selected profile is on like in this example:

Running PHP Code Sniffer in Batch Mode

In version 5.0 PHP Code Sniffer inspection can be run in batch mode just like any other inspection. If you choose Code|Inspect code, you may get something like this:

Note the results from PHP Code Sniffer validation.

Advanced Tool Options

First of all PHP Code Sniffer tool settings are moved to a separate panel under PHP settings to distinguish them from inspection parameters which can be shared via project. Tool settings are always local (PC-specific). Currently you may find the following if you choose Settings|PHP|Code Sniffer:

PHP Code Sniffer path is the same as it used to be and “Validate” button is there too. But there are two new options:

  • Maximum number of messages per file: limits the total number of messages reported to the given number (14 in the example above), all other messages are rejected and a special warning “Too many PHP Code Sniffer messages” is shown in the code.
  • Tool process timeout: allows to control maximum time in which a result from PHP Code Sniffer should be expected. If this time is exceeded, the process is killed to prevent excess CPU and memory usage. Compared to the previous solution where the timeout was hard-coded, this gives you a possibility to fine tune PHP Code Sniffer process behavior depending on PC configuration and specific rule sets.

More Information and Your Feedback

If you are reading about PHP Code Sniffer integration in PhpStorm for the first time, I recommend to take a look at the previous post here. If you give the feature a try, please don’t forget that your feedback is extremely important for us. Don’t hesitate to submit any kind of findings and/or new ideas in YouTrack. And thank you for using our IDE!

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

    I would say… quite exciting :)

    Next on my wishlist would be exclude / run only specific sniffs.

    Great feature.

  • OZ

    “It’s not a bug, it’s feature”
    It’s first time in my experience, when uncaught exception is presented as normal behavior :)

    Please, tell me somebody, what new gives Code Sniffer in compare with PhpStorm “Reformat code”?

  • Nicole Cordes

    It would be great if you could add another input box to display the path of the chosen custom rules.

  • FG

    How do I have to set my phpcs path when i’m on a windows machine and installed phpcs on a linux server? I have connection to that linux machine by a mounted drive.

    • rustam.vishnyakov

      You can not do this. Php Code Sniffer must be installed on the same PC where you run PhpStorm. PhpStorm invokes it as an external process (i.e. Windows process on Windows machine). Having phpcs on a mounted drive will not help because you can run it only from Linux environment.