Let CodeSniffer inspect your code in PhpStorm

Some time ago I wrote an article on how to format your code automatically in PhpStorm to be conform the PSR standards. In this article I want to show you how you can enable inspections in PhpStorm which throws a warning if you have code which is not PSR, e.g.

  • Whitespace at the end of line
  • No newline at the end of file
  • No newline between namespace and use statements
  • etc…

This is pretty easy. We will use squizlabs/PHP_CodeSniffer from Github.

I’m doing this on an Ubuntu machine, but it should work the same in OS X (no idea about Windows).

Open your terminal and download phpcs, make it executable and move it somewhere.

curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
chmod a+x phpcs.phar
sudo mv phpcs.phar /usr/local/bin/phpcs

Now if you type in phpcs --version you should get an output like this

PHP_CodeSniffer version 2.7.0 (stable) by Squiz (http://www.squiz.net)

Ok, we installed the CodeSniffer, now we need to enable it in PhpStorm and activate the inspection profile. Open your settings in PhpStorm and navigate to Language & Frameworks -> PHP -> Code Sniffer (or search for „Code Sniffer“). You should now see a window wher your development environment is selected (in my case it’s Local). Click on the ellipsis next to your configuration and a new window should pop up. In this window you have to enter the path where you moved phpcs previously. If you did it like me it should be /usr/local/bin/phpcs. Press on validate and it should look like this

bildschirmfoto-vom-2016-09-14-14-51-48

Press Apply & Ok and go to Editor -> Inspections. If you haven’t searched for Code Sniffer do it now and activate PHP Code Sniffer validation. On the bottom right of the window you should see a dropdown with a label „Coding standard“. There are arrows next to the dropdown. Press these and wait for the coding standards to be loaded. Afterwards select one, PSR2 for example. It should look like this

bildschirmfoto-vom-2016-09-14-14-54-13

Press Apply & Ok and you are set to go. If you now have a wrongly formatted file PhpStorm will tell you that there’s something wrong.

It's only fair to share...Share on FacebookTweet about this on TwitterShare on RedditEmail this to someoneShare on Tumblr

Leave a Comment

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.