@@ -282,6 +282,97 @@ public function processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens)
282
282
*/
283
283
public function processMultiLineDeclaration ($ phpcsFile , $ stackPtr , $ tokens )
284
284
{
285
+ $ this ->processArgumentList ($ phpcsFile , $ stackPtr , $ this ->indent );
286
+
287
+ $ closeBracket = $ tokens [$ stackPtr ]['parenthesis_closer ' ];
288
+ if ($ tokens [$ stackPtr ]['code ' ] === T_CLOSURE ) {
289
+ $ use = $ phpcsFile ->findNext (T_USE , ($ closeBracket + 1 ), $ tokens [$ stackPtr ]['scope_opener ' ]);
290
+ if ($ use !== false ) {
291
+ $ open = $ phpcsFile ->findNext (T_OPEN_PARENTHESIS , ($ use + 1 ));
292
+ $ closeBracket = $ tokens [$ open ]['parenthesis_closer ' ];
293
+ }
294
+ }
295
+
296
+ if (isset ($ tokens [$ stackPtr ]['scope_opener ' ]) === false ) {
297
+ return ;
298
+ }
299
+
300
+ // The opening brace needs to be one space away from the closing parenthesis.
301
+ $ opener = $ tokens [$ stackPtr ]['scope_opener ' ];
302
+ if ($ tokens [$ opener ]['line ' ] !== $ tokens [$ closeBracket ]['line ' ]) {
303
+ $ error = 'The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line ' ;
304
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ opener , 'NewlineBeforeOpenBrace ' );
305
+ if ($ fix === true ) {
306
+ $ prev = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ opener - 1 ), $ closeBracket , true );
307
+ $ phpcsFile ->fixer ->beginChangeset ();
308
+ $ phpcsFile ->fixer ->addContent ($ prev , ' { ' );
309
+
310
+ // If the opener is on a line by itself, removing it will create
311
+ // an empty line, so just remove the entire line instead.
312
+ $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ opener - 1 ), $ closeBracket , true );
313
+ $ next = $ phpcsFile ->findNext (T_WHITESPACE , ($ opener + 1 ), null , true );
314
+
315
+ if ($ tokens [$ prev ]['line ' ] < $ tokens [$ opener ]['line ' ]
316
+ && $ tokens [$ next ]['line ' ] > $ tokens [$ opener ]['line ' ]
317
+ ) {
318
+ // Clear the whole line.
319
+ for ($ i = ($ prev + 1 ); $ i < $ next ; $ i ++) {
320
+ if ($ tokens [$ i ]['line ' ] === $ tokens [$ opener ]['line ' ]) {
321
+ $ phpcsFile ->fixer ->replaceToken ($ i , '' );
322
+ }
323
+ }
324
+ } else {
325
+ // Just remove the opener.
326
+ $ phpcsFile ->fixer ->replaceToken ($ opener , '' );
327
+ if ($ tokens [$ next ]['line ' ] === $ tokens [$ opener ]['line ' ]) {
328
+ $ phpcsFile ->fixer ->replaceToken (($ opener + 1 ), '' );
329
+ }
330
+ }
331
+
332
+ $ phpcsFile ->fixer ->endChangeset ();
333
+ }//end if
334
+ } else {
335
+ $ prev = $ tokens [($ opener - 1 )];
336
+ if ($ prev ['code ' ] !== T_WHITESPACE ) {
337
+ $ length = 0 ;
338
+ } else {
339
+ $ length = strlen ($ prev ['content ' ]);
340
+ }
341
+
342
+ if ($ length !== 1 ) {
343
+ $ error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found %s spaces ' ;
344
+ $ fix = $ phpcsFile ->addFixableError ($ error , ($ opener - 1 ), 'SpaceBeforeOpenBrace ' , [$ length ]);
345
+ if ($ fix === true ) {
346
+ if ($ length === 0 ) {
347
+ $ phpcsFile ->fixer ->addContentBefore ($ opener , ' ' );
348
+ } else {
349
+ $ phpcsFile ->fixer ->replaceToken (($ opener - 1 ), ' ' );
350
+ }
351
+ }
352
+
353
+ return ;
354
+ }//end if
355
+ }//end if
356
+
357
+ }//end processMultiLineDeclaration()
358
+
359
+
360
+ /**
361
+ * Processes multi-line argument list declarations.
362
+ *
363
+ * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
364
+ * @param int $stackPtr The position of the current token
365
+ * in the stack passed in $tokens.
366
+ * @param int $indent The number of spaces code should be indented.
367
+ * @param string $type The type of the token the brackets
368
+ * belong to.
369
+ *
370
+ * @return void
371
+ */
372
+ public function processArgumentList ($ phpcsFile , $ stackPtr , $ indent , $ type ='function ' )
373
+ {
374
+ $ tokens = $ phpcsFile ->getTokens ();
375
+
285
376
// We need to work out how far indented the function
286
377
// declaration itself is, so we can work out how far to
287
378
// indent parameters.
@@ -309,13 +400,13 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
309
400
true
310
401
);
311
402
312
- if ($ tokens [$ closeBracket ]['line ' ] !== $ tokens [$ tokens [$ closeBracket ]['parenthesis_opener ' ]]['line ' ]) {
313
- if ( $ tokens [$ prev ]['line ' ] === $ tokens [$ closeBracket ]['line ' ]) {
314
- $ error = ' The closing parenthesis of a multi-line function declaration must be on a new line ' ;
315
- $ fix = $ phpcsFile -> addFixableError ( $ error , $ closeBracket , ' CloseBracketLine ' ) ;
316
- if ( $ fix === true ) {
317
- $ phpcsFile -> fixer -> addNewlineBefore ( $ closeBracket );
318
- }
403
+ if ($ tokens [$ closeBracket ]['line ' ] !== $ tokens [$ tokens [$ closeBracket ]['parenthesis_opener ' ]]['line ' ]
404
+ && $ tokens [$ prev ]['line ' ] === $ tokens [$ closeBracket ]['line ' ]
405
+ ) {
406
+ $ error = ' The closing parenthesis of a multi-line ' . $ type . ' declaration must be on a new line ' ;
407
+ $ fix = $ phpcsFile -> addFixableError ( $ error , $ closeBracket , ' CloseBracketLine ' );
408
+ if ( $ fix === true ) {
409
+ $ phpcsFile -> fixer -> addNewlineBefore ( $ closeBracket );
319
410
}
320
411
}
321
412
@@ -335,13 +426,13 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
335
426
true
336
427
);
337
428
338
- if ($ tokens [$ closeBracket ]['line ' ] !== $ tokens [$ tokens [$ closeBracket ]['parenthesis_opener ' ]]['line ' ]) {
339
- if ( $ tokens [$ prev ]['line ' ] === $ tokens [$ closeBracket ]['line ' ]) {
340
- $ error = ' The closing parenthesis of a multi-line use declaration must be on a new line ' ;
341
- $ fix = $ phpcsFile -> addFixableError ( $ error , $ closeBracket , ' UseCloseBracketLine ' ) ;
342
- if ( $ fix === true ) {
343
- $ phpcsFile -> fixer -> addNewlineBefore ( $ closeBracket );
344
- }
429
+ if ($ tokens [$ closeBracket ]['line ' ] !== $ tokens [$ tokens [$ closeBracket ]['parenthesis_opener ' ]]['line ' ]
430
+ && $ tokens [$ prev ]['line ' ] === $ tokens [$ closeBracket ]['line ' ]
431
+ ) {
432
+ $ error = ' The closing parenthesis of a multi-line use declaration must be on a new line ' ;
433
+ $ fix = $ phpcsFile -> addFixableError ( $ error , $ closeBracket , ' UseCloseBracketLine ' );
434
+ if ( $ fix === true ) {
435
+ $ phpcsFile -> fixer -> addNewlineBefore ( $ closeBracket );
345
436
}
346
437
}
347
438
}//end if
@@ -361,7 +452,7 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
361
452
// as the function.
362
453
$ expectedIndent = $ functionIndent ;
363
454
} else {
364
- $ expectedIndent = ($ functionIndent + $ this -> indent );
455
+ $ expectedIndent = ($ functionIndent + $ indent );
365
456
}
366
457
367
458
// We changed lines, so this should be a whitespace indent token.
@@ -371,7 +462,7 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
371
462
// This is an empty line, so don't check the indent.
372
463
$ foundIndent = $ expectedIndent ;
373
464
374
- $ error = 'Blank lines are not allowed in a multi-line function declaration ' ;
465
+ $ error = 'Blank lines are not allowed in a multi-line ' . $ type . ' declaration ' ;
375
466
$ fix = $ phpcsFile ->addFixableError ($ error , $ i , 'EmptyLine ' );
376
467
if ($ fix === true ) {
377
468
$ phpcsFile ->fixer ->replaceToken ($ i , '' );
@@ -381,7 +472,7 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
381
472
}
382
473
383
474
if ($ expectedIndent !== $ foundIndent ) {
384
- $ error = 'Multi-line function declaration not indented correctly; expected %s spaces but found %s ' ;
475
+ $ error = 'Multi-line ' . $ type . ' declaration not indented correctly; expected %s spaces but found %s ' ;
385
476
$ data = [
386
477
$ expectedIndent ,
387
478
$ foundIndent ,
@@ -414,68 +505,7 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
414
505
}
415
506
}//end for
416
507
417
- if (isset ($ tokens [$ stackPtr ]['scope_opener ' ]) === false ) {
418
- return ;
419
- }
420
-
421
- // The opening brace needs to be one space away from the closing parenthesis.
422
- $ opener = $ tokens [$ stackPtr ]['scope_opener ' ];
423
- if ($ tokens [$ opener ]['line ' ] !== $ tokens [$ closeBracket ]['line ' ]) {
424
- $ error = 'The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line ' ;
425
- $ fix = $ phpcsFile ->addFixableError ($ error , $ opener , 'NewlineBeforeOpenBrace ' );
426
- if ($ fix === true ) {
427
- $ prev = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ opener - 1 ), $ closeBracket , true );
428
- $ phpcsFile ->fixer ->beginChangeset ();
429
- $ phpcsFile ->fixer ->addContent ($ prev , ' { ' );
430
-
431
- // If the opener is on a line by itself, removing it will create
432
- // an empty line, so just remove the entire line instead.
433
- $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ opener - 1 ), $ closeBracket , true );
434
- $ next = $ phpcsFile ->findNext (T_WHITESPACE , ($ opener + 1 ), null , true );
435
-
436
- if ($ tokens [$ prev ]['line ' ] < $ tokens [$ opener ]['line ' ]
437
- && $ tokens [$ next ]['line ' ] > $ tokens [$ opener ]['line ' ]
438
- ) {
439
- // Clear the whole line.
440
- for ($ i = ($ prev + 1 ); $ i < $ next ; $ i ++) {
441
- if ($ tokens [$ i ]['line ' ] === $ tokens [$ opener ]['line ' ]) {
442
- $ phpcsFile ->fixer ->replaceToken ($ i , '' );
443
- }
444
- }
445
- } else {
446
- // Just remove the opener.
447
- $ phpcsFile ->fixer ->replaceToken ($ opener , '' );
448
- if ($ tokens [$ next ]['line ' ] === $ tokens [$ opener ]['line ' ]) {
449
- $ phpcsFile ->fixer ->replaceToken (($ opener + 1 ), '' );
450
- }
451
- }
452
-
453
- $ phpcsFile ->fixer ->endChangeset ();
454
- }//end if
455
- } else {
456
- $ prev = $ tokens [($ opener - 1 )];
457
- if ($ prev ['code ' ] !== T_WHITESPACE ) {
458
- $ length = 0 ;
459
- } else {
460
- $ length = strlen ($ prev ['content ' ]);
461
- }
462
-
463
- if ($ length !== 1 ) {
464
- $ error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found %s spaces ' ;
465
- $ fix = $ phpcsFile ->addFixableError ($ error , ($ opener - 1 ), 'SpaceBeforeOpenBrace ' , [$ length ]);
466
- if ($ fix === true ) {
467
- if ($ length === 0 ) {
468
- $ phpcsFile ->fixer ->addContentBefore ($ opener , ' ' );
469
- } else {
470
- $ phpcsFile ->fixer ->replaceToken (($ opener - 1 ), ' ' );
471
- }
472
- }
473
-
474
- return ;
475
- }//end if
476
- }//end if
477
-
478
- }//end processMultiLineDeclaration()
508
+ }//end processArgumentList()
479
509
480
510
481
511
}//end class
0 commit comments