@@ -338,40 +338,53 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
338
338
// We need to work out how far indented the function
339
339
// call itself is, so we can work out how far to
340
340
// indent the arguments.
341
- $ start = $ phpcsFile ->findStartOfStatement ( $ stackPtr );
342
- foreach ([ ' stackPtr ' , ' start ' ] as $ checkToken ) {
343
- $ x = $ $ checkToken ;
344
- for ( $ i = ( $ x - 1 ); $ i >= 0 ; $ i -- ) {
345
- if ( $ tokens [ $ i ][ ' line ' ] !== $ tokens [ $ x ][ ' line ' ]) {
346
- $ i ++;
347
- break ;
348
- }
349
- }
341
+ $ first = $ phpcsFile ->findFirstOnLine ( T_WHITESPACE , $ stackPtr, true );
342
+ if ( $ tokens [ $ first ][ ' code ' ] === T_CONSTANT_ENCAPSED_STRING
343
+ && $ tokens [( $ first - 1 )][ ' code ' ] === T_CONSTANT_ENCAPSED_STRING
344
+ ) {
345
+ // We are in a multi- line string, so find the start and use
346
+ // the indent from there.
347
+ $ prev = $ phpcsFile -> findPrevious ( T_CONSTANT_ENCAPSED_STRING , ( $ first - 2 ), null , true ) ;
348
+ $ first = $ phpcsFile -> findFirstOnLine (Tokens:: $ emptyTokens , $ prev , true );
349
+ }
350
350
351
- if ($ i <= 0 ) {
352
- $ functionIndent = 0 ;
353
- } else if ($ tokens [$ i ]['code ' ] === T_WHITESPACE ) {
354
- $ functionIndent = strlen ($ tokens [$ i ]['content ' ]);
355
- } else if ($ tokens [$ i ]['code ' ] === T_CONSTANT_ENCAPSED_STRING ) {
356
- $ functionIndent = 0 ;
357
- } else {
358
- $ trimmed = ltrim ($ tokens [$ i ]['content ' ]);
351
+ $ foundIndent = 0 ;
352
+ if ($ first !== false ) {
353
+ if ($ tokens [$ first ]['code ' ] === T_INLINE_HTML ) {
354
+ $ trimmed = ltrim ($ tokens [$ first ]['content ' ]);
359
355
if ($ trimmed === '' ) {
360
- if ($ tokens [$ i ]['code ' ] === T_INLINE_HTML ) {
361
- $ functionIndent = strlen ($ tokens [$ i ]['content ' ]);
362
- } else {
363
- $ functionIndent = ($ tokens [$ i ]['column ' ] - 1 );
364
- }
356
+ $ foundIndent = strlen ($ tokens [$ first ]['content ' ]);
365
357
} else {
366
- $ functionIndent = (strlen ($ tokens [$ i ]['content ' ]) - strlen ($ trimmed ));
358
+ $ foundIndent = (strlen ($ tokens [$ first ]['content ' ]) - strlen ($ trimmed ));
367
359
}
360
+ } else {
361
+ $ foundIndent = ($ tokens [$ first ]['column ' ] - 1 );
368
362
}
363
+ }
369
364
370
- $ varName = $ checkToken .'Indent ' ;
371
- $ $ varName = $ functionIndent ;
372
- }//end foreach
365
+ // Make sure the function indent is divisible by the indent size.
366
+ // We round down here because this accounts for times when the
367
+ // surrounding code is indented a little too far in, and not correctly
368
+ // at a tab stop. Without this, the function will be indented a further
369
+ // $indent spaces to the right.
370
+ $ functionIndent = (int ) (floor ($ foundIndent / $ this ->indent ) * $ this ->indent );
371
+ if ($ foundIndent !== $ functionIndent ) {
372
+ $ error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s ' ;
373
+ $ data = [
374
+ $ functionIndent ,
375
+ $ foundIndent ,
376
+ ];
373
377
374
- $ functionIndent = max ($ startIndent , $ stackPtrIndent );
378
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ first , 'OpeningIndent ' , $ data );
379
+ if ($ fix === true ) {
380
+ $ padding = str_repeat (' ' , $ functionIndent );
381
+ if ($ foundIndent === 0 ) {
382
+ $ phpcsFile ->fixer ->addContentBefore ($ first , $ padding );
383
+ } else {
384
+ $ phpcsFile ->fixer ->replaceToken (($ first - 1 ), $ padding );
385
+ }
386
+ }
387
+ }
375
388
376
389
$ next = $ phpcsFile ->findNext (Tokens::$ emptyTokens , ($ openBracket + 1 ), null , true );
377
390
if ($ tokens [$ next ]['line ' ] === $ tokens [$ openBracket ]['line ' ]) {
@@ -470,13 +483,6 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
470
483
$ expectedIndent = ($ functionIndent + $ this ->indent );
471
484
}
472
485
473
- // Make sure the expected indent is divisible by the indent size.
474
- // We round down here because this accounts for times when the
475
- // surrounding code is indented a little too far in, and not correctly
476
- // at a tab stop. Without this, the function will be indented a further
477
- // $indent spaces to the right.
478
- $ expectedIndent = (int ) (floor ($ expectedIndent / $ this ->indent ) * $ this ->indent );
479
-
480
486
if ($ tokens [$ i ]['code ' ] !== T_WHITESPACE
481
487
&& $ tokens [$ i ]['code ' ] !== T_DOC_COMMENT_WHITESPACE
482
488
) {
0 commit comments