-
Notifications
You must be signed in to change notification settings - Fork 257
/
CyclomaticComplexityVisitor.php
130 lines (116 loc) · 5.11 KB
/
CyclomaticComplexityVisitor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Hal\Metric\Class_\Complexity;
use Hal\Metric\Helper\MetricClassNameGenerator;
use Hal\Metric\Helper\RoleOfMethodDetector;
use Hal\Metric\Metrics;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\NodeVisitorAbstract;
/**
* Calculate cyclomatic complexity number and weighted method count.
*
* The cyclomatic complexity (CCN) is a measure of control structure complexity of a function or procedure.
* We can calculate ccn in two ways (we choose the second):
*
* 1. Cyclomatic complexity (CCN) = E - N + 2P
* Where:
* P = number of disconnected parts of the flow graph (e.g. a calling program and a subroutine)
* E = number of edges (transfers of control)
* N = number of nodes (sequential group of statements containing only one transfer of control)
*
* 2. CCN = Number of each decision point
*
* The weighted method count (WMC) is count of methods parameterized by a algorithm to compute the weight of a method.
* Given a weight metric w and methods m it can be computed as
*
* sum m(w') over (w' in w)
*
* Possible algorithms are:
*
* - Cyclomatic Complexity
* - Lines of Code
* - 1 (unweighted WMC)
*
* This visitor provides two metrics, the maximal CCN of all methods from one class (currently stored as ccnMethodMax)
* and the WMC using the CCN as weight metric (currently stored as ccn).
*
* @see https://en.wikipedia.org/wiki/Cyclomatic_complexity
* @see http://www.literateprogramming.com/mccabe.pdf
* @see https://www.pitt.edu/~ckemerer/CK%20research%20papers/MetricForOOD_ChidamberKemerer94.pdf
*/
class CyclomaticComplexityVisitor extends NodeVisitorAbstract
{
/** @var Metrics */
private $metrics;
public function __construct(Metrics $metrics)
{
$this->metrics = $metrics;
}
public function leaveNode(Node $node)
{
if ($node instanceof Stmt\Class_
|| $node instanceof Stmt\Interface_
|| $node instanceof Stmt\Trait_
) {
$class = $this->metrics->get(MetricClassNameGenerator::getName($node));
$ccn = 1;
$wmc = 0;
$ccnByMethod = [0]; // default maxMethodCcn if no methods are available
$roleDetector = new RoleOfMethodDetector();
foreach ($node->stmts as $stmt) {
if ($stmt instanceof Stmt\ClassMethod) {
$role = $roleDetector->detects($stmt);
if (in_array($role, ['getter', 'setter'])) {
// We don't want to increase the CCN for getters and setters,
continue;
}
// iterate over children, recursively
$cb = function ($node) use (&$cb) {
$ccn = 0;
foreach (get_object_vars($node) as $name => $member) {
foreach (is_array($member) ? $member : [$member] as $memberItem) {
if ($memberItem instanceof Node) {
$ccn += $cb($memberItem);
}
}
}
switch (true) {
case $node instanceof Stmt\If_:
case $node instanceof Stmt\ElseIf_:
case $node instanceof Stmt\For_:
case $node instanceof Stmt\Foreach_:
case $node instanceof Stmt\While_:
case $node instanceof Stmt\Do_:
case $node instanceof Node\Expr\BinaryOp\LogicalAnd:
case $node instanceof Node\Expr\BinaryOp\LogicalOr:
case $node instanceof Node\Expr\BinaryOp\LogicalXor:
case $node instanceof Node\Expr\BinaryOp\BooleanAnd:
case $node instanceof Node\Expr\BinaryOp\BooleanOr:
case $node instanceof Stmt\Catch_:
case $node instanceof Node\Expr\Ternary:
case $node instanceof Node\Expr\BinaryOp\Coalesce:
$ccn++;
break;
case $node instanceof Stmt\Case_: // include default
if ($node->cond !== null) { // exclude default
$ccn++;
}
break;
case $node instanceof Node\Expr\BinaryOp\Spaceship:
$ccn += 2;
break;
}
return $ccn;
};
$methodCcn = $cb($stmt) + 1; // each method by default is CCN 1 even if it's empty
$wmc += $methodCcn;
$ccn += $methodCcn - 1;
$ccnByMethod[] = $methodCcn;
}
}
$class->set('wmc', $wmc);
$class->set('ccn', $ccn);
$class->set('ccnMethodMax', max($ccnByMethod));
}
}
}