Skip to content

Commit d5359c7

Browse files
author
MarkBaker
committed
Include NullSafe operator in calculation for Cyclomatic Complexity Sniff, with appropriate unit tests
1 parent 6296b27 commit d5359c7

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,19 @@ public function process(File $phpcsFile, $stackPtr)
7171

7272
// Predicate nodes for PHP.
7373
$find = [
74-
T_CASE => true,
75-
T_DEFAULT => true,
76-
T_CATCH => true,
77-
T_IF => true,
78-
T_FOR => true,
79-
T_FOREACH => true,
80-
T_WHILE => true,
81-
T_ELSEIF => true,
82-
T_INLINE_THEN => true,
83-
T_COALESCE => true,
84-
T_COALESCE_EQUAL => true,
85-
T_MATCH_ARROW => true,
74+
T_CASE => true,
75+
T_DEFAULT => true,
76+
T_CATCH => true,
77+
T_IF => true,
78+
T_FOR => true,
79+
T_FOREACH => true,
80+
T_WHILE => true,
81+
T_ELSEIF => true,
82+
T_INLINE_THEN => true,
83+
T_COALESCE => true,
84+
T_COALESCE_EQUAL => true,
85+
T_MATCH_ARROW => true,
86+
T_NULLSAFE_OBJECT_OPERATOR => true,
8687
];
8788

8889
$complexity = 1;

src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,4 +433,22 @@ function complexityFourteenWithMatch()
433433
};
434434
}
435435

436+
437+
function complexitySevenWithNullSafeOperator()
438+
{
439+
$foo = $object1->getX()?->getY()?->getZ();
440+
$bar = $object2->getX()?->getY()?->getZ();
441+
$baz = $object3->getX()?->getY()?->getZ();
442+
}
443+
444+
445+
function complexityElevenWithNullSafeOperator()
446+
{
447+
$foo = $object1->getX()?->getY()?->getZ();
448+
$bar = $object2->getX()?->getY()?->getZ();
449+
$baz = $object3->getX()?->getY()?->getZ();
450+
$bacon = $object4->getX()?->getY()?->getZ();
451+
$bits = $object5->getX()?->getY()?->getZ();
452+
}
453+
436454
?>

src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function getWarningList()
4949
333 => 1,
5050
381 => 1,
5151
417 => 1,
52+
445 => 1,
5253
];
5354

5455
}//end getWarningList()

0 commit comments

Comments
 (0)