1 | <?php declare(strict_types = 1); |
||
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 |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
111 | |||
112 | /** |
||
113 | * Count how many '&&' there are. |
||
114 | * @param string $contents File contents. |
||
115 | * @return void |
||
116 | */ |
||
117 | protected function countTheLogicalAnds(string $contents) |
||
121 | |||
122 | /** |
||
123 | * Count how many '||' there are. |
||
124 | * @param string $contents File contents. |
||
125 | * @return void |
||
126 | */ |
||
127 | protected function countTheLogicalOrs(string $contents) |
||
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 |
||
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 |
||
156 | } |
||
157 |