• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 8895440661

30 Apr 2024 01:13PM UTC coverage: 75.015% (+0.02%) from 75.0%
8895440661

Pull #38

github

web-flow
Merge 810f74786 into 7aceb913e
Pull Request #38: Avoid incompatibility between `Generic` and `PSR2` standards for function call indentation

57 of 61 new or added lines in 2 files covered. (93.44%)

1 existing line in 1 file now uncovered.

23100 of 30794 relevant lines covered (75.01%)

58.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.67
/src/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php
1
<?php
2
/**
3
 * Ensures function calls are formatted correctly.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\Sniff;
14
use PHP_CodeSniffer\Util\Tokens;
15

16
class FunctionCallSignatureSniff implements Sniff
17
{
18

19
    /**
20
     * A list of tokenizers this sniff supports.
21
     *
22
     * @var array
23
     */
24
    public $supportedTokenizers = [
25
        'PHP',
26
        'JS',
27
    ];
28

29
    /**
30
     * The number of spaces code should be indented.
31
     *
32
     * @var integer
33
     */
34
    public $indent = 4;
35

36
    /**
37
     * If TRUE, multiple arguments can be defined per line in a multi-line call.
38
     *
39
     * @var boolean
40
     */
41
    public $allowMultipleArguments = true;
42

43
    /**
44
     * How many spaces should follow the opening bracket.
45
     *
46
     * @var integer
47
     */
48
    public $requiredSpacesAfterOpen = 0;
49

50
    /**
51
     * How many spaces should precede the closing bracket.
52
     *
53
     * @var integer
54
     */
55
    public $requiredSpacesBeforeClose = 0;
56

57

58
    /**
59
     * Returns an array of tokens this test wants to listen for.
60
     *
61
     * @return array<int|string>
62
     */
63
    public function register()
3✔
64
    {
65
        $tokens = Tokens::$functionNameTokens;
3✔
66

67
        $tokens[] = T_VARIABLE;
3✔
68
        $tokens[] = T_CLOSE_CURLY_BRACKET;
3✔
69
        $tokens[] = T_CLOSE_SQUARE_BRACKET;
3✔
70
        $tokens[] = T_CLOSE_PARENTHESIS;
3✔
71

72
        return $tokens;
3✔
73

74
    }//end register()
75

76

77
    /**
78
     * Processes this test, when one of its tokens is encountered.
79
     *
80
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
81
     * @param int                         $stackPtr  The position of the current token
82
     *                                               in the stack passed in $tokens.
83
     *
84
     * @return void
85
     */
86
    public function process(File $phpcsFile, $stackPtr)
3✔
87
    {
88
        $this->requiredSpacesAfterOpen   = (int) $this->requiredSpacesAfterOpen;
3✔
89
        $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
3✔
90
        $tokens = $phpcsFile->getTokens();
3✔
91

92
        if ($tokens[$stackPtr]['code'] === T_CLOSE_CURLY_BRACKET
3✔
93
            && isset($tokens[$stackPtr]['scope_condition']) === true
3✔
94
        ) {
1✔
95
            // Not a function call.
96
            return;
3✔
97
        }
98

99
        // Find the next non-empty token.
100
        $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
3✔
101

102
        if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
3✔
103
            // Not a function call.
104
            return;
3✔
105
        }
106

107
        if (isset($tokens[$openBracket]['parenthesis_closer']) === false) {
3✔
108
            // Not a function call.
109
            return;
×
110
        }
111

112
        // Find the previous non-empty token.
113
        $search   = Tokens::$emptyTokens;
3✔
114
        $search[] = T_BITWISE_AND;
3✔
115
        $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true);
3✔
116
        if ($tokens[$previous]['code'] === T_FUNCTION) {
3✔
117
            // It's a function definition, not a function call.
118
            return;
3✔
119
        }
120

121
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
3✔
122

123
        if (($stackPtr + 1) !== $openBracket) {
3✔
124
            // Checking this: $value = my_function[*](...).
125
            $error = 'Space before opening parenthesis of function call prohibited';
3✔
126
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeOpenBracket');
3✔
127
            if ($fix === true) {
3✔
128
                $phpcsFile->fixer->beginChangeset();
3✔
129
                for ($i = ($stackPtr + 1); $i < $openBracket; $i++) {
3✔
130
                    $phpcsFile->fixer->replaceToken($i, '');
3✔
131
                }
1✔
132

133
                // Modify the bracket as well to ensure a conflict if the bracket
134
                // has been changed in some way by another sniff.
135
                $phpcsFile->fixer->replaceToken($openBracket, '(');
3✔
136
                $phpcsFile->fixer->endChangeset();
3✔
137
            }
1✔
138
        }
1✔
139

140
        $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
3✔
141
        if ($tokens[$next]['code'] === T_SEMICOLON) {
3✔
142
            if (isset(Tokens::$emptyTokens[$tokens[($closeBracket + 1)]['code']]) === true) {
3✔
143
                $error = 'Space after closing parenthesis of function call prohibited';
3✔
144
                $fix   = $phpcsFile->addFixableError($error, $closeBracket, 'SpaceAfterCloseBracket');
3✔
145
                if ($fix === true) {
3✔
146
                    $phpcsFile->fixer->beginChangeset();
3✔
147
                    for ($i = ($closeBracket + 1); $i < $next; $i++) {
3✔
148
                        $phpcsFile->fixer->replaceToken($i, '');
3✔
149
                    }
1✔
150

151
                    // Modify the bracket as well to ensure a conflict if the bracket
152
                    // has been changed in some way by another sniff.
153
                    $phpcsFile->fixer->replaceToken($closeBracket, ')');
3✔
154
                    $phpcsFile->fixer->endChangeset();
3✔
155
                }
1✔
156
            }
1✔
157
        }
1✔
158

159
        // Check if this is a single line or multi-line function call.
160
        if ($this->isMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens) === true) {
3✔
161
            $this->processMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
3✔
162
        } else {
1✔
163
            $this->processSingleLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
3✔
164
        }
165

166
    }//end process()
2✔
167

168

169
    /**
170
     * Determine if this is a multi-line function call.
171
     *
172
     * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
173
     * @param int                         $stackPtr    The position of the current token
174
     *                                                 in the stack passed in $tokens.
175
     * @param int                         $openBracket The position of the opening bracket
176
     *                                                 in the stack passed in $tokens.
177
     * @param array                       $tokens      The stack of tokens that make up
178
     *                                                 the file.
179
     *
180
     * @return bool
181
     */
182
    public function isMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $tokens)
3✔
183
    {
184
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
3✔
185
        if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) {
3✔
186
            return true;
3✔
187
        }
188

189
        return false;
3✔
190

191
    }//end isMultiLineCall()
192

193

194
    /**
195
     * Processes single-line calls.
196
     *
197
     * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
198
     * @param int                         $stackPtr    The position of the current token
199
     *                                                 in the stack passed in $tokens.
200
     * @param int                         $openBracket The position of the opening bracket
201
     *                                                 in the stack passed in $tokens.
202
     * @param array                       $tokens      The stack of tokens that make up
203
     *                                                 the file.
204
     *
205
     * @return void
206
     */
207
    public function processSingleLineCall(File $phpcsFile, $stackPtr, $openBracket, $tokens)
3✔
208
    {
209
        $closer = $tokens[$openBracket]['parenthesis_closer'];
3✔
210
        if ($openBracket === ($closer - 1)) {
3✔
211
            return;
3✔
212
        }
213

214
        // If the function call has no arguments or comments, enforce 0 spaces.
215
        $next = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), $closer, true);
3✔
216
        if ($next === false) {
3✔
217
            $requiredSpacesAfterOpen   = 0;
3✔
218
            $requiredSpacesBeforeClose = 0;
3✔
219
        } else {
1✔
220
            $requiredSpacesAfterOpen   = $this->requiredSpacesAfterOpen;
3✔
221
            $requiredSpacesBeforeClose = $this->requiredSpacesBeforeClose;
3✔
222
        }
223

224
        if ($requiredSpacesAfterOpen === 0 && $tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
3✔
225
            // Checking this: $value = my_function([*]...).
226
            $error = 'Space after opening parenthesis of function call prohibited';
3✔
227
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpenBracket');
3✔
228
            if ($fix === true) {
3✔
229
                $phpcsFile->fixer->replaceToken(($openBracket + 1), '');
3✔
230
            }
1✔
231
        } else if ($requiredSpacesAfterOpen > 0) {
3✔
232
            $spaceAfterOpen = 0;
3✔
233
            if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
3✔
234
                $spaceAfterOpen = $tokens[($openBracket + 1)]['length'];
3✔
235
            }
1✔
236

237
            if ($spaceAfterOpen !== $requiredSpacesAfterOpen) {
3✔
238
                $error = 'Expected %s spaces after opening parenthesis; %s found';
3✔
239
                $data  = [
1✔
240
                    $requiredSpacesAfterOpen,
3✔
241
                    $spaceAfterOpen,
3✔
242
                ];
2✔
243
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpenBracket', $data);
3✔
244
                if ($fix === true) {
3✔
245
                    $padding = str_repeat(' ', $requiredSpacesAfterOpen);
3✔
246
                    if ($spaceAfterOpen === 0) {
3✔
247
                        $phpcsFile->fixer->addContent($openBracket, $padding);
3✔
248
                    } else {
1✔
249
                        $phpcsFile->fixer->replaceToken(($openBracket + 1), $padding);
3✔
250
                    }
251
                }
1✔
252
            }
1✔
253
        }//end if
1✔
254

255
        // Checking this: $value = my_function(...[*]).
256
        $spaceBeforeClose = 0;
3✔
257
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($closer - 1), $openBracket, true);
3✔
258
        if ($tokens[$prev]['code'] === T_END_HEREDOC || $tokens[$prev]['code'] === T_END_NOWDOC) {
3✔
259
            // Need a newline after these tokens, so ignore this rule.
260
            return;
×
261
        }
262

263
        if ($tokens[$prev]['line'] !== $tokens[$closer]['line']) {
3✔
264
            $spaceBeforeClose = 'newline';
×
265
        } else if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) {
3✔
266
            $spaceBeforeClose = $tokens[($closer - 1)]['length'];
3✔
267
        }
1✔
268

269
        if ($spaceBeforeClose !== $requiredSpacesBeforeClose) {
3✔
270
            $error = 'Expected %s spaces before closing parenthesis; %s found';
3✔
271
            $data  = [
1✔
272
                $requiredSpacesBeforeClose,
3✔
273
                $spaceBeforeClose,
3✔
274
            ];
2✔
275
            $fix   = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeCloseBracket', $data);
3✔
276
            if ($fix === true) {
3✔
277
                $padding = str_repeat(' ', $requiredSpacesBeforeClose);
3✔
278

279
                if ($spaceBeforeClose === 0) {
3✔
280
                    $phpcsFile->fixer->addContentBefore($closer, $padding);
3✔
281
                } else if ($spaceBeforeClose === 'newline') {
3✔
282
                    $phpcsFile->fixer->beginChangeset();
×
283

284
                    $closingContent = ')';
×
285

286
                    $next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), null, true);
×
287
                    if ($tokens[$next]['code'] === T_SEMICOLON) {
×
288
                        $closingContent .= ';';
×
289
                        for ($i = ($closer + 1); $i <= $next; $i++) {
×
290
                            $phpcsFile->fixer->replaceToken($i, '');
×
291
                        }
292
                    }
293

294
                    // We want to jump over any whitespace or inline comment and
295
                    // move the closing parenthesis after any other token.
296
                    $prev = ($closer - 1);
×
297
                    while (isset(Tokens::$emptyTokens[$tokens[$prev]['code']]) === true) {
×
298
                        if (($tokens[$prev]['code'] === T_COMMENT)
×
299
                            && (strpos($tokens[$prev]['content'], '*/') !== false)
×
300
                        ) {
301
                            break;
×
302
                        }
303

304
                        $prev--;
×
305
                    }
306

307
                    $phpcsFile->fixer->addContent($prev, $padding.$closingContent);
×
308

309
                    $prevNonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($closer - 1), null, true);
×
310
                    for ($i = ($prevNonWhitespace + 1); $i <= $closer; $i++) {
×
311
                        $phpcsFile->fixer->replaceToken($i, '');
×
312
                    }
313

314
                    $phpcsFile->fixer->endChangeset();
×
315
                } else {
316
                    $phpcsFile->fixer->replaceToken(($closer - 1), $padding);
3✔
317
                }//end if
318
            }//end if
1✔
319
        }//end if
1✔
320

321
    }//end processSingleLineCall()
2✔
322

323

324
    /**
325
     * Processes multi-line calls.
326
     *
327
     * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
328
     * @param int                         $stackPtr    The position of the current token
329
     *                                                 in the stack passed in $tokens.
330
     * @param int                         $openBracket The position of the opening bracket
331
     *                                                 in the stack passed in $tokens.
332
     * @param array                       $tokens      The stack of tokens that make up
333
     *                                                 the file.
334
     *
335
     * @return void
336
     */
337
    public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $tokens)
3✔
338
    {
339
        // We need to work out how far indented the function
340
        // call itself is, so we can work out how far to
341
        // indent the arguments.
342
        $first = $phpcsFile->findFirstOnLine([T_WHITESPACE, T_INLINE_HTML], $stackPtr, true);
3✔
343
        if ($first !== false
2✔
344
            && $tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING
3✔
345
            && $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING
3✔
346
        ) {
1✔
347
            // We are in a multi-line string, so find the start and use
348
            // the indent from there.
349
            $prev  = $phpcsFile->findPrevious(T_CONSTANT_ENCAPSED_STRING, ($first - 2), null, true);
3✔
350
            $first = $phpcsFile->findFirstOnLine(Tokens::$emptyTokens, $prev, true);
3✔
351
            if ($first === false) {
3✔
352
                $first = ($prev + 1);
3✔
353
            }
1✔
354
        }
1✔
355

356
        $foundFunctionIndent = 0;
3✔
357
        if ($first !== false) {
3✔
358
            if ($tokens[$first]['code'] === T_INLINE_HTML
3✔
359
                || ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING
3✔
360
                && $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING)
3✔
361
            ) {
1✔
362
                $trimmed = ltrim($tokens[$first]['content']);
3✔
363
                if ($trimmed === '') {
3✔
UNCOV
364
                    $foundFunctionIndent = strlen($tokens[$first]['content']);
×
365
                } else {
366
                    $foundFunctionIndent = (strlen($tokens[$first]['content']) - strlen($trimmed));
3✔
367
                }
368
            } else {
1✔
369
                $foundFunctionIndent = ($tokens[$first]['column'] - 1);
3✔
370
            }
371
        }
1✔
372

373
        // Make sure the function indent is divisible by the indent size.
374
        // We round down here because this accounts for times when the
375
        // surrounding code is indented a little too far in, and not correctly
376
        // at a tab stop. Without this, the function will be indented a further
377
        // $indent spaces to the right.
378
        $functionIndent = (int) (floor($foundFunctionIndent / $this->indent) * $this->indent);
3✔
379
        $adjustment     = ($functionIndent - $foundFunctionIndent);
3✔
380

381
        if ($foundFunctionIndent !== $functionIndent) {
3✔
382
            $this->complainOpenStatementWrongIndent(
3✔
383
                $phpcsFile,
3✔
384
                $first,
3✔
385
                $tokens,
3✔
386
                $functionIndent,
3✔
387
                $foundFunctionIndent
2✔
388
            );
2✔
389
        }//end if
1✔
390

391
        $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
3✔
392
        if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
3✔
393
            $error = 'Opening parenthesis of a multi-line function call must be the last content on the line';
3✔
394
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpenBracket');
3✔
395
            if ($fix === true) {
3✔
396
                $phpcsFile->fixer->addContent(
3✔
397
                    $openBracket,
3✔
398
                    $phpcsFile->eolChar.str_repeat(' ', ($foundFunctionIndent + $this->indent))
3✔
399
                );
2✔
400
            }
1✔
401
        }
1✔
402

403
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
3✔
404
        $prev         = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true);
3✔
405
        if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
3✔
406
            $error = 'Closing parenthesis of a multi-line function call must be on a line by itself';
3✔
407
            $fix   = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine');
3✔
408
            if ($fix === true) {
3✔
409
                $phpcsFile->fixer->addContentBefore(
3✔
410
                    $closeBracket,
3✔
411
                    $phpcsFile->eolChar.str_repeat(' ', ($foundFunctionIndent + $this->indent))
3✔
412
                );
2✔
413
            }
1✔
414
        }
1✔
415

416
        // Each line between the parenthesis should be indented n spaces.
417
        $lastLine = ($tokens[$openBracket]['line'] - 1);
3✔
418
        $argStart = null;
3✔
419
        $argEnd   = null;
3✔
420

421
        // Start processing at the first argument.
422
        $i = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);
3✔
423

424
        if ($tokens[$i]['line'] > ($tokens[$openBracket]['line'] + 1)) {
3✔
425
            $error = 'The first argument in a multi-line function call must be on the line after the opening parenthesis';
3✔
426
            $fix   = $phpcsFile->addFixableError($error, $i, 'FirstArgumentPosition');
3✔
427
            if ($fix === true) {
3✔
428
                $phpcsFile->fixer->beginChangeset();
3✔
429
                for ($x = ($openBracket + 1); $x < $i; $x++) {
3✔
430
                    if ($tokens[$x]['line'] === $tokens[$openBracket]['line']) {
3✔
431
                        continue;
3✔
432
                    }
433

434
                    if ($tokens[$x]['line'] === $tokens[$i]['line']) {
3✔
435
                        break;
3✔
436
                    }
437

438
                    $phpcsFile->fixer->replaceToken($x, '');
3✔
439
                }
1✔
440

441
                $phpcsFile->fixer->endChangeset();
3✔
442
            }
1✔
443
        }//end if
1✔
444

445
        $i = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
3✔
446

447
        if ($tokens[($i - 1)]['code'] === T_WHITESPACE
3✔
448
            && $tokens[($i - 1)]['line'] === $tokens[$i]['line']
3✔
449
        ) {
1✔
450
            // Make sure we check the indent.
451
            $i--;
3✔
452
        }
1✔
453

454
        for (; $i < $closeBracket; $i++) {
3✔
455
            if ($i > $argStart && $i < $argEnd) {
3✔
456
                $inArg = true;
3✔
457
            } else {
1✔
458
                $inArg = false;
3✔
459
            }
460

461
            if ($tokens[$i]['line'] !== $lastLine) {
3✔
462
                $lastLine = $tokens[$i]['line'];
3✔
463

464
                // Ignore heredoc indentation.
465
                if (isset(Tokens::$heredocTokens[$tokens[$i]['code']]) === true) {
3✔
466
                    continue;
3✔
467
                }
468

469
                // Ignore multi-line string indentation.
470
                if (isset(Tokens::$stringTokens[$tokens[$i]['code']]) === true
3✔
471
                    && $tokens[$i]['code'] === $tokens[($i - 1)]['code']
3✔
472
                ) {
1✔
473
                    continue;
3✔
474
                }
475

476
                // Ignore inline HTML.
477
                if ($tokens[$i]['code'] === T_INLINE_HTML) {
3✔
478
                    continue;
3✔
479
                }
480

481
                if ($tokens[$i]['line'] !== $tokens[$openBracket]['line']) {
3✔
482
                    // We changed lines, so this should be a whitespace indent token, but first make
483
                    // sure it isn't a blank line because we don't need to check indent unless there
484
                    // is actually some code to indent.
485
                    if ($tokens[$i]['code'] === T_WHITESPACE) {
3✔
486
                        $nextCode = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), ($closeBracket + 1), true);
3✔
487
                        if ($tokens[$nextCode]['line'] !== $lastLine) {
3✔
488
                            if ($inArg === false) {
3✔
489
                                $error = 'Empty lines are not allowed in multi-line function calls';
3✔
490
                                $fix   = $phpcsFile->addFixableError($error, $i, 'EmptyLine');
3✔
491
                                if ($fix === true) {
3✔
492
                                    $phpcsFile->fixer->replaceToken($i, '');
3✔
493
                                }
1✔
494
                            }
1✔
495

496
                            continue;
3✔
497
                        }
498
                    } else {
1✔
499
                        $nextCode = $i;
3✔
500
                    }
501

502
                    if ($tokens[$nextCode]['line'] === $tokens[$closeBracket]['line']) {
3✔
503
                        // Closing brace needs to be indented to the same level
504
                        // as the function call.
505
                        $inArg          = false;
3✔
506
                        $expectedIndent = ($foundFunctionIndent + $adjustment);
3✔
507
                    } else {
1✔
508
                        $expectedIndent = ($foundFunctionIndent + $this->indent + $adjustment);
3✔
509
                    }
510

511
                    if ($tokens[$i]['code'] !== T_WHITESPACE
3✔
512
                        && $tokens[$i]['code'] !== T_DOC_COMMENT_WHITESPACE
3✔
513
                    ) {
1✔
514
                        // Just check if it is a multi-line block comment. If so, we can
515
                        // calculate the indent from the whitespace before the content.
516
                        if ($tokens[$i]['code'] === T_COMMENT
3✔
517
                            && $tokens[($i - 1)]['code'] === T_COMMENT
3✔
518
                        ) {
1✔
519
                            $content       = $tokens[$i]['content'];
3✔
520
                            $trimmedLength = strlen(ltrim($content));
3✔
521
                            if ($trimmedLength === 0) {
3✔
522
                                // This is a blank comment line, so indenting it is
523
                                // pointless.
524
                                continue;
3✔
525
                            }
526

527
                            $content     = str_replace("\t", str_repeat(' ', $this->indent), $content);
3✔
528
                            $foundIndent = (strlen($content) - $trimmedLength ) ;
3✔
529
                        } else {
1✔
530
                            $foundIndent = 0;
3✔
531
                        }
532
                    } else {
1✔
533
                        $content     = str_replace("\t", str_repeat(' ', $this->indent), $tokens[$i]['content']);
3✔
534
                        $foundIndent = strlen($content);
3✔
535
                    }//end if
536

537
                    $indentCorrect = true;
3✔
538

539
                    if ($foundIndent < $expectedIndent) {
3✔
540
                        $indentCorrect = false;
3✔
541
                    } else if ($inArg === false && $expectedIndent !== $foundIndent) {
3✔
542
                        $indentCorrect = false;
3✔
543

544
                        // It is permitted to indent chains further than one tab stop to
545
                        // align vertically with the previous method call.
546
                        if ($i === ($closeBracket - 1)) {
3✔
547
                            if ($foundIndent === $foundFunctionIndent) {
3✔
548
                                // This is the closing paren; it lines up vertically with the opening paren.
549
                                $indentCorrect = true;
3✔
550
                            }
1✔
551
                        } else {
1✔
552
                            if ($foundIndent === ($tokens[$openBracket]['column'] - 1)) {
3✔
553
                                // This is a parameter; it lines up vertically with the opening paren.
554
                                $indentCorrect = true;
3✔
555
                            }
1✔
556

557
                            if ($foundIndent === ($foundFunctionIndent + ($this->indent))) {
3✔
558
                                // This is a parameter; it is indented one more step than the function call around it.
559
                                $indentCorrect = true;
3✔
560
                            }
1✔
561
                        }
562
                    }//end if
1✔
563

564
                    if ($indentCorrect === false) {
3✔
565
                        $error = 'Multi-line function call not indented correctly; expected %s spaces but found %s';
3✔
566
                        $data  = [
1✔
567
                            $expectedIndent,
3✔
568
                            $foundIndent,
3✔
569
                        ];
2✔
570

571
                        $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data);
3✔
572
                        if ($fix === true) {
3✔
573
                            $phpcsFile->fixer->beginChangeset();
3✔
574

575
                            $padding = str_repeat(' ', $expectedIndent);
3✔
576
                            if ($foundIndent === 0) {
3✔
577
                                $phpcsFile->fixer->addContentBefore($i, $padding);
3✔
578
                                if (isset($tokens[$i]['scope_opener']) === true) {
3✔
579
                                    $phpcsFile->fixer->changeCodeBlockIndent($i, $tokens[$i]['scope_closer'], $expectedIndent);
1✔
580
                                }
581
                            } else {
1✔
582
                                if ($tokens[$i]['code'] === T_COMMENT) {
3✔
583
                                    $comment = $padding.ltrim($tokens[$i]['content']);
×
584
                                    $phpcsFile->fixer->replaceToken($i, $comment);
×
585
                                } else {
586
                                    $phpcsFile->fixer->replaceToken($i, $padding);
3✔
587
                                }
588

589
                                if (isset($tokens[($i + 1)]['scope_opener']) === true) {
3✔
590
                                    $phpcsFile->fixer->changeCodeBlockIndent(($i + 1), $tokens[($i + 1)]['scope_closer'], ($expectedIndent - $foundIndent));
3✔
591
                                }
1✔
592
                            }
593

594
                            $phpcsFile->fixer->endChangeset();
3✔
595
                        }//end if
1✔
596
                    }//end if
1✔
597
                } else {
1✔
598
                    $nextCode = $i;
3✔
599
                }//end if
600

601
                if ($inArg === false) {
3✔
602
                    $argStart = $nextCode;
3✔
603
                    $argEnd   = $phpcsFile->findEndOfStatement($nextCode, [T_COLON]);
3✔
604
                }
1✔
605
            }//end if
1✔
606

607
            // If we are within an argument we should be ignoring commas
608
            // as these are not signalling the end of an argument.
609
            if ($inArg === false && $tokens[$i]['code'] === T_COMMA) {
3✔
610
                $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), $closeBracket, true);
3✔
611
                if ($next === false) {
3✔
612
                    continue;
3✔
613
                }
614

615
                if ($this->allowMultipleArguments === false) {
3✔
616
                    // Comma has to be the last token on the line.
617
                    if ($tokens[$i]['line'] === $tokens[$next]['line']) {
3✔
618
                        $error = 'Only one argument is allowed per line in a multi-line function call';
3✔
619
                        $fix   = $phpcsFile->addFixableError($error, $next, 'MultipleArguments');
3✔
620
                        if ($fix === true) {
3✔
621
                            $phpcsFile->fixer->beginChangeset();
3✔
622
                            for ($x = ($next - 1); $x > $i; $x--) {
3✔
623
                                if ($tokens[$x]['code'] !== T_WHITESPACE) {
3✔
624
                                    break;
3✔
625
                                }
626

627
                                $phpcsFile->fixer->replaceToken($x, '');
3✔
628
                            }
1✔
629

630
                            $phpcsFile->fixer->addContentBefore(
3✔
631
                                $next,
3✔
632
                                $phpcsFile->eolChar.str_repeat(' ', ($foundFunctionIndent + $this->indent))
3✔
633
                            );
2✔
634
                            $phpcsFile->fixer->endChangeset();
3✔
635
                        }
1✔
636
                    }
1✔
637
                }//end if
1✔
638

639
                $argStart = $next;
3✔
640
                $argEnd   = $phpcsFile->findEndOfStatement($next, [T_COLON]);
3✔
641
            }//end if
1✔
642
        }//end for
1✔
643

644
    }//end processMultiLineCall()
2✔
645

646

647
    /**
648
     * Add a complaint (and auto-fix) if the function indent is 'wrong'.
649
     *
650
     * @param File  $phpcsFile           The file being scanned.
651
     * @param int   $first               Pointer to the first empty token on this line.
652
     * @param array $tokens              The stack of tokens that make up the file.
653
     * @param int   $functionIndent      The expected indent for this function definition.
654
     * @param int   $foundFunctionIndent The actual indent for this function definition.
655
     *
656
     * @return void
657
     */
658
    protected function complainOpenStatementWrongIndent(
3✔
659
        $phpcsFile,
660
        $first,
661
        $tokens,
662
        $functionIndent,
663
        $foundFunctionIndent
664
    ) {
665
        if ($foundFunctionIndent === $functionIndent) {
3✔
NEW
666
            return;
×
667
        }
668

669
        $firstToken = $tokens[$first];
3✔
670
        if ($firstToken['code'] === T_OPEN_TAG || $firstToken['code'] === T_OPEN_TAG_WITH_ECHO) {
3✔
671
            $prevToken = $tokens[($first - 1)];
3✔
672
            if ($prevToken !== false && $firstToken['line'] === $prevToken['line']) {
3✔
673
                return;
3✔
674
            }
675
        }
676

677
        $error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
3✔
678
        $data  = [
1✔
679
            $functionIndent,
3✔
680
            $foundFunctionIndent,
3✔
681
        ];
2✔
682

683
        $fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
3✔
684
        if ($fix !== true) {
3✔
685
            return;
3✔
686
        }
687

688
        $padding = str_repeat(' ', $functionIndent);
3✔
689

690
        if ($foundFunctionIndent === 0) {
3✔
NEW
691
            $phpcsFile->fixer->addContentBefore($first, $padding);
×
692
        } else if ($tokens[$first]['code'] === T_INLINE_HTML) {
3✔
NEW
693
            $newContent = $padding.ltrim($tokens[$first]['content']);
×
NEW
694
            $phpcsFile->fixer->replaceToken($first, $newContent);
×
695
        } else if ($tokens[($first - 1)]['code'] === T_WHITESPACE) {
3✔
696
            $phpcsFile->fixer->replaceToken(($first - 1), $padding);
3✔
697
        }
1✔
698

699
    }//end complainOpenStatementWrongIndent()
2✔
700

701

702
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc