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