Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cyclomatic complexity updates and fixes #3495

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 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,20 @@ 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 is not required for incrementing CYC, as the terminating while in a do/while loop triggers the branch.
// T_DO => true.
T_ELSEIF => true,
T_INLINE_THEN => true,
T_COALESCE => true,
T_COALESCE_EQUAL => true,
T_MATCH_ARROW => true,
];

$complexity = 1;
Expand Down
288 changes: 282 additions & 6 deletions src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ function complexityTwenty()

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
do {
if ($condition) {
} else if ($cond) {
}
} while ($cond);
break;
case '2':
while ($cond) {
Expand Down Expand Up @@ -116,9 +118,11 @@ function complexityTwenty()
function complexityTwentyOne()
{
while ($condition === true) {
if ($condition) {
} else if ($cond) {
}
do {
if ($condition) {
} else if ($cond) {
}
} while ($cond);
}

switch ($condition) {
Expand Down Expand Up @@ -157,4 +161,276 @@ function complexityTwentyOne()
}
}


function complexityTenWithTernaries()
{
$value1 = (empty($condition1)) ? $value1A : $value1B;
$value2 = (empty($condition2)) ? $value2A : $value2B;

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
break;
case '2':
while ($cond) {
echo 'hi';
}
break;
case '3':
break;
default:
break;
}
}


function complexityElevenWithTernaries()
{
$value1 = (empty($condition1)) ? $value1A : $value1B;
$value2 = (empty($condition2)) ? $value2A : $value2B;
$value3 = (empty($condition3)) ? $value3A : $value3B;

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
break;
case '2':
while ($cond) {
echo 'hi';
}
break;
case '3':
break;
default:
break;
}
}


function complexityTenWithNestedTernaries()
{
$value1 = true ? $value1A : false ? $value1B : $value1C;

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
break;
case '2':
while ($cond) {
echo 'hi';
}
break;
case '3':
break;
default:
break;
}
}


function complexityElevenWithNestedTernaries()
{
$value1 = (empty($condition1)) ? $value1A : $value1B;
$value2 = true ? $value2A : false ? $value2B : $value2C;

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
break;
case '2':
while ($cond) {
echo 'hi';
}
break;
case '3':
break;
default:
break;
}
}


function complexityTenWithNullCoalescence()
{
$value1 = $value1A ?? $value1B;
$value2 = $value2A ?? $value2B;

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
break;
case '2':
while ($cond) {
echo 'hi';
}
break;
case '3':
break;
default:
break;
}
}


function complexityElevenWithNullCoalescence()
{
$value1 = $value1A ?? $value1B;
$value2 = $value2A ?? $value2B;
$value3 = $value3A ?? $value3B;

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
break;
case '2':
while ($cond) {
echo 'hi';
}
break;
case '3':
break;
default:
break;
}
}


function complexityTenWithNestedNullCoalescence()
{
$value1 = $value1A ?? $value1B ?? $value1C;

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
break;
case '2':
while ($cond) {
echo 'hi';
}
break;
case '3':
break;
default:
break;
}
}


function complexityElevenWithNestedNullCoalescence()
{
$value1 = $value1A ?? $value1B;
$value2 = $value2A ?? $value2B ?? $value2C;

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
break;
case '2':
while ($cond) {
echo 'hi';
}
break;
case '3':
break;
default:
break;
}
}


function complexityTenWithNullCoalescenceAssignment()
{
$value1 ??= $default1;
$value2 ??= $default2;

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
break;
case '2':
while ($cond) {
echo 'hi';
}
break;
case '3':
break;
default:
break;
}
}


function complexityElevenWithNullCoalescenceAssignment()
{
$value1 ??= $default1;
$value2 ??= $default2;
$value3 ??= $default3;

switch ($condition) {
case '1':
if ($condition) {
} else if ($cond) {
}
break;
case '2':
while ($cond) {
echo 'hi';
}
break;
case '3':
break;
default:
break;
}
}


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 @@ -25,7 +25,7 @@ class CyclomaticComplexityUnitTest extends AbstractSniffUnitTest
*/
public function getErrorList()
{
return [116 => 1];
return [118 => 1];

}//end getErrorList()

Expand All @@ -41,8 +41,14 @@ public function getErrorList()
public function getWarningList()
{
return [
45 => 1,
72 => 1,
45 => 1,
72 => 1,
189 => 1,
237 => 1,
285 => 1,
333 => 1,
381 => 1,
417 => 1,
];

}//end getWarningList()
Expand Down