|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hal\Metric\Class_\Text; |
|
4
|
|
|
|
|
5
|
|
|
use Hal\Metric\FunctionMetric; |
|
6
|
|
|
use Hal\Metric\MetricNullException; |
|
7
|
|
|
use Hal\Metric\Metrics; |
|
8
|
|
|
use Hoa\Ruler\Model\Bag\Scalar; |
|
9
|
|
|
use PhpParser\Node; |
|
10
|
|
|
use PhpParser\Node\Stmt; |
|
11
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Calculates Halstead complexity |
|
15
|
|
|
* |
|
16
|
|
|
* According Wikipedia, "Halstead complexity measures are software metrics introduced by Maurice Howard Halstead in |
|
17
|
|
|
* 1977 as part of his treatise on establishing an empirical science of software development. |
|
18
|
|
|
* Halstead makes the observation that metrics of the software should reflect the implementation or |
|
19
|
|
|
* expression of algorithms in different languages, but be independent of their execution on a specific platform. |
|
20
|
|
|
* These metrics are therefore computed statically from the code." |
|
21
|
|
|
* |
|
22
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
|
23
|
|
|
* @package Hal\Metric\Class_\Coupling |
|
24
|
|
|
*/ |
|
25
|
|
|
class HalsteadVisitor extends NodeVisitorAbstract |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Metrics |
|
30
|
|
|
*/ |
|
31
|
|
|
private $metrics; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Metrics $metrics |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Metrics $metrics) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->metrics = $metrics; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
|
|
public function leaveNode(Node $node) |
|
45
|
|
|
{ |
|
46
|
|
|
if ($node instanceof Stmt\Class_ || $node instanceof Stmt\Function_ || $node instanceof Stmt\Trait_) { |
|
47
|
|
View Code Duplication |
if ($node instanceof Stmt\Class_ || $node instanceof Stmt\Trait_) { |
|
|
|
|
|
|
48
|
|
|
$name = (string)(isset($node->namespacedName) ? $node->namespacedName : 'anonymous@' . spl_object_hash($node)); |
|
49
|
|
|
$classOrFunction = $this->metrics->get($name); |
|
50
|
|
|
} else { |
|
51
|
|
|
$name = (string)$node->name; |
|
52
|
|
|
$classOrFunction = new FunctionMetric($name); |
|
53
|
|
|
$this->metrics->attach($classOrFunction); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($classOrFunction === null) { |
|
57
|
|
|
throw new MetricNullException($name, self::class); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// search for operands and operators |
|
61
|
|
|
$operands = []; |
|
62
|
|
|
$operators = []; |
|
63
|
|
|
|
|
64
|
|
|
iterate_over_node($node, function ($node) use (&$operators, &$operands) { |
|
65
|
|
|
if ($node instanceof Node\Expr\BinaryOp |
|
66
|
|
|
|| $node instanceof Node\Expr\AssignOp |
|
67
|
|
|
|| $node instanceof Stmt\If_ |
|
68
|
|
|
|| $node instanceof Stmt\For_ |
|
69
|
|
|
|| $node instanceof Stmt\Switch_ |
|
70
|
|
|
|| $node instanceof Stmt\Catch_ |
|
71
|
|
|
|| $node instanceof Stmt\Return_ |
|
72
|
|
|
|| $node instanceof Stmt\While_ |
|
73
|
|
|
|| $node instanceof Node\Expr\Assign |
|
74
|
|
|
) { |
|
75
|
|
|
// operators |
|
76
|
|
|
array_push($operators, get_class($node)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// nicik/php-parser:^4 |
|
80
|
|
|
if ($node instanceof Node\Param |
|
81
|
|
|
&& isset($node->var) |
|
82
|
|
|
&& $node->var instanceof Node\Expr\Variable |
|
83
|
|
|
) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ($node instanceof Node\Expr\Cast |
|
88
|
|
|
|| $node instanceof Node\Expr\Variable |
|
89
|
|
|
|| $node instanceof Node\Param |
|
90
|
|
|
|| $node instanceof Node\Scalar |
|
91
|
|
|
) { |
|
92
|
|
|
// operands |
|
93
|
|
|
if (isset($node->value)) { |
|
94
|
|
|
$name = $node->value; |
|
95
|
|
|
} elseif (isset($node->name)) { |
|
96
|
|
|
$name = $node->name; |
|
97
|
|
|
} else { |
|
98
|
|
|
$name = get_class($node); |
|
99
|
|
|
} |
|
100
|
|
|
array_push($operands, $name); |
|
101
|
|
|
} |
|
102
|
|
|
}); |
|
103
|
|
|
|
|
104
|
|
|
// calculate halstead metrics |
|
105
|
|
|
$uniqueOperators = array_map('unserialize', array_unique(array_map('serialize', $operators))); |
|
106
|
|
|
$uniqueOperands = array_map('unserialize', array_unique(array_map('serialize', $operands))); |
|
107
|
|
|
|
|
108
|
|
|
$n1 = count($uniqueOperators, COUNT_NORMAL); |
|
109
|
|
|
$n2 = count($uniqueOperands, COUNT_NORMAL); |
|
110
|
|
|
$N1 = count($operators, COUNT_NORMAL); |
|
111
|
|
|
$N2 = count($operands, COUNT_NORMAL); |
|
112
|
|
|
|
|
113
|
|
|
if (($n2 == 0) || ($N2 == 0)) { |
|
114
|
|
|
// files without operators |
|
115
|
|
|
$V = $n1 = $n2 = $N1 = $N2 = $E = $D = $B = $T = $I = $L = 0; |
|
116
|
|
|
} else { |
|
117
|
|
|
$devAbility = 3000; |
|
118
|
|
|
$N = $N1 + $N2; |
|
119
|
|
|
$n = $n1 + $n2; |
|
120
|
|
|
$V = $N * log($n, 2); |
|
121
|
|
|
$L = (2 / max(1, $n1)) * ($n2 / $N2); |
|
122
|
|
|
$D = ($n1 / 2) * ($N2 / $n2); |
|
123
|
|
|
$E = $V * $D; |
|
124
|
|
|
$B = $V / $devAbility; |
|
125
|
|
|
$T = $E / 18; |
|
126
|
|
|
$I = $L * $V; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// save result |
|
130
|
|
|
$classOrFunction |
|
131
|
|
|
->set('length', $N1 + $N2) |
|
132
|
|
|
->set('vocabulary', $n1 + $n2) |
|
133
|
|
|
->set('volume', round($V, 2)) |
|
134
|
|
|
->set('difficulty', round($D, 2)) |
|
135
|
|
|
->set('effort', round($E, 2)) |
|
136
|
|
|
->set('level', round($L, 2)) |
|
137
|
|
|
->set('bugs', round($B, 2)) |
|
138
|
|
|
->set('time', round($T)) |
|
139
|
|
|
->set('intelligentContent', round($I, 2)) |
|
140
|
|
|
->set('number_operators', $N1) |
|
141
|
|
|
->set('number_operands', $N2) |
|
142
|
|
|
->set('number_operators_unique', $n1) |
|
143
|
|
|
->set('number_operands_unique', $n2); |
|
144
|
|
|
$this->metrics->attach($classOrFunction); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return null; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
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.