Skip to content

Commit b698f0c

Browse files
committed
Modernize: Generic/CyclomaticComplexity: use class constant for constant array
1 parent 1cdc2c2 commit b698f0c

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020
class CyclomaticComplexitySniff implements Sniff
2121
{
2222

23+
/**
24+
* Predicate nodes for PHP.
25+
*
26+
* @var array<int|string, true>
27+
*/
28+
private const PREDICATE_NODES = [
29+
T_CASE => true,
30+
T_DEFAULT => true,
31+
T_CATCH => true,
32+
T_IF => true,
33+
T_FOR => true,
34+
T_FOREACH => true,
35+
T_WHILE => true,
36+
T_ELSEIF => true,
37+
T_INLINE_THEN => true,
38+
T_COALESCE => true,
39+
T_COALESCE_EQUAL => true,
40+
T_MATCH_ARROW => true,
41+
T_NULLSAFE_OBJECT_OPERATOR => true,
42+
];
43+
2344
/**
2445
* A complexity higher than this value will throw a warning.
2546
*
@@ -69,28 +90,11 @@ public function process(File $phpcsFile, $stackPtr)
6990
$start = $tokens[$stackPtr]['scope_opener'];
7091
$end = $tokens[$stackPtr]['scope_closer'];
7192

72-
// Predicate nodes for PHP.
73-
$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,
86-
T_NULLSAFE_OBJECT_OPERATOR => true,
87-
];
88-
8993
$complexity = 1;
9094

9195
// Iterate from start to end and count predicate nodes.
9296
for ($i = ($start + 1); $i < $end; $i++) {
93-
if (isset($find[$tokens[$i]['code']]) === true) {
97+
if (isset(self::PREDICATE_NODES[$tokens[$i]['code']]) === true) {
9498
$complexity++;
9599
}
96100
}

0 commit comments

Comments
 (0)