PHPMD: A PHP Code Smells Detector

Detect the messines in your PHP code without pulling you toward the messines itself

Muhammad Naufal Pratama
3 min readMay 8, 2023
Photo by Emile Perron on Unsplash

Hello! How’re you doing so far, guys? Still, remembering me? After a pretty long time without an article I publish, I come back and back and back… no…no…no… I’m not the Demon witch, Lion. But instead, I’m still a quarter-time writer, and here in this very first PHP story of mine, I’ll guide you on how to detect code smells inside your PHP code using my lovely tool, PHPMD (PHP Mess Detector). As usual, I won’t describe the tool I use in my story, and in this case, PHPMD. You can always refer to the PHPMD on its website at https://phpmd.org/ . So, here is the outline for this story.

  • Installing PHPMD
  • Setting up the configuration file
  • Using PHPMD to detect code smells

Without further wait, let’s get into The PHPMD tutorial for detection code smells!

[1] Installing PHPMD 📀

PHPMD download page already provides a good explanation of how to install the tool. And here I just want to make more clear for you. Below are the steps that I take to install PHPMD on my PHP project. First of all, make sure that you have already a PHP project with composer.json file.

  1. Prepare your PHP project
  2. Open up your composer.json project file
  3. Add this line inside your “require-dev” so that we can install the PHPMD later
"phpmd/phpmd" : "@stable"

4. So your “require-dev” will pretty much looks like this

{
"require-dev": {
"phpmd/phpmd" : "@stable"
}
}

5. Execute this command to install the PHPMD

composer update

There you go. Now you can use PHPMD to detect code smells inside files or directories by run below the command.

./vendor/bin/phpmd ./folder xml rulesets.xml --report-file ./result.xml

Where:

  • folder = the target folder or file you want to detect
  • rulesets.xml = a PHPMD configuration file (we will discuss about it soon)
  • result.xml = the code smells detection result file

[2] Setting Up The Configuration File 📁

I already mentioned that we need to add a configuration file so that we PHPMD can clearly define what code smells to be detected. Common practice for naming this configuration file is by naming it with rulesets.xml. Copy the below XML code and paste it inside your rulesets.xml file.

rulesets.xml

In the above code, we set a total of 6 rulesets. 5 with the general ruleset (from line 4 to line 8) and the last one, which is “cleancode.xml/IfStatementAssignment” means that we only want to use the “IfStatementAssignment” ruleset that live inside the cleancode.xml rule, skipping the others available rules inside it.

You can find the complete rulesets here.

[3] Using PHPMD To Detect Code Smells 🔍

Now you can run this command again and see the PHPMD result inside the result.xml file.

./vendor/bin/phpmd ./folder xml rulesets.xml --report-file ./result.xml

The result.xml will pretty much look like this.

result.xml

I guess that’s all for the PHPMD. Always refer to the docs in case you need something more deep. And one thing to note, as far as I know, PHPMD doesn’t cache the result of each run. Although you can force it to cache using certain steps explained here. https://github.com/phpmd/phpmd/issues/534

Don’t miss me too much and see you on the next one, where I will share you guys about the best friend of PHPMD, which is PHPDepend 👋

And here is the tutorial of measuring software metrics using PHPDepend

Sources:

[1] PHPMD

--

--