📜 ⬆️ ⬇️

Code Quality Visualization with PhpMetrics

Recently I came across a great tool for analyzing PHP code. I publish the translation of an article with an overview of this tool.


PhpMetrics uses D3 and several complex algorithms to scan your application code and output intricate results reports.
image


Installing and using PhpMetrics


It is a little difficult to discuss without having a good example in front of your eyes, so let's install and run the utility, and then we will look at every detail.
')
Environment

For the experiments we need an isolated environment. As usual, we will use our trusty Homestead Improved Vagrant Box so that all readers have the same environment for experiments.

Install PhpMetrics

As with any modern PHP project, the installation is very simple. Installation will perform globally, making it available to all projects on the machine.

sudo composer global require 'halleck45/phpmetrics' 


Getting the code

We will test PhpMetrics on two heavyweight projects: Laravel and Symfony, let's consider frameworks, not projects. This means that we will not use the Composer'a create-project command, but examine the source code of each framework.

 git clone https://github.com/symfony/symfony symframe git clone https://github.com/laravel/framework lframe 

We do this to stay away from code that is not related to the framework.

Using PhpMetrics on a heavyweight project

Now that we have uploaded the projects, let's run PhpMetrics on them.

 mkdir Laravel mkdir Laravel/public phpmetrics --report-html=/Laravel/public/report_symfony.html symframe phpmetrics --report-html=/Laravel/public/report_laravel.html lframe 

Never mind that we use the “Laravel” directory for reports, it is just registered in the default settings of Homestead and we can use them for our small example to reduce the number of steps when setting up. This allows us to easily access reports at the following addresses.

 homestead.app:8000/report_symfony.html homestead.app:8000/report_laravel.html 

This is what we see in the Laravel report:
image

and this is what we get for symfony analysis:
image

Analysis


PhpMetrics offers many metrics. Let's clarify the essence of these circles and graphs and find out exactly what we are looking at!

Color coding

After reviewing the guide to the report, we can immediately note that symfony has more orange-red, while Laravel has more green. Note the important point - how many more files does Symfony have in relation to Laravel - you can clearly see this by comparing the density of the circles.
PhpMetrics takes into account people with vision problems. The “color blind” checkbox changes the color of the circles from green, yellow and orange to stripes at different angles.
image

Cyclomatic complexity and sustainability score

If you look closely, it becomes clear that the circles are indicators of CC and MI, and, as stated in the documentation, large red circles are likely to be very difficult to maintain.
Both terms are easy to googling, but let me rid you of this and explain on your fingers.
Cyclomatic complexity essentially shows the number of various paths through which you can go through the logic of the script. The more paths, the higher the complexity of the file. A good way to avoid high CC is to make the code as modular and atomic as possible; this happens naturally if you follow the SOLID principles.
As Wikipedia says, the maintainability index is calculated using specific formulas based on the following metrics — the number of lines of code, McCabe, Holstead. The number of lines of code is self-evident - literally counting the number of lines of code. The Mac Cabe metric is actually the second name for cyclomatic complexity. Halstead's complexity is a set of formulas that try to look into the syntax of the code and derive properties such as coding complexity, a set of unique program commands, difficulty understanding the program, etc. If you are interested in specific formulas, check out Wikipedia.
Taking all this into account, the circles are set in size and color, reflecting the sum of all standard metrics of complexity and maintainability of the code.

Tables

If you want to see all the data in tabular form, you can select “Explore” in the report menu.
image

This table will include all of the above and more - from the number of lines of code to the number of classes and, moreover, information for both the directory and the file. This incredibly detailed report you need to search for very accurate information on the entire code in one place.

Customizable graphics

The middle area in the report contains a custom graph.
image

In the example above, we compare for each file the ratio CC (mentioned above) and Lcom (as far as the methods are not related to each other through properties), but you can easily switch the values ​​of the X and Y axes to something else. For example, there is a comparison of complexity with the number of lines of code - you can note that the trend for Laravel is more proportional - the more Loc, the greater the complexity. while for symfony the picture is more blurred for some files.
image
For example, the BinaryMode.php file Symfony has a very big complexity with 150 lines of code, and Response.php with 1275 lines has a very low complexity indicator. Examine the files that give curious results and see what can be obtained from these observations.

Abstraction / Instability

This graph is very well explained here , but if it is short, the middle diagonal line is called the balance or the main sequence. The goal is to keep your classes as close to the line as possible, because it means that they are balanced — fairly abstract and concrete enough.
Note translator: the author for some reason failed to build this graph. To make it clear what I mean, I give an example for Yii2.
image

Evaluation

Under the assessment refers to the graph, which reflects the average values ​​of various indicators of the entire project. These values ​​are not absolute, but are given in comparison with modern projects considered by the tool at the moment. On the report page there is a reservation, they say, the schedule is purely cosmetic, does not reflect an objective picture.
image
image
The tool considers both frameworks the same in terms of maintainability. Not very shocking the conclusion that Laravel is friendlier to new developers and simpler in terms of algorithms, not to mention smaller volumes.

Map of connections

The map of connections is more useful for small projects - it allows you to see the connections between classes. If a class extends or implements another, the map of links will show it. Due to the fact that in both projects the number of classes and the connections between them are too large, the map is not so useful for our case - in fact, I did not have it built for Symfony. But if you try to build it on a test project with a smaller number of links, you will definitely see the benefits.
Note translator, so that it was clear what I was talking about, I give an example for Yii2
image

Redistribution

And finally, the redistribution information page summarizes all the results in a simpler form than on the Explore tab.

Symfony:
image

Laravel:
image

Comparison results


So, what conclusions can be drawn from the results? Without making a deep analysis, we can conclude that:
  1. Laravel is more user-friendly for new developers, has simpler algorithms and less bloated files.
  2. Laravel is radically more lightweight. Symfony has at least three times the most — methods, classes, lines of code ... it's three times more massive.
  3. Symfony has a higher relative complexity (middle column in the redistribution report).
  4. Laravel is more stable in terms of complexity in files - complexity is proportional to the lines of code, in Symfony this is not the case.
  5. Symfony can work on dividing some complex files into more atomic files — large red circles everywhere on the first graph.

See what else you can find out by playing on your own with graphs and numbers, let us know in the comments.

Conclusion


PhpMetrics is another tool in a long line of code quality analyzers, but with buns. In comparison with the search for typical code problems by standard means, here we see a whole set of new attributes and explanations in excellent form. To find out what each attribute means, refer to the table .
Will you use phpMetrics to analyze your project? Do you embed this tool in your IDE ? Do you see this tool as a constant addition to your continuous integration process?

Source: https://habr.com/ru/post/254941/


All Articles