Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add ternary operator, null coalescence operator and null coalescence assignment operator to tokens that trigger an increase in cyclomatic complexity
  • Loading branch information
MarkBaker committed Nov 11, 2021
1 parent 5fb9b64 commit 0705fc5
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 9 deletions.
21 changes: 12 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,18 @@ 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_INLINE_THEN => true,
T_COALESCE => true,
T_COALESCE_EQUAL => true,
];

$complexity = 1;
Expand Down
118 changes: 118 additions & 0 deletions src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,122 @@ function complexityTwentyOne()
}
}

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 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 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 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 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;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function getWarningList()
return [
45 => 1,
72 => 1,
160 => 1,
184 => 1,
207 => 1,
231 => 1,
254 => 1,
];

}//end getWarningList()
Expand Down

0 comments on commit 0705fc5

Please sign in to comment.