Recently I’ve been upgrading the way I write code. I’ve been programming for a while now, but there’s always room for improvement. Lucky for us developers, there are numerous tools for us we can use to aid us in our development. Things like unit testing and coding standards are great of course. But there’s also another great tool you can use to make your code more solid and maintainable: PHP Mess Detector.
PHP Mess Detector is a tool that checks if you aren’t making a mess of your code. Think of methods of hundreds of lines, classes that extend class after class after class, lack of documentation, nested loops, etc. PHP Mess Detector points out these culprits and aids into writing more manageable code. So how can we harvest such great power?
Installing PHP Mess Detector on OSX
The first thing you need to do is fire up your terminal and add the Pear channel:
1 |
sudo pear channel-discover pear.phpmd.org |
To list the available packages of PHP Mess Detector do:
1 |
pear remote-list -c phpmd |
Now it’s time to install it:
1 |
sudo pear install phpmd/PHP_PMD |
Install the PHP Depend dependancy
If you can’t install the PHP Mess Detector pear package and you see an error like this, you need to install PHP Depend:
1 2 3 4 |
Unknown remote channel: pear.pdepend.org phpmd/PHP_PMD requires package "channel://pear.pdepend.org/PHP_Depend" (version >= 1.1.1) No valid packages found install failed |
First add the channel:
1 |
sudo pear channel-discover pear.pdepend.org |
List the available packages:
1 |
pear remote-list -c pdepend |
Install the correct package:
1 |
sudo pear install pdepend/PHP_Depend |
Try installing PHP Mess Detector again:
1 |
sudo pear install phpmd/PHP_PMD |
Set it up in PhpStorm
Open the configuration in PhpStorm, and navigate to “Languages & Frameworks > PHP > Mess Detector”.
Click on the … -icon in Development environment and set the path to where the PHP Mess Detector binary is installed (this usually is /usr/bin/phpmd ):
Navigate to “Editor > Inspections”, and enable “PHP Mess Detector validation” under “PHP”:
Create a ruleset.xml
Now you can use PHP Mess Detector in PhpStorm according to it’s default ruleset, or you can create your own.
Visitors give this article an average rating of 4.5 out of 5.
How would you rate this article?
★ ★ ★ ★ ★