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

PHPCSStandards / PHP_CodeSniffer / 17662127818

12 Sep 2025 01:50AM UTC coverage: 78.786%. Remained the same
17662127818

push

github

web-flow
Merge pull request #1241 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-3

CS: normalize code style rules [3]

343 of 705 new or added lines in 108 files covered. (48.65%)

3 existing lines in 3 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

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

91.7
/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
     * The number of spaces code should be indented.
21
     *
22
     * @var integer
23
     */
24
    public $indent = 4;
25

26
    /**
27
     * If TRUE, multiple arguments can be defined per line in a multi-line call.
28
     *
29
     * @var boolean
30
     */
31
    public $allowMultipleArguments = true;
32

33
    /**
34
     * How many spaces should follow the opening bracket.
35
     *
36
     * @var integer
37
     */
38
    public $requiredSpacesAfterOpen = 0;
39

40
    /**
41
     * How many spaces should precede the closing bracket.
42
     *
43
     * @var integer
44
     */
45
    public $requiredSpacesBeforeClose = 0;
46

47

48
    /**
49
     * Returns an array of tokens this test wants to listen for.
50
     *
51
     * @return array<int|string>
52
     */
53
    public function register()
3✔
54
    {
55
        $tokens = Tokens::FUNCTION_NAME_TOKENS;
3✔
56

57
        $tokens[] = T_VARIABLE;
3✔
58
        $tokens[] = T_CLOSE_CURLY_BRACKET;
3✔
59
        $tokens[] = T_CLOSE_SQUARE_BRACKET;
3✔
60
        $tokens[] = T_CLOSE_PARENTHESIS;
3✔
61

62
        return $tokens;
3✔
63

64
    }//end register()
65

66

67
    /**
68
     * Processes this test, when one of its tokens is encountered.
69
     *
70
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
71
     * @param int                         $stackPtr  The position of the current token
72
     *                                               in the stack passed in $tokens.
73
     *
74
     * @return void
75
     */
76
    public function process(File $phpcsFile, int $stackPtr)
3✔
77
    {
78
        $this->requiredSpacesAfterOpen   = (int) $this->requiredSpacesAfterOpen;
3✔
79
        $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
3✔
80
        $tokens = $phpcsFile->getTokens();
3✔
81

82
        if ($tokens[$stackPtr]['code'] === T_CLOSE_CURLY_BRACKET
3✔
83
            && isset($tokens[$stackPtr]['scope_condition']) === true
3✔
84
        ) {
85
            // Not a function call.
86
            return;
3✔
87
        }
88

89
        // Find the next non-empty token.
90
        $openBracket = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($stackPtr + 1), null, true);
3✔
91

92
        if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
3✔
93
            // Not a function call.
94
            return;
3✔
95
        }
96

97
        if (isset($tokens[$openBracket]['parenthesis_closer']) === false) {
3✔
98
            // Not a function call.
99
            return;
×
100
        }
101

102
        // Find the previous non-empty token.
103
        $search   = Tokens::EMPTY_TOKENS;
3✔
104
        $search[] = T_BITWISE_AND;
3✔
105
        $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true);
3✔
106
        if ($tokens[$previous]['code'] === T_FUNCTION) {
3✔
107
            // It's a function definition, not a function call.
108
            return;
3✔
109
        }
110

111
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
3✔
112

113
        if ($tokens[$stackPtr]['code'] !== T_ANON_CLASS
3✔
114
            && ($stackPtr + 1) !== $openBracket
3✔
115
        ) {
116
            // Checking this: $value = my_function[*](...).
117
            $error = 'Space before opening parenthesis of function call prohibited';
3✔
118
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeOpenBracket');
3✔
119
            if ($fix === true) {
3✔
120
                $phpcsFile->fixer->beginChangeset();
3✔
121
                for ($i = ($stackPtr + 1); $i < $openBracket; $i++) {
3✔
122
                    $phpcsFile->fixer->replaceToken($i, '');
3✔
123
                }
124

125
                // Modify the bracket as well to ensure a conflict if the bracket
126
                // has been changed in some way by another sniff.
127
                $phpcsFile->fixer->replaceToken($openBracket, '(');
3✔
128
                $phpcsFile->fixer->endChangeset();
3✔
129
            }
130
        }
131

132
        $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
3✔
133
        if ($tokens[$next]['code'] === T_SEMICOLON) {
3✔
134
            if (isset(Tokens::EMPTY_TOKENS[$tokens[($closeBracket + 1)]['code']]) === true) {
3✔
135
                $error = 'Space after closing parenthesis of function call prohibited';
3✔
136
                $fix   = $phpcsFile->addFixableError($error, $closeBracket, 'SpaceAfterCloseBracket');
3✔
137
                if ($fix === true) {
3✔
138
                    $phpcsFile->fixer->beginChangeset();
3✔
139
                    for ($i = ($closeBracket + 1); $i < $next; $i++) {
3✔
140
                        $phpcsFile->fixer->replaceToken($i, '');
3✔
141
                    }
142

143
                    // Modify the bracket as well to ensure a conflict if the bracket
144
                    // has been changed in some way by another sniff.
145
                    $phpcsFile->fixer->replaceToken($closeBracket, ')');
3✔
146
                    $phpcsFile->fixer->endChangeset();
3✔
147
                }
148
            }
149
        }
150

151
        // Check if this is a single line or multi-line function call.
152
        if ($this->isMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens) === true) {
3✔
153
            $this->processMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
3✔
154
        } else {
155
            $this->processSingleLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
3✔
156
        }
157

158
    }//end process()
1✔
159

160

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

181
        return false;
3✔
182

183
    }//end isMultiLineCall()
184

185

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

206
        // If the function call has no arguments or comments, enforce 0 spaces.
207
        $next = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), $closer, true);
3✔
208
        if ($next === false) {
3✔
209
            $requiredSpacesAfterOpen   = 0;
3✔
210
            $requiredSpacesBeforeClose = 0;
3✔
211
        } else {
212
            $requiredSpacesAfterOpen   = $this->requiredSpacesAfterOpen;
3✔
213
            $requiredSpacesBeforeClose = $this->requiredSpacesBeforeClose;
3✔
214
        }
215

216
        if ($requiredSpacesAfterOpen === 0 && $tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
3✔
217
            // Checking this: $value = my_function([*]...).
218
            $error = 'Space after opening parenthesis of function call prohibited';
3✔
219
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpenBracket');
3✔
220
            if ($fix === true) {
3✔
221
                $phpcsFile->fixer->replaceToken(($openBracket + 1), '');
3✔
222
            }
223
        } else if ($requiredSpacesAfterOpen > 0) {
3✔
224
            $spaceAfterOpen = 0;
3✔
225
            if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
3✔
226
                $spaceAfterOpen = $tokens[($openBracket + 1)]['length'];
3✔
227
            }
228

229
            if ($spaceAfterOpen !== $requiredSpacesAfterOpen) {
3✔
230
                $error = 'Expected %s spaces after opening parenthesis; %s found';
3✔
231
                $data  = [
2✔
232
                    $requiredSpacesAfterOpen,
3✔
233
                    $spaceAfterOpen,
3✔
234
                ];
2✔
235
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpenBracket', $data);
3✔
236
                if ($fix === true) {
3✔
237
                    $padding = str_repeat(' ', $requiredSpacesAfterOpen);
3✔
238
                    if ($spaceAfterOpen === 0) {
3✔
239
                        $phpcsFile->fixer->addContent($openBracket, $padding);
3✔
240
                    } else {
241
                        $phpcsFile->fixer->replaceToken(($openBracket + 1), $padding);
3✔
242
                    }
243
                }
244
            }
245
        }//end if
246

247
        // Checking this: $value = my_function(...[*]).
248
        $spaceBeforeClose = 0;
3✔
249
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($closer - 1), $openBracket, true);
3✔
250
        if ($tokens[$prev]['code'] === T_END_HEREDOC || $tokens[$prev]['code'] === T_END_NOWDOC) {
3✔
251
            // Need a newline after these tokens, so ignore this rule.
252
            return;
×
253
        }
254

255
        if ($tokens[$prev]['line'] !== $tokens[$closer]['line']) {
3✔
256
            $spaceBeforeClose = 'newline';
×
257
        } else if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) {
3✔
258
            $spaceBeforeClose = $tokens[($closer - 1)]['length'];
3✔
259
        }
260

261
        if ($spaceBeforeClose !== $requiredSpacesBeforeClose) {
3✔
262
            $error = 'Expected %s spaces before closing parenthesis; %s found';
3✔
263
            $data  = [
2✔
264
                $requiredSpacesBeforeClose,
3✔
265
                $spaceBeforeClose,
3✔
266
            ];
2✔
267
            $fix   = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeCloseBracket', $data);
3✔
268
            if ($fix === true) {
3✔
269
                $padding = str_repeat(' ', $requiredSpacesBeforeClose);
3✔
270

271
                if ($spaceBeforeClose === 0) {
3✔
272
                    $phpcsFile->fixer->addContentBefore($closer, $padding);
3✔
273
                } else if ($spaceBeforeClose === 'newline') {
3✔
274
                    $phpcsFile->fixer->beginChangeset();
×
275

276
                    $closingContent = ')';
×
277

278
                    $next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), null, true);
×
279
                    if ($tokens[$next]['code'] === T_SEMICOLON) {
×
280
                        $closingContent .= ';';
×
281
                        for ($i = ($closer + 1); $i <= $next; $i++) {
×
282
                            $phpcsFile->fixer->replaceToken($i, '');
×
283
                        }
284
                    }
285

286
                    // We want to jump over any whitespace or inline comment and
287
                    // move the closing parenthesis after any other token.
288
                    $prev = ($closer - 1);
×
289
                    while (isset(Tokens::EMPTY_TOKENS[$tokens[$prev]['code']]) === true) {
×
290
                        if (($tokens[$prev]['code'] === T_COMMENT)
×
291
                            && (strpos($tokens[$prev]['content'], '*/') !== false)
×
292
                        ) {
293
                            break;
×
294
                        }
295

296
                        $prev--;
×
297
                    }
298

NEW
299
                    $phpcsFile->fixer->addContent($prev, $padding . $closingContent);
×
300

301
                    $prevNonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($closer - 1), null, true);
×
302
                    for ($i = ($prevNonWhitespace + 1); $i <= $closer; $i++) {
×
303
                        $phpcsFile->fixer->replaceToken($i, '');
×
304
                    }
305

306
                    $phpcsFile->fixer->endChangeset();
×
307
                } else {
308
                    $phpcsFile->fixer->replaceToken(($closer - 1), $padding);
3✔
309
                }//end if
310
            }//end if
311
        }//end if
312

313
    }//end processSingleLineCall()
1✔
314

315

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

348
        $foundFunctionIndent = 0;
3✔
349
        if ($first !== false) {
3✔
350
            if ($tokens[$first]['code'] === T_INLINE_HTML
3✔
351
                || ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING
3✔
352
                && $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING)
3✔
353
            ) {
354
                $trimmed = ltrim($tokens[$first]['content']);
3✔
355
                if ($trimmed === '') {
3✔
356
                    $foundFunctionIndent = strlen($tokens[$first]['content']);
3✔
357
                } else {
358
                    $foundFunctionIndent = (strlen($tokens[$first]['content']) - strlen($trimmed));
3✔
359
                }
360
            } else {
361
                $foundFunctionIndent = ($tokens[$first]['column'] - 1);
3✔
362
            }
363
        }
364

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($foundFunctionIndent / $this->indent) * $this->indent);
3✔
371
        $adjustment     = 0;
3✔
372

373
        if ($foundFunctionIndent !== $functionIndent) {
3✔
374
            $error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
3✔
375
            $data  = [
2✔
376
                $functionIndent,
3✔
377
                $foundFunctionIndent,
3✔
378
            ];
2✔
379

380
            $fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
3✔
381
            if ($fix === true) {
3✔
382
                // Set adjustment for use later to determine whether argument indentation is correct when fixing.
383
                $adjustment = ($functionIndent - $foundFunctionIndent);
3✔
384

385
                $padding = str_repeat(' ', $functionIndent);
3✔
386
                if ($foundFunctionIndent === 0) {
3✔
387
                    $phpcsFile->fixer->addContentBefore($first, $padding);
×
388
                } else if ($tokens[$first]['code'] === T_INLINE_HTML) {
3✔
389
                    $newContent = $padding . ltrim($tokens[$first]['content']);
3✔
390
                    $phpcsFile->fixer->replaceToken($first, $newContent);
3✔
391
                } else {
392
                    $phpcsFile->fixer->replaceToken(($first - 1), $padding);
3✔
393
                }
394
            }
395
        }//end if
396

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

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

422
        // Each line between the parenthesis should be indented n spaces.
423
        $lastLine = ($tokens[$openBracket]['line'] - 1);
3✔
424
        $argStart = null;
3✔
425
        $argEnd   = null;
3✔
426

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

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

440
                    if ($tokens[$x]['line'] === $tokens[$i]['line']) {
3✔
441
                        break;
3✔
442
                    }
443

444
                    $phpcsFile->fixer->replaceToken($x, '');
3✔
445
                }
446

447
                $phpcsFile->fixer->endChangeset();
3✔
448
            }
449
        }//end if
450

451
        $i = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($openBracket + 1), null, true);
3✔
452

453
        if ($tokens[($i - 1)]['code'] === T_WHITESPACE
3✔
454
            && $tokens[($i - 1)]['line'] === $tokens[$i]['line']
3✔
455
        ) {
456
            // Make sure we check the indent.
457
            $i--;
3✔
458
        }
459

460
        for ($i; $i < $closeBracket; $i++) {
3✔
461
            if ($i > $argStart && $i < $argEnd) {
3✔
462
                $inArg = true;
3✔
463
            } else {
464
                $inArg = false;
3✔
465
            }
466

467
            if ($tokens[$i]['line'] !== $lastLine) {
3✔
468
                $lastLine = $tokens[$i]['line'];
3✔
469

470
                // Ignore heredoc indentation.
471
                if (isset(Tokens::HEREDOC_TOKENS[$tokens[$i]['code']]) === true) {
3✔
472
                    continue;
3✔
473
                }
474

475
                // Ignore multi-line string indentation.
476
                if (isset(Tokens::STRING_TOKENS[$tokens[$i]['code']]) === true
3✔
477
                    && $tokens[$i]['code'] === $tokens[($i - 1)]['code']
3✔
478
                ) {
479
                    continue;
3✔
480
                }
481

482
                // Ignore inline HTML.
483
                if ($tokens[$i]['code'] === T_INLINE_HTML) {
3✔
484
                    continue;
3✔
485
                }
486

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

502
                            continue;
3✔
503
                        }
504
                    } else {
505
                        $nextCode = $i;
3✔
506
                    }
507

508
                    if ($tokens[$nextCode]['line'] === $tokens[$closeBracket]['line']) {
3✔
509
                        // Closing brace needs to be indented to the same level
510
                        // as the function call.
511
                        $inArg          = false;
3✔
512
                        $expectedIndent = ($foundFunctionIndent + $adjustment);
3✔
513
                    } else {
514
                        $expectedIndent = ($foundFunctionIndent + $this->indent + $adjustment);
3✔
515
                    }
516

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

532
                            $foundIndent = (strlen($tokens[$i]['content']) - $trimmedLength);
3✔
533
                        } else {
534
                            $foundIndent = 0;
3✔
535
                        }
536
                    } else {
537
                        $foundIndent = $tokens[$i]['length'];
3✔
538
                    }
539

540
                    if ($foundIndent < $expectedIndent
3✔
541
                        || ($inArg === false
3✔
542
                        && $expectedIndent !== $foundIndent)
3✔
543
                    ) {
544
                        $error = 'Multi-line function call not indented correctly; expected %s spaces but found %s';
3✔
545
                        $data  = [
2✔
546
                            $expectedIndent,
3✔
547
                            $foundIndent,
3✔
548
                        ];
2✔
549

550
                        $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data);
3✔
551
                        if ($fix === true) {
3✔
552
                            $phpcsFile->fixer->beginChangeset();
3✔
553

554
                            $padding = str_repeat(' ', $expectedIndent);
3✔
555
                            if ($foundIndent === 0) {
3✔
556
                                $phpcsFile->fixer->addContentBefore($i, $padding);
3✔
557
                                if (isset($tokens[$i]['scope_opener']) === true) {
3✔
558
                                    $phpcsFile->fixer->changeCodeBlockIndent($i, $tokens[$i]['scope_closer'], $expectedIndent);
1✔
559
                                }
560
                            } else {
561
                                if ($tokens[$i]['code'] === T_COMMENT) {
3✔
NEW
562
                                    $comment = $padding . ltrim($tokens[$i]['content']);
×
563
                                    $phpcsFile->fixer->replaceToken($i, $comment);
×
564
                                } else {
565
                                    $phpcsFile->fixer->replaceToken($i, $padding);
3✔
566
                                }
567

568
                                if (isset($tokens[($i + 1)]['scope_opener']) === true) {
3✔
569
                                    $phpcsFile->fixer->changeCodeBlockIndent(($i + 1), $tokens[($i + 1)]['scope_closer'], ($expectedIndent - $foundIndent));
3✔
570
                                }
571
                            }
572

573
                            $phpcsFile->fixer->endChangeset();
3✔
574
                        }//end if
575
                    }//end if
576
                } else {
577
                    $nextCode = $i;
3✔
578
                }//end if
579

580
                if ($inArg === false) {
3✔
581
                    $argStart = $nextCode;
3✔
582
                    $argEnd   = $phpcsFile->findEndOfStatement($nextCode, [T_COLON]);
3✔
583
                }
584
            }//end if
585

586
            // If we are within an argument we should be ignoring commas
587
            // as these are not signalling the end of an argument.
588
            if ($inArg === false && $tokens[$i]['code'] === T_COMMA) {
3✔
589
                $next = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($i + 1), $closeBracket, true);
3✔
590
                if ($next === false) {
3✔
591
                    continue;
3✔
592
                }
593

594
                if ($this->allowMultipleArguments === false) {
3✔
595
                    // Comma has to be the last token on the line.
596
                    if ($tokens[$i]['line'] === $tokens[$next]['line']) {
3✔
597
                        $error = 'Only one argument is allowed per line in a multi-line function call';
3✔
598
                        $fix   = $phpcsFile->addFixableError($error, $next, 'MultipleArguments');
3✔
599
                        if ($fix === true) {
3✔
600
                            $phpcsFile->fixer->beginChangeset();
3✔
601
                            for ($x = ($next - 1); $x > $i; $x--) {
3✔
602
                                if ($tokens[$x]['code'] !== T_WHITESPACE) {
3✔
603
                                    break;
3✔
604
                                }
605

606
                                $phpcsFile->fixer->replaceToken($x, '');
3✔
607
                            }
608

609
                            $phpcsFile->fixer->addContentBefore(
3✔
610
                                $next,
3✔
611
                                $phpcsFile->eolChar . str_repeat(' ', ($foundFunctionIndent + $this->indent))
3✔
612
                            );
2✔
613
                            $phpcsFile->fixer->endChangeset();
3✔
614
                        }
615
                    }
616
                }//end if
617

618
                $argStart = $next;
3✔
619
                $argEnd   = $phpcsFile->findEndOfStatement($next, [T_COLON]);
3✔
620
            }//end if
621
        }//end for
622

623
    }//end processMultiLineCall()
1✔
624

625

626
}//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