|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hal\Metric\Class_\Structural; |
|
3
|
|
|
|
|
4
|
|
|
use Hal\Metric\Helper\MetricClassNameGenerator; |
|
5
|
|
|
use Hal\Metric\Metrics; |
|
6
|
|
|
use PhpParser\Node; |
|
7
|
|
|
use PhpParser\Node\Stmt; |
|
8
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Calculates Card And Agresti metric |
|
12
|
|
|
* |
|
13
|
|
|
* Fan-out = Structural fan-out = Number of other procedures this procedure calls |
|
14
|
|
|
* v = number of input/output variables for a procedure |
|
15
|
|
|
* |
|
16
|
|
|
* (SC) Structural complexity = fan-out^2 |
|
17
|
|
|
* (DC) Data complexity = v / (fan-out + 1) |
|
18
|
|
|
* |
|
19
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
|
20
|
|
|
*/ |
|
21
|
|
|
class SystemComplexityVisitor extends NodeVisitorAbstract |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Metrics |
|
26
|
|
|
*/ |
|
27
|
|
|
private $metrics; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* ClassEnumVisitor constructor. |
|
31
|
|
|
* @param Metrics $metrics |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(Metrics $metrics) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->metrics = $metrics; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public function leaveNode(Node $node) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($node instanceof Stmt\Class_) { |
|
44
|
|
|
|
|
45
|
|
|
$class = $this->metrics->get(MetricClassNameGenerator::getName($node)); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$sy = $dc = $sc = array(); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($node->stmts as $stmt) { |
|
50
|
|
|
if ($stmt instanceof Stmt\ClassMethod) { |
|
51
|
|
|
|
|
52
|
|
|
// number of returns and calls |
|
53
|
|
|
$output = 0; |
|
54
|
|
|
$fanout = []; |
|
55
|
|
|
|
|
56
|
|
|
iterate_over_node($node, function ($node) use (&$output, &$fanout) { |
|
57
|
|
View Code Duplication |
switch (true) { |
|
|
|
|
|
|
58
|
|
|
case $node instanceof Stmt\Return_: |
|
59
|
|
|
$output++; |
|
60
|
|
|
break; |
|
61
|
|
|
case $node instanceof Node\Expr\StaticCall: |
|
62
|
|
|
case $node instanceof Node\Expr\MethodCall: |
|
63
|
|
|
array_push($fanout, getNameOfNode($node)); |
|
64
|
|
|
} |
|
65
|
|
|
}); |
|
66
|
|
|
|
|
67
|
|
|
$fanout = sizeof(array_unique($fanout)); |
|
68
|
|
|
$v = sizeof($stmt->params) + $output; |
|
69
|
|
|
$ldc = $v / ($fanout + 1); |
|
70
|
|
|
$lsc = pow($fanout, 2); |
|
71
|
|
|
$sy[] = $ldc + $lsc; |
|
72
|
|
|
$dc[] = $ldc; |
|
73
|
|
|
$sc[] = $lsc; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// average for class |
|
78
|
|
|
$class |
|
|
|
|
|
|
79
|
|
|
->set('relativeStructuralComplexity', empty($sc) ? 0 : round(array_sum($sc) / sizeof($sc), 2)) |
|
80
|
|
|
->set('relativeDataComplexity', empty($dc) ? 0 : round(array_sum($dc) / sizeof($dc), 2)) |
|
81
|
|
|
->set('relativeSystemComplexity', empty($sy) ? 0 : round(array_sum($sy) / sizeof($sy), 2)) |
|
82
|
|
|
->set('totalStructuralComplexity', round(array_sum($sc), 2)) |
|
83
|
|
|
->set('totalDataComplexity', round(array_sum($dc), 2)) |
|
84
|
|
|
->set('totalSystemComplexity', round(array_sum($dc) + array_sum($sc), 2)); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.