Skip to content

Commit 454ec64

Browse files
committed
Merge branch 'CyclomaticComplexity-Updates-and-Fixes' of https://github.com/MarkBaker/PHP_CodeSniffer
2 parents 8884d16 + a29192b commit 454ec64

File tree

3 files changed

+305
-18
lines changed

3 files changed

+305
-18
lines changed

src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,20 @@ public function process(File $phpcsFile, $stackPtr)
7171

7272
// Predicate nodes for PHP.
7373
$find = [
74-
T_CASE => true,
75-
T_DEFAULT => true,
76-
T_CATCH => true,
77-
T_IF => true,
78-
T_FOR => true,
79-
T_FOREACH => true,
80-
T_WHILE => true,
81-
T_DO => true,
82-
T_ELSEIF => true,
74+
T_CASE => true,
75+
T_DEFAULT => true,
76+
T_CATCH => true,
77+
T_IF => true,
78+
T_FOR => true,
79+
T_FOREACH => true,
80+
T_WHILE => true,
81+
// T_DO is not required for incrementing CYC, as the terminating while in a do/while loop triggers the branch.
82+
// T_DO => true.
83+
T_ELSEIF => true,
84+
T_INLINE_THEN => true,
85+
T_COALESCE => true,
86+
T_COALESCE_EQUAL => true,
87+
T_MATCH_ARROW => true,
8388
];
8489

8590
$complexity = 1;

src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.inc

Lines changed: 282 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ function complexityTwenty()
7979

8080
switch ($condition) {
8181
case '1':
82-
if ($condition) {
83-
} else if ($cond) {
84-
}
82+
do {
83+
if ($condition) {
84+
} else if ($cond) {
85+
}
86+
} while ($cond);
8587
break;
8688
case '2':
8789
while ($cond) {
@@ -116,9 +118,11 @@ function complexityTwenty()
116118
function complexityTwentyOne()
117119
{
118120
while ($condition === true) {
119-
if ($condition) {
120-
} else if ($cond) {
121-
}
121+
do {
122+
if ($condition) {
123+
} else if ($cond) {
124+
}
125+
} while ($cond);
122126
}
123127

124128
switch ($condition) {
@@ -157,4 +161,276 @@ function complexityTwentyOne()
157161
}
158162
}
159163

164+
165+
function complexityTenWithTernaries()
166+
{
167+
$value1 = (empty($condition1)) ? $value1A : $value1B;
168+
$value2 = (empty($condition2)) ? $value2A : $value2B;
169+
170+
switch ($condition) {
171+
case '1':
172+
if ($condition) {
173+
} else if ($cond) {
174+
}
175+
break;
176+
case '2':
177+
while ($cond) {
178+
echo 'hi';
179+
}
180+
break;
181+
case '3':
182+
break;
183+
default:
184+
break;
185+
}
186+
}
187+
188+
189+
function complexityElevenWithTernaries()
190+
{
191+
$value1 = (empty($condition1)) ? $value1A : $value1B;
192+
$value2 = (empty($condition2)) ? $value2A : $value2B;
193+
$value3 = (empty($condition3)) ? $value3A : $value3B;
194+
195+
switch ($condition) {
196+
case '1':
197+
if ($condition) {
198+
} else if ($cond) {
199+
}
200+
break;
201+
case '2':
202+
while ($cond) {
203+
echo 'hi';
204+
}
205+
break;
206+
case '3':
207+
break;
208+
default:
209+
break;
210+
}
211+
}
212+
213+
214+
function complexityTenWithNestedTernaries()
215+
{
216+
$value1 = true ? $value1A : false ? $value1B : $value1C;
217+
218+
switch ($condition) {
219+
case '1':
220+
if ($condition) {
221+
} else if ($cond) {
222+
}
223+
break;
224+
case '2':
225+
while ($cond) {
226+
echo 'hi';
227+
}
228+
break;
229+
case '3':
230+
break;
231+
default:
232+
break;
233+
}
234+
}
235+
236+
237+
function complexityElevenWithNestedTernaries()
238+
{
239+
$value1 = (empty($condition1)) ? $value1A : $value1B;
240+
$value2 = true ? $value2A : false ? $value2B : $value2C;
241+
242+
switch ($condition) {
243+
case '1':
244+
if ($condition) {
245+
} else if ($cond) {
246+
}
247+
break;
248+
case '2':
249+
while ($cond) {
250+
echo 'hi';
251+
}
252+
break;
253+
case '3':
254+
break;
255+
default:
256+
break;
257+
}
258+
}
259+
260+
261+
function complexityTenWithNullCoalescence()
262+
{
263+
$value1 = $value1A ?? $value1B;
264+
$value2 = $value2A ?? $value2B;
265+
266+
switch ($condition) {
267+
case '1':
268+
if ($condition) {
269+
} else if ($cond) {
270+
}
271+
break;
272+
case '2':
273+
while ($cond) {
274+
echo 'hi';
275+
}
276+
break;
277+
case '3':
278+
break;
279+
default:
280+
break;
281+
}
282+
}
283+
284+
285+
function complexityElevenWithNullCoalescence()
286+
{
287+
$value1 = $value1A ?? $value1B;
288+
$value2 = $value2A ?? $value2B;
289+
$value3 = $value3A ?? $value3B;
290+
291+
switch ($condition) {
292+
case '1':
293+
if ($condition) {
294+
} else if ($cond) {
295+
}
296+
break;
297+
case '2':
298+
while ($cond) {
299+
echo 'hi';
300+
}
301+
break;
302+
case '3':
303+
break;
304+
default:
305+
break;
306+
}
307+
}
308+
309+
310+
function complexityTenWithNestedNullCoalescence()
311+
{
312+
$value1 = $value1A ?? $value1B ?? $value1C;
313+
314+
switch ($condition) {
315+
case '1':
316+
if ($condition) {
317+
} else if ($cond) {
318+
}
319+
break;
320+
case '2':
321+
while ($cond) {
322+
echo 'hi';
323+
}
324+
break;
325+
case '3':
326+
break;
327+
default:
328+
break;
329+
}
330+
}
331+
332+
333+
function complexityElevenWithNestedNullCoalescence()
334+
{
335+
$value1 = $value1A ?? $value1B;
336+
$value2 = $value2A ?? $value2B ?? $value2C;
337+
338+
switch ($condition) {
339+
case '1':
340+
if ($condition) {
341+
} else if ($cond) {
342+
}
343+
break;
344+
case '2':
345+
while ($cond) {
346+
echo 'hi';
347+
}
348+
break;
349+
case '3':
350+
break;
351+
default:
352+
break;
353+
}
354+
}
355+
356+
357+
function complexityTenWithNullCoalescenceAssignment()
358+
{
359+
$value1 ??= $default1;
360+
$value2 ??= $default2;
361+
362+
switch ($condition) {
363+
case '1':
364+
if ($condition) {
365+
} else if ($cond) {
366+
}
367+
break;
368+
case '2':
369+
while ($cond) {
370+
echo 'hi';
371+
}
372+
break;
373+
case '3':
374+
break;
375+
default:
376+
break;
377+
}
378+
}
379+
380+
381+
function complexityElevenWithNullCoalescenceAssignment()
382+
{
383+
$value1 ??= $default1;
384+
$value2 ??= $default2;
385+
$value3 ??= $default3;
386+
387+
switch ($condition) {
388+
case '1':
389+
if ($condition) {
390+
} else if ($cond) {
391+
}
392+
break;
393+
case '2':
394+
while ($cond) {
395+
echo 'hi';
396+
}
397+
break;
398+
case '3':
399+
break;
400+
default:
401+
break;
402+
}
403+
}
404+
405+
406+
function complexityFiveWithMatch()
407+
{
408+
return match(strtolower(substr($monthName, 0, 3))){
409+
'apr', 'jun', 'sep', 'nov' => 30,
410+
'jan', 'mar', 'may', 'jul', 'aug', 'oct', 'dec' => 31,
411+
'feb' => is_leap_year($year) ? 29 : 28,
412+
default => throw new InvalidArgumentException("Invalid month"),
413+
}
414+
}
415+
416+
417+
function complexityFourteenWithMatch()
418+
{
419+
return match(strtolower(substr($monthName, 0, 3))) {
420+
'jan' => 31,
421+
'feb' => is_leap_year($year) ? 29 : 28,
422+
'mar' => 31,
423+
'apr' => 30,
424+
'may' => 31,
425+
'jun' => 30,
426+
'jul' => 31,
427+
'aug' => 31,
428+
'sep' => 30,
429+
'oct' => 31,
430+
'nov' => 30,
431+
'dec' => 31,
432+
default => throw new InvalidArgumentException("Invalid month"),
433+
};
434+
}
435+
160436
?>

src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CyclomaticComplexityUnitTest extends AbstractSniffUnitTest
2525
*/
2626
public function getErrorList()
2727
{
28-
return [116 => 1];
28+
return [118 => 1];
2929

3030
}//end getErrorList()
3131

@@ -41,8 +41,14 @@ public function getErrorList()
4141
public function getWarningList()
4242
{
4343
return [
44-
45 => 1,
45-
72 => 1,
44+
45 => 1,
45+
72 => 1,
46+
189 => 1,
47+
237 => 1,
48+
285 => 1,
49+
333 => 1,
50+
381 => 1,
51+
417 => 1,
4652
];
4753

4854
}//end getWarningList()

0 commit comments

Comments
 (0)