66use PHP_CodeSniffer \Sniffs \Sniff ;
77use SlevomatCodingStandard \Helpers \ScopeHelper ;
88use SlevomatCodingStandard \Helpers \TokenHelper ;
9- use function array_key_exists ;
109use function array_keys ;
1110use function array_reverse ;
1211use function count ;
2423use const T_DOUBLE_COLON ;
2524use const T_ELSEIF ;
2625use const T_EQUAL ;
26+ use const T_FOR ;
27+ use const T_FOREACH ;
2728use const T_IF ;
2829use const T_MINUS_EQUAL ;
2930use const T_MOD_EQUAL ;
3031use const T_MUL_EQUAL ;
3132use const T_OPEN_CURLY_BRACKET ;
32- use const T_OPEN_PARENTHESIS ;
3333use const T_OR_EQUAL ;
3434use const T_PLUS_EQUAL ;
3535use const T_POW_EQUAL ;
3838use const T_SL_EQUAL ;
3939use const T_SR_EQUAL ;
4040use const T_STATIC ;
41+ use const T_SWITCH ;
4142use const T_VARIABLE ;
4243use const T_WHILE ;
4344use const T_WHITESPACE ;
@@ -104,6 +105,10 @@ public function process(File $phpcsFile, $returnPointer): void
104105 return ;
105106 }
106107
108+ if ($ this ->isAssignedInControlStructure ($ phpcsFile , $ previousVariablePointer )) {
109+ return ;
110+ }
111+
107112 if ($ this ->hasVariableVarAnnotation ($ phpcsFile , $ previousVariablePointer )) {
108113 return ;
109114 }
@@ -122,28 +127,6 @@ public function process(File $phpcsFile, $returnPointer): void
122127 self ::CODE_USELESS_VARIABLE ,
123128 ];
124129
125- $ searchBefore = $ previousVariablePointer ;
126- do {
127- $ previousOpenParenthesisPointer = TokenHelper::findPrevious ($ phpcsFile , T_OPEN_PARENTHESIS , $ searchBefore - 1 );
128-
129- if (
130- $ previousOpenParenthesisPointer === null
131- || $ tokens [$ previousOpenParenthesisPointer ]['parenthesis_closer ' ] < $ previousVariablePointer
132- ) {
133- break ;
134- }
135-
136- if (
137- array_key_exists ('parenthesis_owner ' , $ tokens [$ previousOpenParenthesisPointer ])
138- && in_array ($ tokens [$ tokens [$ previousOpenParenthesisPointer ]['parenthesis_owner ' ]]['code ' ], [T_IF , T_ELSEIF , T_WHILE ], true )
139- ) {
140- return ;
141- }
142-
143- $ searchBefore = $ previousOpenParenthesisPointer ;
144-
145- } while (true );
146-
147130 $ pointerBeforePreviousVariable = TokenHelper::findPreviousEffective ($ phpcsFile , $ previousVariablePointer - 1 );
148131
149132 if (
@@ -234,6 +217,26 @@ private function findPreviousVariablePointer(File $phpcsFile, int $pointer, stri
234217 return null ;
235218 }
236219
220+ private function isAssignedInControlStructure (File $ phpcsFile , int $ pointer ): bool
221+ {
222+ $ controlStructure = TokenHelper::findPrevious ($ phpcsFile , [
223+ T_WHILE ,
224+ T_FOR ,
225+ T_FOREACH ,
226+ T_SWITCH ,
227+ T_IF ,
228+ T_ELSEIF ,
229+ ], $ pointer - 1 );
230+
231+ if ($ controlStructure === null ) {
232+ return false ;
233+ }
234+
235+ $ tokens = $ phpcsFile ->getTokens ();
236+
237+ return $ tokens [$ controlStructure ]['parenthesis_opener ' ] < $ pointer && $ pointer < $ tokens [$ controlStructure ]['parenthesis_closer ' ];
238+ }
239+
237240 private function isAssigmentToVariable (File $ phpcsFile , int $ pointer ): bool
238241 {
239242 $ assigmentPointer = TokenHelper::findNextEffective ($ phpcsFile , $ pointer + 1 );
0 commit comments