Tag Archives: static analysis

Tools: phpMetrics

I’m researching some tools to visualize PHP code which I think can be helpful to understand the complexity of a project.
In this article I explore a tool called phpMetrics that is a static analysis tool for php. It can collect information about PHP files in your project.

This tool can be installed via composer as a standalone tool.

composer global require 'phpmetrics/phpmetrics'
phpmetrics --report-html=myreport.html /path/of/your/sources

When the tool is installed you can run it from the composer global directory. It starts with analysing all the PHP files and collecting metrics and generating everything that is needed.

vagrant@seoeffect:/var/www/html$ /home/vagrant/.composer/vendor/bin/phpmetrics --report-html=myreport.html /var/www/seoeffect
PHPMetrics by Jean-François Lépine 

  795/1853 [============>---------------]  42%

There are a lot of numerical metrics collected like the cyclomatic complexity, number of lines, number of classes etc.. For an overview of all metrics the documentation should be consulted. These metrics can be quite useful for a global overview about how well your code is doing and where the possible problems in your code are. All these metrics are drawn into graphs that give a quick overview about the state of a project.

When the analyzing is done there is one html file generated with an overview of the metrics. For this project there are a lot of red circles so there is a lot of work to do.


One particular view is the relation view to see how classes are interconnected. When the project is getting to large the map is getting difficult to read and is not useful anymore. As you can see in the following screenshot all the class names are getting a mess and therefore this graph gets quite unusable.



I had a quick look at phpMetrics and it works great and gives a lot of metrics about the project that is analysed. But for really large projects it is difficult to get an overview and to pinpoint the exact class where the problem is. I’m looking for tooling to perform static analysis on large (legacy) PHP projects so dealing with a lot of files should be possible.

The problem with dynamic languages and large projects

Abstract: In large projects it is important to find unused and old code and remove those pieces of code. With dynamic languages this is virtually impossible. It is important to clean and refactor unused parts of a system because this can become a security risk. Also a lot of “noise” is added to the system that makes it more difficult to understand the system.

While working on a large project I have the need to find pieces of code that are no longer in use. With a dynamic language and specific PHP this is virtually impossible and below I will give an explanation why that is. Continue reading The problem with dynamic languages and large projects