@@ -215,30 +215,34 @@ public function process(File $phpcsFile, $stackPtr)
215
215
$ end = $ lastNonEmpty ;
216
216
}
217
217
218
- $ next = $ phpcsFile ->findNext (Tokens::$ emptyTokens , ($ end + 1 ), null , true );
219
-
220
- if ($ next === false || $ tokens [$ next ]['line ' ] !== $ tokens [$ end ]['line ' ]) {
218
+ $ nextContent = $ phpcsFile ->findNext (Tokens::$ emptyTokens , ($ end + 1 ), null , true );
219
+ if ($ nextContent === false || $ tokens [$ nextContent ]['line ' ] !== $ tokens [$ end ]['line ' ]) {
221
220
// Looks for completely empty statements.
222
221
$ next = $ phpcsFile ->findNext (T_WHITESPACE , ($ closer + 1 ), ($ end + 1 ), true );
223
-
224
- // Account for a comment on the end of the line.
225
- for ($ endLine = $ end ; $ endLine < $ phpcsFile ->numTokens ; $ endLine ++) {
226
- if (isset ($ tokens [($ endLine + 1 )]) === false
227
- || $ tokens [$ endLine ]['line ' ] !== $ tokens [($ endLine + 1 )]['line ' ]
228
- ) {
229
- break ;
230
- }
231
- }
232
-
233
- if ($ tokens [$ endLine ]['code ' ] !== T_COMMENT ) {
234
- $ endLine = $ end ;
235
- }
236
222
} else {
237
223
$ next = ($ end + 1 );
238
224
$ endLine = $ end ;
239
225
}
240
226
241
227
if ($ next !== $ end ) {
228
+ if ($ nextContent === false || $ tokens [$ nextContent ]['line ' ] !== $ tokens [$ end ]['line ' ]) {
229
+ // Account for a comment on the end of the line.
230
+ for ($ endLine = $ end ; $ endLine < $ phpcsFile ->numTokens ; $ endLine ++) {
231
+ if (isset ($ tokens [($ endLine + 1 )]) === false
232
+ || $ tokens [$ endLine ]['line ' ] !== $ tokens [($ endLine + 1 )]['line ' ]
233
+ ) {
234
+ break ;
235
+ }
236
+ }
237
+
238
+ if (isset (Tokens::$ commentTokens [$ tokens [$ endLine ]['code ' ]]) === false
239
+ && ($ tokens [$ endLine ]['code ' ] !== T_WHITESPACE
240
+ || isset (Tokens::$ commentTokens [$ tokens [($ endLine - 1 )]['code ' ]]) === false )
241
+ ) {
242
+ $ endLine = $ end ;
243
+ }
244
+ }
245
+
242
246
if ($ endLine !== $ end ) {
243
247
$ endToken = $ endLine ;
244
248
$ addedContent = '' ;
@@ -262,9 +266,7 @@ public function process(File $phpcsFile, $stackPtr)
262
266
} else {
263
267
$ indent = '' ;
264
268
for ($ first = $ stackPtr ; $ first > 0 ; $ first --) {
265
- if ($ first === 1
266
- || $ tokens [($ first - 1 )]['line ' ] !== $ tokens [$ first ]['line ' ]
267
- ) {
269
+ if ($ tokens [$ first ]['column ' ] === 1 ) {
268
270
break ;
269
271
}
270
272
}
@@ -285,6 +287,24 @@ public function process(File $phpcsFile, $stackPtr)
285
287
$ phpcsFile ->fixer ->addContent ($ endToken , $ addedContent );
286
288
}//end if
287
289
} else {
290
+ if ($ nextContent === false || $ tokens [$ nextContent ]['line ' ] !== $ tokens [$ end ]['line ' ]) {
291
+ // Account for a comment on the end of the line.
292
+ for ($ endLine = $ end ; $ endLine < $ phpcsFile ->numTokens ; $ endLine ++) {
293
+ if (isset ($ tokens [($ endLine + 1 )]) === false
294
+ || $ tokens [$ endLine ]['line ' ] !== $ tokens [($ endLine + 1 )]['line ' ]
295
+ ) {
296
+ break ;
297
+ }
298
+ }
299
+
300
+ if ($ tokens [$ endLine ]['code ' ] !== T_COMMENT
301
+ && ($ tokens [$ endLine ]['code ' ] !== T_WHITESPACE
302
+ || $ tokens [($ endLine - 1 )]['code ' ] !== T_COMMENT )
303
+ ) {
304
+ $ endLine = $ end ;
305
+ }
306
+ }
307
+
288
308
if ($ endLine !== $ end ) {
289
309
$ phpcsFile ->fixer ->replaceToken ($ end , '' );
290
310
$ phpcsFile ->fixer ->addNewlineBefore ($ endLine );
0 commit comments