Skip to content

Commit

Permalink
Factor match expression into the CyclomaticComplexity calculation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MarkBaker committed Dec 3, 2021
1 parent 9553ab6 commit a29192b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function process(File $phpcsFile, $stackPtr)
T_INLINE_THEN => true,
T_COALESCE => true,
T_COALESCE_EQUAL => true,
T_MATCH_ARROW => true,
];

$complexity = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,35 @@ function complexityElevenWithNullCoalescenceAssignment()
}
}


function complexityFiveWithMatch()
{
return match(strtolower(substr($monthName, 0, 3))){
'apr', 'jun', 'sep', 'nov' => 30,
'jan', 'mar', 'may', 'jul', 'aug', 'oct', 'dec' => 31,
'feb' => is_leap_year($year) ? 29 : 28,
default => throw new InvalidArgumentException("Invalid month"),
}
}


function complexityFourteenWithMatch()
{
return match(strtolower(substr($monthName, 0, 3))) {
'jan' => 31,
'feb' => is_leap_year($year) ? 29 : 28,
'mar' => 31,
'apr' => 30,
'may' => 31,
'jun' => 30,
'jul' => 31,
'aug' => 31,
'sep' => 30,
'oct' => 31,
'nov' => 30,
'dec' => 31,
default => throw new InvalidArgumentException("Invalid month"),
};
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function getWarningList()
285 => 1,
333 => 1,
381 => 1,
417 => 1,
];

}//end getWarningList()
Expand Down

0 comments on commit a29192b

Please sign in to comment.