Category Archives: PHP

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.

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.
Continue reading Attributes in Hack

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.
Continue reading Operator overloading

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 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);
        $this->connector->insert($collection, $document);

PHP 5.6

Earlier I wrote about PHP 5.5 that brought us generators and the finally keyword. PHP 5.6 also brings us some nice improvements in the language. PHP is improving with every release, the only problem is that we cannot use these nice features in production because distributions are still a few releases behind.
Continue reading PHP 5.6

PHP the right way

A nice initiative is the website that shows how PHP can be use in the right way with a lot of pointer to developers where to start and how PHP is different to other languages. Keep up the good work 🙂 Click the banner or the link to read more about it.


Hacking PHP

Facebook has launched the programming language HACK as neither a subset or superset of PHP but more a dialect. It adds typing, generics, lambdas and some more features to PHP.
In this blog I will show how some trivial PHP can be translated to HACK and some things I encountered while trying it out.

From PHP…

I start with a simple pure PHP5 class with a constructor and one method that echos the current variable $a. Nothing extraordinary but I want to show what have to be done to convert this code into a hack file.
Continue reading Hacking PHP

Functional programming in PHP

With the introduction of lambda (or anonymous) functions in PHP 5.3 it is now possible to introduce some nice features from the functional programming paradigm.

Higher order function

When a method (or function) accepts another function as parameter this function is called a higher order function. The following already worked in PHP < 5.3 and is a simple implementation of functional programming with callable variables.

$data = array(10,2,6,4);
function cmp($a, $b) {
    if ($a == $b) return 0;
    return $a < $b ? -1 : 1;
usort($data, "cmp");

Continue reading Functional programming in PHP