MaintainabilityIndex   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 2
lcom 0
cbo 4
dl 0
loc 33
rs 10
c 1
b 1
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A calculate() 0 22 2
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Metrics\Design\Component\MaintainabilityIndex;
11
12
/**
13
 * Calculates Maintainability Index
14
 *
15
 *      According to Wikipedia, "Maintainability Index is a software metric which measures how maintainable (easy to
16
 *      support and change) the source code is. The maintainability index is calculated as a factored formula consisting
17
 *      of Lines Of Code, Cyclomatic Complexity and Halstead volume."
18
 *
19
 *      MIwoc: Maintainability Index without comments
20
 *      MIcw: Maintainability Index comment weight
21
 *      MI: Maintainability Index = MIwoc + MIcw
22
 *
23
 *      MIwoc = 171 - 5.2 * ln(aveV) -0.23 * aveG -16.2 * ln(aveLOC)
24
 *      MIcw = 50 * sin(sqrt(2.4 * perCM))
25
 *      MI = MIwoc + MIcw
26
 *
27
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
28
 */
29
class MaintainabilityIndex {
30
31
    /**
32
     * Calculates Maintainability Index
33
     *
34
     * @param \Hal\Metrics\Complexity\Text\Halstead\Result $rHalstead
35
     * @param \Hal\Metrics\Complexity\Text\Length\Result $rLoc
36
     * @param \Hal\Metrics\Complexity\Component\McCabe\Result $rMcCabe
37
     * @return Result
38
     */
39
    public function calculate(\Hal\Metrics\Complexity\Text\Halstead\Result $rHalstead, \Hal\Metrics\Complexity\Text\Length\Result $rLoc, \Hal\Metrics\Complexity\Component\McCabe\Result $rMcCabe)
40
    {
41
        $result = new Result;
42
        $result->setMaintainabilityIndexWithoutComment(max(
43
             (171
44
             - (5.2 * \log($rHalstead->getVolume()))
45
             - (0.23 * $rMcCabe->getCyclomaticComplexityNumber())
46
             - (16.2 * \log($rLoc->getLogicalLoc()))
47
             ) * 100 / 171
48
             ,0));
49
50
51
        // comment weight
52
        if($rLoc->getLoc() > 0) {
53
            $CM = $rLoc->getCommentLoc() / $rLoc->getLoc();
54
            $result->setCommentWeight(
55
                50 * sin(sqrt(2.4 * $CM))
56
            );
57
        }
58
59
        return $result;
60
    }
61
}