1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Churn\Assessors\CyclomaticComplexity; |
4
|
|
|
|
5
|
|
|
class CyclomaticComplexityAssessor |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The total cyclomatic complexity score. |
9
|
|
|
* @var integer. |
10
|
|
|
*/ |
11
|
|
|
protected $score; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Asses the files cyclomatic complexity. |
15
|
|
|
* @param string $filePath Path and file name. |
16
|
|
|
* @return integer |
17
|
|
|
*/ |
18
|
|
|
public function assess(string $filePath): int |
19
|
|
|
{ |
20
|
|
|
$this->score = 0; |
21
|
|
|
|
22
|
|
|
$contents = $this->getFileContents($filePath); |
23
|
|
|
$this->countTheMethods($contents); |
24
|
|
|
$this->countTheIfStatements($contents); |
25
|
|
|
$this->countTheElseIfStatements($contents); |
26
|
|
|
$this->countTheWhileLoops($contents); |
27
|
|
|
$this->countTheForLoops($contents); |
28
|
|
|
$this->countTheCaseStatements($contents); |
29
|
|
|
$this->countTheTernaryOperators($contents); |
30
|
|
|
$this->countTheLogicalAnds($contents); |
31
|
|
|
$this->countTheLogicalOrs($contents); |
32
|
|
|
|
33
|
|
|
if ($this->score == 0) { |
34
|
|
|
$this->score = 1; |
35
|
|
|
} |
36
|
|
|
return $this->score; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Count how many methods there are. |
41
|
|
|
* @param string $contents File contents. |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
protected function countTheMethods(string $contents) |
45
|
|
|
{ |
46
|
|
|
preg_match("/[ ]function[ ]/", $contents, $matches); |
47
|
|
|
if (isset($matches[0])) { |
48
|
|
|
$this->score ++; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Count how many if statements there are. |
54
|
|
|
* @param string $contents File contents. |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
protected function countTheIfStatements(string $contents) |
58
|
|
|
{ |
59
|
|
|
$this->score += $this->howmAnyPatternMatches("/[ ]if[ ]{0,}\(/", $contents); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Count how many else if statements there are. |
64
|
|
|
* @param string $contents File contents. |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
protected function countTheElseIfStatements(string $contents) |
68
|
|
|
{ |
69
|
|
|
$this->score += $this->howmAnyPatternMatches("/else[ ]{0,}if[ ]{0,}\(/", $contents); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Count how many while loops there are. |
74
|
|
|
* @param string $contents File contents. |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
protected function countTheWhileLoops(string $contents) |
78
|
|
|
{ |
79
|
|
|
$this->score += $this->howmAnyPatternMatches("/while[ ]{0,}\(/", $contents); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Count how many for loops there are. |
84
|
|
|
* @param string $contents File contents. |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function countTheForLoops(string $contents) |
88
|
|
|
{ |
89
|
|
|
$this->score += $this->howmAnyPatternMatches("/[ ]for(each){0,1}[ ]{0,}\(/", $contents); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Count how many case statements there are. |
94
|
|
|
* @param string $contents File contents. |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
protected function countTheCaseStatements(string $contents) |
98
|
|
|
{ |
99
|
|
|
$this->score += $this->howmAnyPatternMatches("/[ ]case[ ]{1}(.*)\:/", $contents); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Count how many ternary operators there are. |
104
|
|
|
* @param string $contents File contents. |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
protected function countTheTernaryOperators(string $contents) |
108
|
|
|
{ |
109
|
|
|
$this->score += $this->howmAnyPatternMatches("/[ ]\?.*:.*;/", $contents); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Count how many '&&' there are. |
114
|
|
|
* @param string $contents File contents. |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function countTheLogicalAnds(string $contents) |
118
|
|
|
{ |
119
|
|
|
$this->score += $this->howmAnyPatternMatches("/[ ]&&[ ]/", $contents); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Count how many '||' there are. |
124
|
|
|
* @param string $contents File contents. |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
protected function countTheLogicalOrs(string $contents) |
128
|
|
|
{ |
129
|
|
|
$this->score += $this->howmAnyPatternMatches("/[ ]\|\|[ ]/", $contents); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* For the given $pattern on $string, how many matches are returned? |
134
|
|
|
* @param string $pattern Regex pattern. |
135
|
|
|
* @param string $string Any string. |
136
|
|
|
* @return integer |
137
|
|
|
*/ |
138
|
|
|
protected function howManyPatternMatches(string $pattern, string $string): int |
139
|
|
|
{ |
140
|
|
|
preg_match_all($pattern, $string, $matches); |
141
|
|
|
if (isset($matches[0])) { |
142
|
|
|
return count($matches[0]); |
143
|
|
|
} |
144
|
|
|
return 0; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Return the contents of the provided file at $filePath. |
149
|
|
|
* @param string $filePath Path and filename. |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function getFileContents(string $filePath): string |
153
|
|
|
{ |
154
|
|
|
return file_get_contents($filePath); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|