@@ -51,50 +51,52 @@ public function process(File $phpcsFile, $stackPtr)
51
51
$ token = $ tokens [$ stackPtr ];
52
52
53
53
$ start = $ phpcsFile ->findStartOfStatement ($ stackPtr );
54
- $ end = $ phpcsFile ->findEndOfStatement ($ stackPtr );
55
-
56
- $ nearestParens = null ;
57
- if (isset ($ token ['nested_parenthesis ' ]) === true ) {
58
- $ parens = $ token ['nested_parenthesis ' ];
59
- end ($ parens );
60
- $ nearestParens = key ($ parens );
61
-
62
- $ start = $ nearestParens ;
63
- $ end = $ parens [$ start ];
64
- }
65
-
66
- $ nearestBracket = $ phpcsFile ->findPrevious (T_OPEN_SQUARE_BRACKET , $ stackPtr );
67
54
68
55
if ($ token ['code ' ] === T_BOOLEAN_AND ) {
69
- $ search = [ T_BOOLEAN_OR ] ;
56
+ $ search = T_BOOLEAN_OR ;
70
57
} else if ($ token ['code ' ] === T_BOOLEAN_OR ) {
71
- $ search = [ T_BOOLEAN_AND ] ;
58
+ $ search = T_BOOLEAN_AND ;
72
59
} else {
73
60
throw new \LogicException ('Unreachable ' );
74
61
}
75
62
76
- do {
77
- $ found = $ phpcsFile ->findNext ($ search , $ start , $ end );
78
- if ($ found === false ) {
63
+ while (true ) {
64
+ $ previous = $ phpcsFile ->findPrevious (
65
+ [
66
+ $ search ,
67
+ T_OPEN_PARENTHESIS ,
68
+ T_OPEN_SQUARE_BRACKET ,
69
+ T_CLOSE_PARENTHESIS ,
70
+ T_CLOSE_SQUARE_BRACKET ,
71
+ ],
72
+ $ stackPtr ,
73
+ $ start
74
+ );
75
+
76
+ if (!$ previous ) {
79
77
break ;
80
78
}
81
79
82
- $ foundParens = null ;
83
- if (isset ($ tokens [$ found ]['nested_parenthesis ' ]) === true ) {
84
- $ parens = $ tokens [$ found ]['nested_parenthesis ' ];
85
- end ($ parens );
86
- $ foundParens = key ($ parens );
87
- }
88
-
89
- $ foundBracket = $ phpcsFile ->findPrevious (T_OPEN_SQUARE_BRACKET , $ found );
90
-
91
- if ($ nearestParens === $ foundParens && $ nearestBracket === $ foundBracket ) {
80
+ if ($ tokens [$ previous ]['code ' ] === T_OPEN_PARENTHESIS
81
+ || $ tokens [$ previous ]['code ' ] === T_OPEN_SQUARE_BRACKET
82
+ ) {
83
+ // We halt if we reach the opening parens / bracket of the boolean operator.
84
+ return ;
85
+ } else if ($ tokens [$ previous ]['code ' ] === T_CLOSE_PARENTHESIS ) {
86
+ // We skip the content of nested parens.
87
+ $ stackPtr = ($ tokens [$ previous ]['parenthesis_opener ' ] - 1 );
88
+ } else if ($ tokens [$ previous ]['code ' ] === T_CLOSE_SQUARE_BRACKET ) {
89
+ // We skip the content of nested brackets.
90
+ $ stackPtr = ($ tokens [$ previous ]['bracket_opener ' ] - 1 );
91
+ } else if ($ tokens [$ previous ]['code ' ] === $ search ) {
92
+ // We reached a mismatching operator, thus we must report the error.
92
93
$ error = "Mixed '&&' and '||' within an expression without using parentheses. " ;
93
94
$ phpcsFile ->addError ($ error , $ stackPtr , 'MissingParentheses ' , []);
95
+ return ;
96
+ } else {
97
+ throw new \LogicException ('Unreachable ' );
94
98
}
95
-
96
- $ start = ($ found + 1 );
97
- } while ($ start !== false );
99
+ }//end while
98
100
99
101
}//end process()
100
102
0 commit comments