@@ -226,18 +226,32 @@ protected function checkLinesAfter(File $phpcsFile, int $controlStructurePointer
226226 return ;
227227 }
228228
229- $ hasCommentAfter = in_array (
230- $ tokens [$ notWhitespacePointerAfter ]['code ' ],
231- Tokens::$ commentTokens ,
232- true
233- ) && $ tokens [$ notWhitespacePointerAfter ]['line ' ] === $ tokens [$ controlStructureEndPointer ]['line ' ];
234- $ pointerAfter = $ hasCommentAfter
235- ? TokenHelper::findNextExcluding ($ phpcsFile , T_WHITESPACE , $ notWhitespacePointerAfter + 1 )
236- : $ notWhitespacePointerAfter ;
237-
238- $ isLastControlStructure = in_array ($ tokens [$ controlStructurePointer ]['code ' ], [T_CASE , T_DEFAULT ], true )
239- ? $ tokens [$ pointerAfter ]['code ' ] === T_CLOSE_CURLY_BRACKET
240- : in_array ($ tokens [$ pointerAfter ]['code ' ], [T_CLOSE_CURLY_BRACKET , T_CASE , T_DEFAULT ], true );
229+ $ hasCommentAfter = in_array ($ tokens [$ notWhitespacePointerAfter ]['code ' ], Tokens::$ commentTokens , true );
230+ $ isCommentAfterOnSameLine = false ;
231+ $ pointerAfter = $ notWhitespacePointerAfter ;
232+
233+ $ isControlStructureEndAfterPointer = static function (int $ pointer ) use ($ tokens , $ controlStructurePointer ): bool {
234+ return in_array ($ tokens [$ controlStructurePointer ]['code ' ], [T_CASE , T_DEFAULT ], true )
235+ ? $ tokens [$ pointer ]['code ' ] === T_CLOSE_CURLY_BRACKET
236+ : in_array ($ tokens [$ pointer ]['code ' ], [T_CLOSE_CURLY_BRACKET , T_CASE , T_DEFAULT ], true );
237+ };
238+
239+ if ($ hasCommentAfter ) {
240+ if ($ tokens [$ notWhitespacePointerAfter ]['line ' ] === $ tokens [$ controlStructureEndPointer ]['line ' ] + 1 ) {
241+ $ commentEndPointer = CommentHelper::getCommentEndPointer ($ phpcsFile , $ notWhitespacePointerAfter );
242+ $ pointerAfterComment = TokenHelper::findNextExcluding ($ phpcsFile , T_WHITESPACE , $ commentEndPointer + 1 );
243+
244+ if ($ isControlStructureEndAfterPointer ($ pointerAfterComment )) {
245+ $ controlStructureEndPointer = $ commentEndPointer ;
246+ $ pointerAfter = $ pointerAfterComment ;
247+ }
248+ } elseif ($ tokens [$ notWhitespacePointerAfter ]['line ' ] === $ tokens [$ controlStructureEndPointer ]['line ' ]) {
249+ $ isCommentAfterOnSameLine = true ;
250+ $ pointerAfter = TokenHelper::findNextExcluding ($ phpcsFile , T_WHITESPACE , $ notWhitespacePointerAfter + 1 );
251+ }
252+ }
253+
254+ $ isLastControlStructure = $ isControlStructureEndAfterPointer ($ pointerAfter );
241255
242256 $ requiredLinesCountAfter = $ isLastControlStructure
243257 ? $ this ->getLinesCountAfterLast ($ phpcsFile , $ controlStructurePointer , $ controlStructureEndPointer )
@@ -263,7 +277,7 @@ protected function checkLinesAfter(File $phpcsFile, int $controlStructurePointer
263277 return ;
264278 }
265279
266- $ replaceStartPointer = $ hasCommentAfter ? $ notWhitespacePointerAfter : $ controlStructureEndPointer ;
280+ $ replaceStartPointer = $ isCommentAfterOnSameLine ? $ notWhitespacePointerAfter : $ controlStructureEndPointer ;
267281 $ endOfLineBeforeAfterPointer = TokenHelper::findPreviousContent ($ phpcsFile , T_WHITESPACE , $ phpcsFile ->eolChar , $ pointerAfter - 1 );
268282
269283 $ phpcsFile ->fixer ->beginChangeset ();
@@ -272,7 +286,7 @@ protected function checkLinesAfter(File $phpcsFile, int $controlStructurePointer
272286 $ phpcsFile ->fixer ->replaceToken ($ i , '' );
273287 }
274288
275- if ($ hasCommentAfter ) {
289+ if ($ isCommentAfterOnSameLine ) {
276290 for ($ i = 0 ; $ i < $ requiredLinesCountAfter ; $ i ++) {
277291 $ phpcsFile ->fixer ->addNewline ($ notWhitespacePointerAfter );
278292 }
0 commit comments