avatar Deluxe Blog Tips About Projects

Measure complexity and maintainability in PHP

I optimize my plugins regularly and I need a way to measure the project's statistics such as complexity and maintainability. After searching on Google, I found PHPMetrics, a wonderful project that can help me understand lots of key metrics for a PHP project. The result is displayed in a HTML report with some graphs and table of metrics which are very easy to understand. I tried using it with Meta Box and here is the result:

Meta Box plugin metrics

The bubbles of the report is an overview of the maintainability and complexity of the project, where:

  • The number of bubbles simply are the number of classes. It does not mean the project is hard to maintain.
  • The color of bubbles / metrics means the maintainability of the class / project: read = hard, yellow = acceptable but careful, red = hard
  • The size of bubbles means the complexity of the class, smaller is better

In the table, there are some key metrics:

  • lloc: Number of logical lines of code (without comments), less is better.
  • length: Halstead metric of the project, less is better
  • MI: Maintainability index is based on Halstead's metrics, LOC and Cyclomatic complexity number, more is better.

To understand other metrics, please read the explanation at the PHPMetrics homepage.

So, Meta Box looks not too complicated and easy to maintain which is quite good to me. In the next versions, my developers and I will work hard on refactoring the plugin, make it smaller and less logic.

I also went further to compare Meta Box with CMB2, a very similar plugin and here is the result of CMB2:

CMB2 metrics

Some notes:

  • The CMB2 uses fewer classes than Meta Box (this is obvious because Meta Box provides more field types than CMB2, where each field type is a class)
  • Although fewer classes, CMB2 seems a little bit more complicated than Meta Box in all metrics (size of bubbles, color of bubbles, lloc, length and MI)

And here is the result of Advanced Custom Fields, the most popular plugin for custom fields:

ACF metrics

It's easy to see that ACF is more complicated and harder to maintain than both Meta Box and CMB2. It's quite obvious since ACF provides UI for users to add and manage custom fields, which requires a lot of work and logic.

I also wanted to check the statistics of WordPress to see how good it is. Unfortunately, the result doesn't look good:

WordPress metrics

Hard to maintain, a lot! Here is the full report.

You can find the metrics of other PHP frameworks as well. I'm very surprised about Laravel and Fuel frameworks, they're amazingly good!

So, if you want to understand your project and keep the development in a good direction, PHPMetrics is a must have tool. Have fun coding and let me know what you think about it in the comments.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Comments