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 12, 2021
1 parent 5fb9b64 commit b666b58
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 176 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
234 changes: 234 additions & 0 deletions src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
<?php

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

?>
Loading

0 comments on commit b666b58

Please sign in to comment.