1212use SlevomatCodingStandard \Helpers \Annotation \VariableAnnotation ;
1313use SlevomatCodingStandard \Helpers \AnnotationHelper ;
1414use SlevomatCodingStandard \Helpers \IndentationHelper ;
15+ use SlevomatCodingStandard \Helpers \ScopeHelper ;
1516use SlevomatCodingStandard \Helpers \TokenHelper ;
1617use SlevomatCodingStandard \Helpers \TypeHintHelper ;
1718use function array_key_exists ;
@@ -109,7 +110,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
109110 continue ;
110111 }
111112
112- $ pointerToAddAssertion = TokenHelper:: findNext ($ phpcsFile , T_SEMICOLON , $ codePointer + 1 );
113+ $ pointerToAddAssertion = $ this -> getNextSemicolonInSameScope ($ phpcsFile , $ codePointer , $ codePointer + 1 );
113114 $ indentation = IndentationHelper::getIndentation ($ phpcsFile , $ docCommentOpenPointer );
114115
115116 } elseif ($ tokens [$ codePointer ]['code ' ] === T_LIST ) {
@@ -120,7 +121,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
120121 continue ;
121122 }
122123
123- $ pointerToAddAssertion = TokenHelper:: findNext ($ phpcsFile , T_SEMICOLON , $ codePointer + 1 );
124+ $ pointerToAddAssertion = $ this -> getNextSemicolonInSameScope ($ phpcsFile , $ codePointer , $ codePointer + 1 );
124125 $ indentation = IndentationHelper::getIndentation ($ phpcsFile , $ docCommentOpenPointer );
125126
126127 } elseif ($ tokens [$ codePointer ]['code ' ] === T_OPEN_SHORT_ARRAY ) {
@@ -134,7 +135,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
134135 continue ;
135136 }
136137
137- $ pointerToAddAssertion = TokenHelper:: findNext ($ phpcsFile , T_SEMICOLON , $ tokens [$ codePointer ]['bracket_closer ' ] + 1 );
138+ $ pointerToAddAssertion = $ this -> getNextSemicolonInSameScope ($ phpcsFile , $ codePointer , $ tokens [$ codePointer ]['bracket_closer ' ] + 1 );
138139 $ indentation = IndentationHelper::getIndentation ($ phpcsFile , $ docCommentOpenPointer );
139140
140141 } else {
@@ -226,6 +227,24 @@ private function isValidTypeNode(TypeNode $typeNode): bool
226227 return !in_array ($ typeNode ->name , ['mixed ' , 'static ' ], true );
227228 }
228229
230+ private function getNextSemicolonInSameScope (File $ phpcsFile , int $ scopePointer , int $ searchAt ): int
231+ {
232+ $ semicolonPointer = null ;
233+
234+ do {
235+ $ semicolonPointer = TokenHelper::findNext ($ phpcsFile , T_SEMICOLON , $ searchAt );
236+
237+ if (ScopeHelper::isInSameScope ($ phpcsFile , $ scopePointer , $ semicolonPointer )) {
238+ break ;
239+ }
240+
241+ $ searchAt = $ semicolonPointer + 1 ;
242+
243+ } while (true );
244+
245+ return $ semicolonPointer ;
246+ }
247+
229248 /**
230249 * @param string $variableName
231250 * @param IdentifierTypeNode|ThisTypeNode|UnionTypeNode|IntersectionTypeNode $typeNode
0 commit comments