Skip to content

Commit a29192b

Browse files
author
MarkBaker
committed
Factor match expression into the CyclomaticComplexity calculation
This is for Issue #3472, which identified that match() was not being included in the count, when it should have been (similar to a switch statement). There is no need to add one for the T_MATCH_DEFAULT token, because this is always followed by a T_MATCH_ARROW token, and to do so would be to double-count that entry. Unit tests provided.
1 parent 9553ab6 commit a29192b

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function process(File $phpcsFile, $stackPtr)
8484
T_INLINE_THEN => true,
8585
T_COALESCE => true,
8686
T_COALESCE_EQUAL => true,
87+
T_MATCH_ARROW => true,
8788
];
8889

8990
$complexity = 1;

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,35 @@ function complexityElevenWithNullCoalescenceAssignment()
402402
}
403403
}
404404

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+
405436
?>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function getWarningList()
4848
285 => 1,
4949
333 => 1,
5050
381 => 1,
51+
417 => 1,
5152
];
5253

5354
}//end getWarningList()

0 commit comments

Comments
 (0)