Integrate PHP Code Sniffer and PHP Mess Detector in PhpStorm for Magento projects.
PHP Code Sniffer is a tool that allows us to check if our code follows some given standard.
PHP Mess Detector is a tool that looks our source code and can tell us, given certain rules, if the code has potential problems like possible bugs, non-optimal code, unused parameters, overcomplicated code, etc.
This way, if our codes follows some standard and those potential problems are reduced to its minimum, we can ensure that it has a certain degree of quality, and it's also easy for any other developer to read because the code is clean, and (hopefully) well written. But the best of it, if we are disciplined through time with the use of these tools, the code becomes easier to maintain.
In order to integrate these tools with PhpStorm you (obviously) need, as first step, to install them.
Becasuse this post is not about how to install these tools, here are the links where you can read how to install them:
Here are rulesets for PHP Code Sniffer and PHP Mess Detector
Note that these rulesets are for Magento 2, but they work great for Magento 1.xx versions.
I have downloaded them in my personal folder and put them in the proper rulesets folders for each one.
In my environment the resulting paths are:
- PHP Code Sniffer: /usr/share/php/PHP/CodeSniffer/Standards/Magento/ruleset.xml (I've added the Magento folder and put the ruleset.xml file inside)
- PHP Mess Detector: /usr/share/php/data/PHP_PMD/resources/rulesets/Magento/ruleset.xml (I've renamed the xml file)
Now, just open your Magento project in PhpStorm, then, in the IDE, go to File → Settings.
In the dialog that appears, select Inspection in the left side. Once the configuration for Inspections appears, open the PHP subtree. You will notice two configurations for both PHP Code Sniffer validation and Php Mess Detector Validation
Lets start with PHP Code Sniffer validation. Enable this validation using the checkbox. Then select the Magento coding standard in the options on the right (if it does not appears, just click the refresh button to update the list of available standards).
Apply the changes.
Now is the turn of PHP Mess Detector Validation. Same as before, enable the validation using the checkbox and then configure properly. Here the trick is to add a new custom ruleset (using the + button). Just put the path to the donwloaded ruleset xml file (that you have copied to the proper folder).
Apply changes again and we are done!!, at least configuring.
Now is time to check our code, and for this you have various options, like check the entire project of just a single file. What I always do is to check even a single file or just an extension or, to say something, the model's folder of a given module.
To do this, in the tree view of the project, secondary click on the file or folder you want to inspect.
Then, click Inspect Code.... This makes a popup to appear, just click on OK.
Wait... and viola!
You have a really nice report of the inspections!
Now, is up to you, as a good programmer, to reduce those “errors” to its minimum!