99use const T_AND_EQUAL ;
1010use const T_CONCAT_EQUAL ;
1111use const T_DIV_EQUAL ;
12+ use const T_DO ;
1213use const T_DOC_COMMENT_CLOSE_TAG ;
1314use const T_ELSEIF ;
1415use const T_EQUAL ;
3132use const T_WHITESPACE ;
3233use const T_XOR_EQUAL ;
3334use function array_key_exists ;
35+ use function array_keys ;
36+ use function array_reverse ;
3437use function count ;
3538use function in_array ;
3639use function preg_match ;
@@ -244,7 +247,7 @@ private function hasAnotherAssigmentBefore(File $phpcsFile, int $variablePointer
244247 return false ;
245248 }
246249
247- return $ this ->areBothPointersNearby ($ phpcsFile , $ previousVariablePointer , $ variablePointer );
250+ return $ this ->areBothVariablesNearby ($ phpcsFile , $ previousVariablePointer , $ variablePointer );
248251 }
249252
250253 private function areBothPointersNearby (File $ phpcsFile , int $ firstPointer , int $ secondPointer ): bool
@@ -255,6 +258,23 @@ private function areBothPointersNearby(File $phpcsFile, int $firstPointer, int $
255258 return $ pointerAfterFirstVariableSemicolon === $ secondPointer ;
256259 }
257260
261+ private function areBothVariablesNearby (File $ phpcsFile , int $ firstVariablePointer , int $ secondVariablePointer ): bool
262+ {
263+ if ($ this ->areBothPointersNearby ($ phpcsFile , $ firstVariablePointer , $ secondVariablePointer )) {
264+ return true ;
265+ }
266+
267+ $ tokens = $ phpcsFile ->getTokens ();
268+
269+ $ lastConditionPointer = array_reverse (array_keys ($ tokens [$ firstVariablePointer ]['conditions ' ]))[0 ];
270+ $ lastConditionScopeCloserPointer = $ tokens [$ lastConditionPointer ]['scope_closer ' ];
271+ if ($ tokens [$ lastConditionPointer ]['code ' ] === T_DO ) {
272+ $ lastConditionScopeCloserPointer = TokenHelper::findNext ($ phpcsFile , T_SEMICOLON , $ lastConditionScopeCloserPointer + 1 );
273+ }
274+
275+ return TokenHelper::findNextEffective ($ phpcsFile , $ lastConditionScopeCloserPointer + 1 ) === $ secondVariablePointer ;
276+ }
277+
258278 private function findSemicolon (File $ phpcsFile , int $ pointer ): int
259279 {
260280 $ tokens = $ phpcsFile ->getTokens ();
0 commit comments