|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hal\Metric\Class_\Component; |
|
3
|
|
|
|
|
4
|
|
|
use Hal\Metric\FunctionMetric; |
|
5
|
|
|
use Hal\Metric\Metrics; |
|
6
|
|
|
use Hoa\Ruler\Model\Bag\Scalar; |
|
7
|
|
|
use PhpParser\Node; |
|
8
|
|
|
use PhpParser\Node\Stmt; |
|
9
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Calculates Maintainability Index |
|
13
|
|
|
* |
|
14
|
|
|
* According to Wikipedia, "Maintainability Index is a software metric which measures how maintainable (easy to |
|
15
|
|
|
* support and change) the source code is. The maintainability index is calculated as a factored formula consisting |
|
16
|
|
|
* of Lines Of Code, Cyclomatic Complexity and Halstead volume." |
|
17
|
|
|
* |
|
18
|
|
|
* MIwoc: Maintainability Index without comments |
|
19
|
|
|
* MIcw: Maintainability Index comment weight |
|
20
|
|
|
* MI: Maintainability Index = MIwoc + MIcw |
|
21
|
|
|
* |
|
22
|
|
|
* MIwoc = 171 - 5.2 * ln(aveV) -0.23 * aveG -16.2 * ln(aveLOC) |
|
23
|
|
|
* MIcw = 50 * sin(sqrt(2.4 * perCM)) |
|
24
|
|
|
* MI = MIwoc + MIcw |
|
25
|
|
|
* |
|
26
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
|
27
|
|
|
*/ |
|
28
|
|
|
class MaintainabilityIndexVisitor extends NodeVisitorAbstract |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Metrics |
|
33
|
|
|
*/ |
|
34
|
|
|
private $metrics; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* ClassEnumVisitor constructor. |
|
38
|
|
|
* @param Metrics $metrics |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(Metrics $metrics) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->metrics = $metrics; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritdoc |
|
47
|
|
|
*/ |
|
48
|
|
|
public function leaveNode(Node $node) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($node instanceof Stmt\Class_) { |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
if ($node instanceof Stmt\Class_) { |
|
|
|
|
|
|
53
|
|
|
$classOrFunction = $this->metrics->get($node->namespacedName->toString()); |
|
|
|
|
|
|
54
|
|
|
} else { |
|
55
|
|
|
$classOrFunction = new FunctionMetric($node->name); |
|
56
|
|
|
$this->metrics->attach($classOrFunction); |
|
57
|
|
|
} |
|
58
|
|
|
if (null === $lloc = $classOrFunction->get('lloc')) { |
|
59
|
|
|
throw new \LogicException('please enable length (lloc) visitor first'); |
|
60
|
|
|
} |
|
61
|
|
|
if (null === $cloc = $classOrFunction->get('cloc')) { |
|
62
|
|
|
throw new \LogicException('please enable length (cloc) visitor first'); |
|
63
|
|
|
} |
|
64
|
|
|
if (null === $loc = $classOrFunction->get('loc')) { |
|
65
|
|
|
throw new \LogicException('please enable length (loc) visitor first'); |
|
66
|
|
|
} |
|
67
|
|
|
if (null === $ccn = $classOrFunction->get('ccn')) { |
|
68
|
|
|
throw new \LogicException('please enable McCabe visitor first'); |
|
69
|
|
|
} |
|
70
|
|
|
if (null === $volume = $classOrFunction->get('volume')) { |
|
71
|
|
|
throw new \LogicException('please enable Halstead visitor first'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// maintainability index without comment |
|
75
|
|
|
$MIwoC = max( |
|
76
|
|
|
(171 |
|
77
|
|
|
- (5.2 * \log($volume)) |
|
78
|
|
|
- (0.23 * $ccn) |
|
79
|
|
|
- (16.2 * \log($lloc)) |
|
80
|
|
|
) * 100 / 171 |
|
81
|
|
|
, 0); |
|
82
|
|
|
if (is_infinite($MIwoC)) { |
|
83
|
|
|
$MIwoC = 171; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// comment weight |
|
87
|
|
|
if ($loc > 0) { |
|
88
|
|
|
$CM = $cloc / $loc; |
|
89
|
|
|
$commentWeight = 50 * sin(sqrt(2.4 * $CM)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// maintainability index |
|
93
|
|
|
$mi = $MIwoC + $commentWeight; |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
// save result |
|
96
|
|
|
$classOrFunction |
|
97
|
|
|
->set('mi', round($mi, 2)) |
|
98
|
|
|
->set('mIwoC', round($MIwoC, 2)) |
|
99
|
|
|
->set('commentWeight', round($commentWeight, 2)); |
|
100
|
|
|
$this->metrics->attach($classOrFunction); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.