@@ -375,28 +375,16 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
375
375
// at a tab stop. Without this, the function will be indented a further
376
376
// $indent spaces to the right.
377
377
$ functionIndent = (int ) (floor ($ foundFunctionIndent / $ this ->indent ) * $ this ->indent );
378
- $ adjustment = 0 ;
378
+ $ adjustment = ( $ functionIndent - $ foundFunctionIndent ) ;
379
379
380
380
if ($ foundFunctionIndent !== $ functionIndent ) {
381
- $ error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s ' ;
382
- $ data = [
381
+ $ this ->complainOpenStatementWrongIndent (
382
+ $ phpcsFile ,
383
+ $ first ,
384
+ $ tokens ,
383
385
$ functionIndent ,
384
- $ foundFunctionIndent ,
385
- ];
386
-
387
- $ fix = $ phpcsFile ->addFixableError ($ error , $ first , 'OpeningIndent ' , $ data );
388
- if ($ fix === true ) {
389
- $ adjustment = ($ functionIndent - $ foundFunctionIndent );
390
- $ padding = str_repeat (' ' , $ functionIndent );
391
- if ($ foundFunctionIndent === 0 ) {
392
- $ phpcsFile ->fixer ->addContentBefore ($ first , $ padding );
393
- } else if ($ tokens [$ first ]['code ' ] === T_INLINE_HTML ) {
394
- $ newContent = $ padding .ltrim ($ tokens [$ first ]['content ' ]);
395
- $ phpcsFile ->fixer ->replaceToken ($ first , $ newContent );
396
- } else {
397
- $ phpcsFile ->fixer ->replaceToken (($ first - 1 ), $ padding );
398
- }
399
- }
386
+ $ foundFunctionIndent
387
+ );
400
388
}//end if
401
389
402
390
$ next = $ phpcsFile ->findNext (Tokens::$ emptyTokens , ($ openBracket + 1 ), null , true );
@@ -462,7 +450,7 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
462
450
$ i --;
463
451
}
464
452
465
- for ($ i ; $ i < $ closeBracket ; $ i ++) {
453
+ for (; $ i < $ closeBracket ; $ i ++) {
466
454
if ($ i > $ argStart && $ i < $ argEnd ) {
467
455
$ inArg = true ;
468
456
} else {
@@ -542,10 +530,34 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
542
530
$ foundIndent = $ tokens [$ i ]['length ' ];
543
531
}
544
532
545
- if ($ foundIndent < $ expectedIndent
546
- || ($ inArg === false
547
- && $ expectedIndent !== $ foundIndent )
548
- ) {
533
+ $ indentCorrect = true ;
534
+
535
+ if ($ foundIndent < $ expectedIndent ) {
536
+ $ indentCorrect = false ;
537
+ } else if ($ inArg === false && $ expectedIndent !== $ foundIndent ) {
538
+ $ indentCorrect = false ;
539
+
540
+ // It is permitted to indent chains further than one tab stop to
541
+ // align vertically with the previous method call.
542
+ if ($ i === ($ closeBracket - 1 )) {
543
+ if ($ foundIndent === $ foundFunctionIndent ) {
544
+ // This is the closing paren; it lines up vertically with the opening paren.
545
+ $ indentCorrect = true ;
546
+ }
547
+ } else {
548
+ if ($ foundIndent === ($ tokens [$ openBracket ]['column ' ] - 1 )) {
549
+ // This is a parameter; it lines up vertically with the opening paren.
550
+ $ indentCorrect = true ;
551
+ }
552
+
553
+ if ($ foundIndent === ($ foundFunctionIndent + ($ this ->indent ))) {
554
+ // This is a parameter; it is indented one more step than the function call around it.
555
+ $ indentCorrect = true ;
556
+ }
557
+ }
558
+ }//end if
559
+
560
+ if ($ indentCorrect === false ) {
549
561
$ error = 'Multi-line function call not indented correctly; expected %s spaces but found %s ' ;
550
562
$ data = [
551
563
$ expectedIndent ,
@@ -628,4 +640,51 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
628
640
}//end processMultiLineCall()
629
641
630
642
643
+ /**
644
+ * Add a complaint (and auto-fix) if the function indent is 'wrong'.
645
+ *
646
+ * @param File $phpcsFile The file being scanned.
647
+ * @param int $first Pointer to the first empty token on this line.
648
+ * @param array $tokens The stack of tokens that make up the file.
649
+ * @param int $functionIndent The expected indent for this function definition.
650
+ * @param int $foundFunctionIndent The actual indent for this function definition.
651
+ *
652
+ * @return void
653
+ */
654
+ protected function complainOpenStatementWrongIndent (
655
+ $ phpcsFile ,
656
+ $ first ,
657
+ $ tokens ,
658
+ $ functionIndent ,
659
+ $ foundFunctionIndent
660
+ ) {
661
+ if ($ foundFunctionIndent === $ functionIndent ) {
662
+ return ;
663
+ }
664
+
665
+ $ error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s ' ;
666
+ $ data = [
667
+ $ functionIndent ,
668
+ $ foundFunctionIndent ,
669
+ ];
670
+
671
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ first , 'OpeningIndent ' , $ data );
672
+ if ($ fix !== true ) {
673
+ return ;
674
+ }
675
+
676
+ $ padding = str_repeat (' ' , $ functionIndent );
677
+
678
+ if ($ foundFunctionIndent === 0 ) {
679
+ $ phpcsFile ->fixer ->addContentBefore ($ first , $ padding );
680
+ } else if ($ tokens [$ first ]['code ' ] === T_INLINE_HTML ) {
681
+ $ newContent = $ padding .ltrim ($ tokens [$ first ]['content ' ]);
682
+ $ phpcsFile ->fixer ->replaceToken ($ first , $ newContent );
683
+ } else {
684
+ $ phpcsFile ->fixer ->replaceToken (($ first - 1 ), $ padding );
685
+ }
686
+
687
+ }//end complainOpenStatementWrongIndent()
688
+
689
+
631
690
}//end class
0 commit comments