## 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. ## Conclusion 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. Tagged with: , Posted in PHP, Software Engineering ## Silex framework in 5 minutes Silex is based on some of the Symfony components and is a simple and easy framework. In this tutorial I’ll show how to install and get Silex up and runnning in 5 minutes. Read more › Tagged with: Posted in PHP ## Quick benchmark on PHPNG and HHVM I performed a quick (and dirty) benchmark on a recent build of PHP7 and HHVM. PHP7 is performing a lot better than older versions but HHVM is still performing a lot faster. This benchmark is pure for fun and is in no way scientific 😉 Read more › Tagged with: , , Posted in Hack, PHP ## Attributes in Hack ## First there where PHP annotations The introduction of PHP style annotation by (mis)using comments always felt like a bad idea for me. Maybe because I think a comment cannot have any meaning beside some meaningful information for a developer or a documenting system that parses the comments to extract the information. But other than it should be possible to remove all comments without changing the behaviour of a progam. Read more › Tagged with: , Posted in Hack, PHP ## Operator overloading For me it is important to write short, easy to read and easy to understand code. When you write a piece of code and see it after a couple of months or even years the meaning should be clear in an instant. One way to achieve brevity in programming is the use of operator overloading. With this it is possible to change the meaning of an operator. In this example I’ll take two interpreted languages I use (Python and PHP) and show how those languages differ in the support for operator overloading and why Python has a more elegant solution to this problem. Read more › Tagged with: , Posted in PHP, Python ## Using assertions in PHP When writing code a lot of assumptions are done and must be done to make code easy to read and write. Assertions can only be used for debugging purposes and should be turned off in production code. So no validation must rely on a assertion. $x = 5;
assert($x > 10);  Running this code with assertions active will output the following because the expression $x > 10 will not yield true.

$php -d assert.active=1 index.php PHP Warning: assert(): Assertion failed in /home/leon/assert/index.php on line 5 PHP Stack trace: PHP 1. {main}() /home/leon/assert/index.php:0 PHP 2. assert() /home/leon/assert/index.php:5  It is tempting to use assertions to validate parameters and to prevent invalid data to enter a system. The problem will come up when the assertions are turned off, then the whole validation is not working anymore. class Person { private$age;

public function setAge($age) { assert($age != 0);
assert($age < 150);$this->age = $age; } }  A better solution in this case would be to use a if statement that checks the arguments and throws an exception when some invalid value is entered. class Person { private$age;

public function setAge($age) { if ($age == 0 || $age > 150) throw new InvalidArgumentException("Invalid age");$this->age = $age; } }  A real use case for assertions would be to check if an object exists when in normal circumstances this should not be a problem. During developing it can help to check those things. class KeywordCollection { private$connector;

public function save($document) { assert($this->connector instanceof MongoCollection);