Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Calculate CYC for PHP 8 match() expression in Cyclomatic Complexity sniff
  • Loading branch information
MarkBaker committed Nov 12, 2021
1 parent 5fb9b64 commit 2989b0d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 176 deletions.
19 changes: 10 additions & 9 deletions src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@ public function process(File $phpcsFile, $stackPtr)

// Predicate nodes for PHP.
$find = [
T_CASE => true,
T_DEFAULT => true,
T_CATCH => true,
T_IF => true,
T_FOR => true,
T_FOREACH => true,
T_WHILE => true,
T_DO => true,
T_ELSEIF => true,
T_CASE => true,
T_DEFAULT => true,
T_CATCH => true,
T_IF => true,
T_FOR => true,
T_FOREACH => true,
T_WHILE => true,
T_DO => true,
T_ELSEIF => true,
T_MATCH_ARROW => true,
];

$complexity = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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) ? 29 : 28,
default => throw new InvalidArgumentException("Bogus month"),
}
}

function complexityFourteenWithMatch()
{
return match(strtolower(substr($monthName, 0, 3))) {
'jan' => 31,
'feb' => is_leap($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("Bogus month"),
};
}

?>
160 changes: 0 additions & 160 deletions src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.inc

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ class CyclomaticComplexityUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [116 => 1];
switch ($testFile) {
case 'CyclomaticComplexityUnitTest.3.inc':
default:
return [];
break;
}//end switch

}//end getErrorList()

Expand All @@ -36,14 +43,19 @@ public function getErrorList()
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getWarningList()
public function getWarningList($testFile='')
{
return [
45 => 1,
72 => 1,
];
switch ($testFile) {
case 'CyclomaticComplexityUnitTest.3.inc':
return [13 => 1];
default:
return [];
break;
}//end switch

}//end getWarningList()

Expand Down

0 comments on commit 2989b0d

Please sign in to comment.