Friday 29 April 2016

PHP: Getting PHPCS and PHPMD working within PHPStorm

G'day:
These are just some quick notes on installing PHP Code Sniffer and PHP Mess Detector to work within PHPStorm. There's nothing particularly elucidating in here, it's simply what I did to get it working, cos someone asked me so I sat down and worked it out. Also my flight's been delayed so I have time to kill until Aer Lingus gets its act together.

I'm running PHPStorm 10 on Windows (Windows 10, in this case, not that it matters).

First up I used composer to install both applications:

C:\Users\adam.cameron\AppData\Roaming\Composer>php c:\ProgramData\ComposerSetup\
bin\composer.phar global require phpmd/phpmd

[much stuff ensued, but it all installed OK]

C:\Users\adam.cameron\AppData\Roaming\Composer>php c:\ProgramData\ComposerSetup\
bin\composer.phar global require squizlabs/php_codesniffer


Note that I initially tried to install the fabpot/php-cs-fixer as suggested in the composer global installation examples (I can never remember how to do it, so I googled), but that version of PHPCS seems to be out of date or dead or something. It dun't work in PHPStorm, anyhow.  I removed that:

C:\Users\adam.cameron\AppData\Roaming\Composer>php c:\ProgramData\ComposerSetup\
bin\composer.phar remove fabpot/php-cs-fixer

Once in PHPStorm I then go to File > Settings > Languages & Frameworks > PHP > Code Sniffer, and use the UI to point it to C:\Users\adam.cameron\AppData\Roaming\Composer\vendor\bin\phpcs.bat And do similar for PHPMD: C:\Users\adam.cameron\AppData\Roaming\Composer\vendor\bin\phpmd.bat.

In there one can "Validate" the install, which is how I found out the fabpot version of PHPCS didn't work.

Once I've done that, I went into Settings > Editor > Code Style > Inspections, within which each option of PHPCS and PHPMD is listed, and having selected each of those, can select the rulesets to use.

I just went for PSR-2 for PHPCS, and all options checked for PHPMD. At some stage I'll probably create a decent ruleset for my work (I hate PSR-2, and and to apply some Clean-Code-esque rules too), but for the time being, that'll do.

Having done that, PHPStorm now applies the rules:


Whilst I think that particular rule is bloody stupid, it is indeed one of PSR-2's rules, and it's also identifying it's being checked by phpcs. Success.

That's it!

Righto... time for another pint...

--
Adam