File tree Expand file tree Collapse file tree 1 file changed +22
-18
lines changed
src/Standards/Generic/Sniffs/Metrics Expand file tree Collapse file tree 1 file changed +22
-18
lines changed Original file line number Diff line number Diff line change 2020class 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 }
You can’t perform that action at this time.
0 commit comments