@@ -168,15 +168,32 @@ public function processOpen(File $phpcsFile, $stackPtr)
168
168
$ fix = $ phpcsFile ->addFixableError ($ error , $ keyword , ucfirst ($ keywordType ).'Line ' , $ data );
169
169
if ($ fix === true ) {
170
170
$ phpcsFile ->fixer ->beginChangeset ();
171
- for ($ i = ($ stackPtr + 1 ); $ i < $ keyword ; $ i ++) {
172
- if ($ tokens [$ i ]['line ' ] !== $ tokens [($ i + 1 )]['line ' ]) {
173
- $ phpcsFile ->fixer ->substrToken ($ i , 0 , (strlen ($ phpcsFile ->eolChar ) * -1 ));
171
+ $ comments = [];
172
+
173
+ for ($ i = ($ stackPtr + 1 ); $ i < $ keyword ; ++$ i ) {
174
+ if ($ tokens [$ i ]['code ' ] === T_COMMENT ) {
175
+ $ comments [] = trim ($ tokens [$ i ]['content ' ]);
176
+ }
177
+
178
+ if ($ tokens [$ i ]['code ' ] === T_WHITESPACE
179
+ || $ tokens [$ i ]['code ' ] === T_COMMENT
180
+ ) {
181
+ $ phpcsFile ->fixer ->replaceToken ($ i , ' ' );
182
+ }
183
+ }
184
+
185
+ $ phpcsFile ->fixer ->addContent ($ stackPtr , ' ' );
186
+ if (empty ($ comments ) === false ) {
187
+ $ i = $ keyword ;
188
+ while ($ tokens [($ i + 1 )]['line ' ] === $ tokens [$ keyword ]['line ' ]) {
189
+ ++$ i ;
174
190
}
191
+
192
+ $ phpcsFile ->fixer ->addContentBefore ($ i , ' ' .implode (' ' , $ comments ));
175
193
}
176
194
177
- $ phpcsFile ->fixer ->addContentBefore ($ keyword , ' ' );
178
195
$ phpcsFile ->fixer ->endChangeset ();
179
- }
196
+ }//end if
180
197
} else {
181
198
// Check the whitespace before. Whitespace after is checked
182
199
// later by looking at the whitespace before the first class name
@@ -228,19 +245,28 @@ public function processOpen(File $phpcsFile, $stackPtr)
228
245
$ nextClass = $ phpcsFile ->findNext ($ find , ($ nextClass + 1 ), ($ openingBrace - 1 ));
229
246
}
230
247
248
+ $ skip = array_merge (
249
+ Tokens::$ emptyTokens ,
250
+ [
251
+ T_STRING ,
252
+ T_NS_SEPARATOR ,
253
+ ]
254
+ );
255
+
231
256
$ classCount = count ($ classNames );
232
257
$ checkingImplements = false ;
233
258
$ implementsToken = null ;
234
- $ numberOfInterface = 0 ;
259
+ $ oneInterface = false ;
235
260
foreach ($ classNames as $ n => $ className ) {
236
261
if ($ tokens [$ className ]['code ' ] === $ keywordTokenType ) {
237
262
$ checkingImplements = true ;
238
263
$ implementsToken = $ className ;
239
- continue ;
240
- }
241
264
242
- if ($ checkingImplements === true ) {
243
- $ numberOfInterface += 1 ;
265
+ if (false === $ phpcsFile ->findNext ($ skip , ($ implementsToken + 1 ), ($ openingBrace - 1 ), true )) {
266
+ $ oneInterface = true ;
267
+ }
268
+
269
+ continue ;
244
270
}
245
271
246
272
if ($ checkingImplements === true
@@ -302,15 +328,39 @@ public function processOpen(File $phpcsFile, $stackPtr)
302
328
$ phpcsFile ->fixer ->addNewline ($ prev );
303
329
$ phpcsFile ->fixer ->endChangeset ();
304
330
}
305
- } else if ($ n === ($ classCount - 1 ) && $ numberOfInterface === 1 ) {
306
- $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ className - 1 ), $ implements , true );
307
- if ($ tokens [$ prev ]['line ' ] !== $ tokens [$ className ]['line ' ]) {
331
+ } else if ($ oneInterface === true ) {
332
+ if ($ tokens [$ implementsToken ]['line ' ] !== $ tokens [$ className ]['line ' ]) {
308
333
$ error = 'Interface name should be in the same line as ' .$ keywordType .' keyword ' ;
309
334
$ fix = $ phpcsFile ->addFixableError ($ error , $ className , 'InterfaceWrongLine ' );
310
335
if ($ fix === true ) {
311
- $ phpcsFile ->fixer ->replaceToken (($ prev + 1 ), ' ' );
312
- }
313
- }
336
+ $ phpcsFile ->fixer ->beginChangeset ();
337
+ $ comments = [];
338
+
339
+ for ($ i = ($ implementsToken + 1 ); $ i < $ className ; ++$ i ) {
340
+ if ($ tokens [$ i ]['code ' ] === T_COMMENT ) {
341
+ $ comments [] = trim ($ tokens [$ i ]['content ' ]);
342
+ }
343
+
344
+ if ($ tokens [$ i ]['code ' ] === T_WHITESPACE
345
+ || $ tokens [$ i ]['code ' ] === T_COMMENT
346
+ ) {
347
+ $ phpcsFile ->fixer ->replaceToken ($ i , '' );
348
+ }
349
+ }
350
+
351
+ $ phpcsFile ->fixer ->addContent ($ implementsToken , ' ' );
352
+ if (empty ($ comments ) === false ) {
353
+ $ i = $ className ;
354
+ while ($ tokens [($ i + 1 )]['line ' ] === $ tokens [$ className ]['line ' ]) {
355
+ ++$ i ;
356
+ }
357
+
358
+ $ phpcsFile ->fixer ->addContentBefore ($ i , ' ' .implode (' ' , $ comments ));
359
+ }
360
+
361
+ $ phpcsFile ->fixer ->endChangeset ();
362
+ }//end if
363
+ }//end if
314
364
} else {
315
365
$ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ className - 1 ), $ implements );
316
366
if ($ tokens [$ prev ]['line ' ] !== $ tokens [$ className ]['line ' ]) {
@@ -321,7 +371,7 @@ public function processOpen(File $phpcsFile, $stackPtr)
321
371
322
372
$ expected = ($ classIndent + $ this ->indent );
323
373
if ($ found !== $ expected ) {
324
- $ error = 'Expected %s spaces before interface name; %s found ' ;
374
+ $ error = 'Expected %s spaces before interface name; %s found - ' . $ n . ' :: ' . $ classCount ;
325
375
$ data = [
326
376
$ expected ,
327
377
$ found ,
0 commit comments