|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hal\Metric\Class_\Structural; |
|
3
|
|
|
|
|
4
|
|
|
use Hal\Metric\Metrics; |
|
5
|
|
|
use PhpParser\Node; |
|
6
|
|
|
use PhpParser\Node\Stmt; |
|
7
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Calculates Card And Agresti metric |
|
11
|
|
|
* |
|
12
|
|
|
* Fan-out = Structural fan-out = Number of other procedures this procedure calls |
|
13
|
|
|
* v = number of input/output variables for a procedure |
|
14
|
|
|
* |
|
15
|
|
|
* (SC) Structural complexity = fan-out^2 |
|
16
|
|
|
* (DC) Data complexity = v / (fan-out + 1) |
|
17
|
|
|
* |
|
18
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
|
19
|
|
|
*/ |
|
20
|
|
|
class SystemComplexityVisitor extends NodeVisitorAbstract |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Metrics |
|
25
|
|
|
*/ |
|
26
|
|
|
private $metrics; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* ClassEnumVisitor constructor. |
|
30
|
|
|
* @param Metrics $metrics |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Metrics $metrics) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->metrics = $metrics; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritdoc |
|
39
|
|
|
*/ |
|
40
|
|
|
public function leaveNode(Node $node) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($node instanceof Stmt\Class_) { |
|
43
|
|
|
|
|
44
|
|
|
$class = $this->metrics->get($node->namespacedName->toString()); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$sy = $dc = $sc = array(); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($node->stmts as $stmt) { |
|
49
|
|
|
if ($stmt instanceof Stmt\ClassMethod) { |
|
50
|
|
|
|
|
51
|
|
|
// number of returns and calls |
|
52
|
|
|
$output = 0; |
|
53
|
|
|
$fanout = []; |
|
54
|
|
|
|
|
55
|
|
|
iterate_over_node($node, function ($node) use (&$output, &$fanout) { |
|
56
|
|
View Code Duplication |
switch (true) { |
|
|
|
|
|
|
57
|
|
|
case $node instanceof Stmt\Return_: |
|
58
|
|
|
$output++; |
|
59
|
|
|
break; |
|
60
|
|
|
case $node instanceof Node\Expr\StaticCall: |
|
61
|
|
|
case $node instanceof Node\Expr\MethodCall: |
|
62
|
|
|
array_push($fanout, getNameOfNode($node)); |
|
63
|
|
|
} |
|
64
|
|
|
}); |
|
65
|
|
|
|
|
66
|
|
|
$fanout = sizeof(array_unique($fanout)); |
|
67
|
|
|
$v = sizeof($stmt->params) + $output; |
|
68
|
|
|
$ldc = $v / ($fanout + 1); |
|
69
|
|
|
$lsc = pow($fanout, 2); |
|
70
|
|
|
$sy[] = $ldc + $lsc; |
|
71
|
|
|
$dc[] = $ldc; |
|
72
|
|
|
$sc[] = $lsc; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// average for class |
|
77
|
|
|
$class |
|
|
|
|
|
|
78
|
|
|
->set('relativeStructuralComplexity', empty($sc) ? 0 : round(array_sum($sc) / sizeof($sc), 2)) |
|
79
|
|
|
->set('relativeDataComplexity', empty($dc) ? 0 : round(array_sum($dc) / sizeof($dc), 2)) |
|
80
|
|
|
->set('relativeSystemComplexity', empty($sy) ? 0 : round(array_sum($sy) / sizeof($sy), 2)) |
|
81
|
|
|
->set('totalStructuralComplexity', round(array_sum($sc), 2)) |
|
82
|
|
|
->set('totalDataComplexity', round(array_sum($dc), 2)) |
|
83
|
|
|
->set('totalSystemComplexity', round(array_sum($dc) + array_sum($sc), 2)); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.