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:

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.

Is it possible to add own coding standards? I cannot see a form field for this case.

rustam.vishnyakov

Christian,
currently PhpStorm supports only the standards installed inside the main PHP_CodeSniffer directory structure.

Mark

If you have your own coding standard you can copy it to PHP/CodeSniffer/Standards or create a symlink there pointing to your standard. You must do this before opening preferences. Then you will see it in the list.

Johannes Stickel

Sounds like a great feature. I’ll try this out asap. Keep up the awesome work!

Arvids

Do you plan on extending the formatting options of the PHPStorm?

I have more that a few things that PHPStorm just can’t get right to align, because there are no ability to specify conditional alignment.

For example:
If function call has an array argument and the line exceeds the right margin, PHPStorm can wrap it, and it wraps with multiple array elements on the line. But I for example want that to be reformatted differently – if wrapping occurs, it should align each argument on it’s own line. If it wraps an array and array exceeds 3-4 arguments (in other words it’s a long line) – it should align the array elements on their own lines.

I can make a feature request with screenshots of how PHPStorm does it now and how I would see that code, but it’s quite a task to make that (will take me a few days). I don’t wana do that if that will be just rejected.

rustam.vishnyakov

Arvids,
we keep improving PhpStorm’s formatting capabilities. You are definitely welcome to make a feature request for the issue you have described and for any other issues as well.

http://kurapov.name Artjom Kurapov

I see that now code assist adds PHPDoc @return void annotation, while it was missing previously. Is it intentional? I’d understand @return null if return was called.. but whats the point of void? It just adds lots of annotations that are not needed. Is there a way of disabling it, while keeping normal @return annotations working?

http://kurapov.name Artjom Kurapov

Also, I’ve noticed that editing a file over 4k lines becomes impossible, since php->phpcs gets called (on every change?) that skyrockets CPU load. Is there a way to add this kind of check to pre-commit stage?

http://kurapov.name Artjom Kurapov

Yep, seems that it spawns php proccesses on every change.. so I added a new annotation @property line and it made 10 processes to analyze this file.. With 90MB each. I mean you could have at least scheduled it to be triggered at most every 30 sec. or someting..

Am I getting this right: I need to install the PEAR package before I can use this?
If so: I was one of the first to know about and try phpstorm. I loved it to bits, but slowly it is starting to make the same type of wrong implementations as all the other IDE’s like Eclipse, netbeans, Aptana, etc.

What was different about phpstorm was the fact that it focussed on the core of web development: html, css, javascript and php. later adding htaccess.
This core however is far from finished, it is still riddled with incomplete or lacking features and usability, and yet you are adding plugin upon plugin that only apply to a part of your userbase, leaving out everybody in general satisfaction.

The idea of code-sniffing is great, but it should be independent of 3rd party software.

Andriy Bazanov

Yeah .. right — PhpStorm should have it’s own PHP implementation (so no PHP is required to execute your scripts); debugger (so no xdebug/zend debug dependency); own version of phpunit (for unit testing); phing; own less/sass/scss/etc compiler implementation etc etc — so no “external dependencies” .. because someone may need extra click to do …

Instead of integrating existing widely used (and not so much, but still well know) tools devs need to spend tons of their time to code their own version of existing software instead of just providing integration (which still takes quite some time to implement and polish).

Do you even realise that different devs working on different areas which very often do not intersect with what another dev do in any way?

And why it has been implemented — because users like me or you asked for this — because that is it convenient to have such seamless integration and it is present in other IDEs (not sure on this as I have not used another IDE for over 2 years).

Different people have different priorities — somebody is happy with basic code formatting while some screaming “I need 10 times more advanced .. and it should think in advance, or even instead of me”; some people are used to run phpunit tests from command line or happy to use standard coverage report .. while some asking for this to be integrated etc etc — everyone wants something different.

John

I know all that and ‘agree’ upto a point. The problem I am having is that all the IDE’s out there that target webdevelopers have tons of feature requests for every API, Framework and whatnot. And all set aside precious time and resources to implement those with different priorities while none of them, I repeat: NONE of them (including phpstrom) are complete in their basic feature set.
And no, I do not think the basic feature set is something that is a personal preference; it is in fact:

And frankly there is just too much sugar and spice in IDEs, and not enough sustenance.

Johnny Woo

There is a problem, though. There are things in IDEA that are extremely useful but nobody’s going to ask for them because users don’t know those features exist. I mean things like intelligent completion, smart shortcuts, etc, who knows what else they have for Java? Almost nobody expected rename refactoring in a PHP IDE before PhpStorm.

But instead of those subtle features, people keep insisting on ‘support’ for whatever they’re using. Look at most voted issues: support framework X, support template language Y, blah blah blah. People want the IDE to do LESS compilaton, for goodness’ sake, while last time I checked lessc could compile anything automatically in real time out of the box. I don’t think 20 half-baked plugins for every possible template language out there are better than one full-featured PHP editor, but it seems like polishing of the editor was sacrificed to populist plugins.

Andriy Bazanov

Sadly it is true. That’s marketing for you — more “new feature” tags impress new customers more than “improved” tags (as usually people do expect having already existing features at good/good enough level + they have used to this already — just look around you in any supermarket — “improved formula”, “improved recipe” etc).

And don’t forget another reason — people (as well as devs/sales) want to have new unique features in this IDE (when comparing to others) as well as those features that some other IDEs (or alike) may already have (e.g. “scroolbar with thumbnails — so you can see where in your long file you are” — instead of using Structure panel or “Navigate to” functionality — “It looks cool — I want it”; “It’s the ONLY thing that stops me from using your IDE” (and then the same message in another 5-6 tickets)). Devs have to mix both together (implementing bit from here bit from there), which is hard.

In any case — I see nothing wrong with having this particular feature be implemented in PhpStorm — running it separately and then constantly switching back to IDE to make changes is really annoying and time consuming (I’m not using it .. but for those who *have* to use it this is really big time saver).

Strayer

Huh, this does not work for me… I installed everything correctly and I can run phpcs in the console, but as soon as I click on validate I get a “Validating…”-popup with an empty progress bar and nothing else happens, locking me out of the IDE…

Things that may be a problem:
– Windows 7
– PHP is not in path, but phpcs.bat calls it directly anyways

rustam.vishnyakov

@Strayer
You have to double check your phpcs installation. I’d recommend to specify a full path to phpcs.bat, for example: c:Phpphpcs.bat and make sure you can run it exacly like this. For example:
cmd> c:Phpphpcs.bat –version
which must give the right version number. One of the reasons it fails may be some kind of errors it reports which are not visible in PhpStorm UI or the command simply doesn’t finish (in interactive mode, for example).

Strayer

Hi,

just tested this in a freshly started cmd which does not have PHP in its path or cwd:

Hello.
Is there any chance to see the results of PHP CodeSniffer working in box like “TODO” (in PhpStorm), not as tooltip when we put pointer on the incorrect part of code? I ask about something like is in NetBeans. There after any code change in the special result box we get list of all standard incompatibilities.

Thanks for great feature!
But i have problems with sniffs having deal with filenames. Instead of real path/name of file to check, $phpcsFile->getFilename() gives something like /tmp/__82348294982348623984.php, and my sniffs does’t work. Can you pass somehow real file name/path to sniff, for example using it as parameter with escapeshellarg(relapath)?

I cannot get this to work. I installed php 5.4.0 on my Windows box and it does not come standard with go-pear, so I had to download and install pear manually. After installing CodeSniffer with pear it does not seem to install phpcs (it is in the install package, it is simply not copied along with the rest of the package which is installed).
My first thought: why do i need to install all this rubbish to use this feature? My second thought: can anyone help me?

rustam.vishnyakov

@Eddy, we are not bundling PHP itself and related php tools with PhpStorm. Our goal is to provide as smooth integration with these tools as possible. For me the installation of CodeSniffer worked pretty well with the instructions from here: http://pear.php.net/package/PHP_CodeSniffer both on Windows and Linux. But I believe you can also ask the maintainers of the tool if something doesn’t work.

Eddy

@rustam:
Well after hours upon hours of trying to install and reinstall, reading and googling I finally got it to work. I found that pear doesn’t just “plug-and-play”. It depends on your system and AMP stack installation/settings if it works or not “out-of-the-box” I needed to manually change settings and even remove a file the pear intaller placed on my system in order for it to work.
Basically pear is just not a user-friendly package which is why I think it is rubbish to depend on (at least without an extra layer).

Eddy

And I mean “rubbish” as in: it is not “smooth” as you say.

Eddy

For other users on windows with problems, this is how I got it to work:

First of all, some amp stacks like WAMP (later versions) do NOT come with pear.bat/phar so you need to download it manually from http://pear.php.net/go-pear.phar and place that file in your php dir (something like ..wamp/bin/php/phpxxx) Then open an administrator level cmd prompt and type php -d phar.require_hash=0 PEAR/go-pear.phar. Choose local installation and keep the rest default.
Next add the php-directory to your PATH variable and type pear config-show. You will probably notice that all directories are wrong so you need to set them manually with pear config-set [dir-name] [dir-location]
Next you need to remove the php file (0kb file) from the PEAR directory and that should do it.

Alex

Looks like PHPStorm doesn’t work with PHP_CodeSniffer 1.3.4 which was released 15 May. With version 1.3.3. works fine.

rustam.vishnyakov

I suspect there may be some sort of configuration problem. Version 1.3.4 works fine for me, I have checked it with PhpStorm 4.0.2

Jusnit

Hi all,

How to set up PHP Code Sniffer(PSR-2) in local machine, where working with remote host? I have problem, installed php storm(4.0) in local machine and working with remote server. What will be path for PHP Code Sniffer path in local machine? . The local machine don’t have php or anything else…
Please help me to set the path…