PHPDepend: Measure Software Metrics Inside Your PHP Code

Measuring your PHP software metrics never been this f****** easy!

Muhammad Naufal Pratama
5 min readMay 11, 2023
Photo by Fotis Fotopoulos on Unsplash

Hello! Seeing the amount of interest in PHPMD in the previous article, in this article I will tell you more about one of the greatest combinations of PHPMD, which is PHPDepend or PDepend. The primary purpose of PHPDepend is to calculate software metrics for your PHP code. Measurable software metrics such as extensibility, reusability, maintainability, cyclomatic complexity, halstead volume, and many others. You can read more about PHPDepend on their documentation page or GitHub page. Here is the outline of this story:

  • Installing PHPDepend
  • Calculate your software metrics using PHPDepend
  • Managing optional configuration file

Without further wait, let’s get into The PHPDepend software metrics tutorial!

UPDATE: I have an urgent update below, regarding this tool!

UPDATE 2: Good news finally comes in!!!

[1] Installing PHPDepend 📀

Same like PHPMD, PHPDepend 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 PHPDepend 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 PHPDepend later
"pdepend/pdepend" : "@stable"

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

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

5. Execute this command to install the PHPDepend

composer update

There you go. Now you can use PHPDepend to measure software metrics inside the targeted folder or directory by running below the command.

./vendor/bin/pdepend --summary-xml=./result.xml ./target-folder

Where:

  • target-folder = the target folder you want to calculate the metrics
  • result.xml = the metrics calculation result file

[2] Calculate Your Software Metrics Using PHPDepend 🔍

Unlike PHPMD where we required to setup a configuration file, here in PHPDepend we don’t required to do that, although we can also add an optional configuration file (I’ll tell you about this later). But for now, try to run this command and see what you get inside the result.xml

./vendor/bin/pdepend --summary-xml=./result.xml ./targeted-folder

The result.xml will pretty much look like this.

result.xml

Actually, beside the metrics result in number format, you can also get a chart and also pyramid overview result of the software metrics. To achieve that run the below command

./vendor/bin/pdepend --summary-xml=./result.xml --jdepend-chart=./jdepend.svg --overview-pyramid=./pyramid.svg ./targeted-folder

The “ — jdepend-chart” will help you get the chart of the metrics and “ — overview-pyramid” will give you the pyramid format of the software metrics.

jdepend.svg
pyramid.svg

[3] Managing Optional Configuration File 🔮

Although it’s not mandatory, a configuration file can be quite help full. In my experience, I use the config file to help me prevent PHPDepend cache the result between each run. I found about this caching problem here. So the config file is actually very helpful for me. The complete docs of the config file can be seen here. Here is the config file I use:

pdepend.xml

The above config file will prevent PHPDepend to cache the calculation results. If you want to cache the calculation results, use the below config file:

pdepend.xml

The above config will cache the result into a file and put the cache files into folder “./cache/phpdepend”.

To use the config file, name the config file as pdepend.xml and put the pdepend.xml inside the targeted folder where you run the PHPDepend. For example, if I want to calculate the metrics inside my controllers folder, I create a pdepend.xml file inside the controllers folder and run the previous command as usual.

There are many other configuration options that you can use, such as set the cache time-to-live and editing the chart. Find out more here https://github.com/pdepend/pdepend/blob/2.13.0/src/site/rst/documentation/handbook/configuration/index.rst#cache

Thank you for reading and let me know if you have questions or clarifications about the PHPDepend, as long I can help, I’ll try my best to help you. Until next time 👋

📌 UPDATE:

I recently found that PHPDepend incorrectly uses the coefficients in the Halstead volume formula. This makes calculating Maintainability index would give a wrong answer. Read more here:

For a temporary workaround you can use my updated version of PHPDepend here. For tutorial how to use it, I’ll tell you in the next story. See you soon!

I decide to include the tutorial of how to use it here

🧬 PHP: How To Edit Your Package Inside Vendor Folder and Use It For YOUR PROJECT!

Sometimes when we use a certain package for our project, we realize that the package that we use is not like we want. Bug, missing features, and some other reason come up and make us regret using the package. Then we decide to look for another package that has almost the same functionality as the previous package. If you are lucky enough, you’ll find the other package. However, life doesn’t go as easy as we want to. In a lot of cases, there is no other package like we want. So then, your “Developer Power” comes up and you decide to EDIT the “broken” package and make a pull request while finger-crossing that the maintainer will accept your pull request. You’re waiting… waiting… and still waiting. No response at all. As I said, life doesn’t go as we expect. But worry not, it turns out, that in PHP and composer, you can use your custom or edited package for your project, without needing the original package. And here’s how you do it!

  1. Fork the repository package to your GitHub account. In this example I fork the PHPDepend repository.
  2. Inside your forked repository, create a new branch and give it a name, e.g. formula
  3. Make some changes to the code inside the branch formula and don’t forget to commit it.
  4. Open you composer.json file. Edit the package key version number. Because in this example I use the PHPDepend package, so I change the pdpend/pdpend inside the require-dev property

From

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

To

{
"require-dev": {
"pdepend/pdepend" : "dev-formula"
}
}

The format for the version value is dev-yourBranchName . Because in this example I name the branch formula , so I name the version with dev-formula. Remember to add the dev- before your branch name!

6. Inside the repositories property add a like toward the GitHub forked repository that in step 1 you already done.

7. Add a new property called provide and put the name and version of the original package.

{
"provide": {
"pdepend/pdepend": "@stable"
}
}

8. Run composer update and voila! You’re happy to use your own version of PHP Package!

Sources:

[1] PHPDepend

[2] Provide

--

--