How to understand the PhpMetrics' metrics

According to me, you should understand at least the following metrics at beginning:

Lack of cohesion of methods


Lack of cohesion of methods (LCOM) is a measure of the number of responsabilities of class.

Thus, if you consider the SOLID principles, the ideal LCOM is LCOM=1.

We can take an example:

<?php
class Example {
    private $a;
    private $b;

    public function f1() {
        $this->a = 1;
    }

    public function f2() {
        $this->f3();
    }

    public function f3() {
        $this->a = 2;
    }

    public function f4() {
        $this->b = 3;
    }

    public function f5() {
        $this->b = 4;
    }

}

If we examine this class, we get two flows:

  • f1() share attribute a with f3(), and f2() calls f3()
  • f4() share attribute b with f5()

Therefore, the LCOM value for Example is 2. Indeed, Example has two responsabilities.

Maintainability Index


Maintainability index evaluates ... the maintainability of any project. It provides a score between 0 to 221.

This score is standard, and works for any language : PHP, .Net, Java...

The calculation's formula is the one created in 1992 by Paul Oman and Jack Hagemeister, promoted by the Software Engine Institute (SEI) in 1997:

MI = 171 - 5.2 * log2(V) - 0.23 * CC - 16.2 * log2 (LOC) + 50 * sin (sqrt(2.4 * CM))

With:

  • V - the Halstead Volume
  • CC - the cyclomatic Complexity
  • LOC - Lines of code
  • CM - percentage of comment lines (value between 0 and 1, 1 => 100%)

Halstead' difficulty


Soon