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

PHPCSStandards / PHP_CodeSniffer / 17339559314

30 Aug 2025 04:49AM UTC coverage: 79.152% (+0.6%) from 78.578%
17339559314

Pull #1204

github

web-flow
Merge c09bbe9d8 into ca606d9f6
Pull Request #1204: Add option to allow heredoc after function open bracket

3 of 5 new or added lines in 1 file covered. (60.0%)

1 existing line in 1 file now uncovered.

25293 of 31955 relevant lines covered (79.15%)

70.58 hits per line

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

92.66
/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
     * Allows Heredoc and Nowdoc to start after the opening bracket.
59
     *
60
     * @var boolean
61
     */
62
    public $allowHereDocAfterOpenBracket = false;
63

64

65
    /**
66
     * Returns an array of tokens this test wants to listen for.
67
     *
68
     * @return array<int|string>
69
     */
70
    public function register()
3✔
71
    {
72
        $tokens = Tokens::$functionNameTokens;
3✔
73

74
        $tokens[] = T_VARIABLE;
3✔
75
        $tokens[] = T_CLOSE_CURLY_BRACKET;
3✔
76
        $tokens[] = T_CLOSE_SQUARE_BRACKET;
3✔
77
        $tokens[] = T_CLOSE_PARENTHESIS;
3✔
78

79
        return $tokens;
3✔
80

81
    }//end register()
82

83

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

99
        if ($tokens[$stackPtr]['code'] === T_CLOSE_CURLY_BRACKET
3✔
100
            && isset($tokens[$stackPtr]['scope_condition']) === true
3✔
101
        ) {
1✔
102
            // Not a function call.
103
            return;
3✔
104
        }
105

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

109
        if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
3✔
110
            // Not a function call.
111
            return;
3✔
112
        }
113

114
        if (isset($tokens[$openBracket]['parenthesis_closer']) === false) {
3✔
115
            // Not a function call.
116
            return;
×
117
        }
118

119
        // Find the previous non-empty token.
120
        $search   = Tokens::$emptyTokens;
3✔
121
        $search[] = T_BITWISE_AND;
3✔
122
        $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true);
3✔
123
        if ($tokens[$previous]['code'] === T_FUNCTION) {
3✔
124
            // It's a function definition, not a function call.
125
            return;
3✔
126
        }
127

128
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
3✔
129

130
        if (($stackPtr + 1) !== $openBracket) {
3✔
131
            // Checking this: $value = my_function[*](...).
132
            $error = 'Space before opening parenthesis of function call prohibited';
3✔
133
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeOpenBracket');
3✔
134
            if ($fix === true) {
3✔
135
                $phpcsFile->fixer->beginChangeset();
3✔
136
                for ($i = ($stackPtr + 1); $i < $openBracket; $i++) {
3✔
137
                    $phpcsFile->fixer->replaceToken($i, '');
3✔
138
                }
1✔
139

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

147
        $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
3✔
148
        if ($tokens[$next]['code'] === T_SEMICOLON) {
3✔
149
            if (isset(Tokens::$emptyTokens[$tokens[($closeBracket + 1)]['code']]) === true) {
3✔
150
                $error = 'Space after closing parenthesis of function call prohibited';
3✔
151
                $fix   = $phpcsFile->addFixableError($error, $closeBracket, 'SpaceAfterCloseBracket');
3✔
152
                if ($fix === true) {
3✔
153
                    $phpcsFile->fixer->beginChangeset();
3✔
154
                    for ($i = ($closeBracket + 1); $i < $next; $i++) {
3✔
155
                        $phpcsFile->fixer->replaceToken($i, '');
3✔
156
                    }
1✔
157

158
                    // Modify the bracket as well to ensure a conflict if the bracket
159
                    // has been changed in some way by another sniff.
160
                    $phpcsFile->fixer->replaceToken($closeBracket, ')');
3✔
161
                    $phpcsFile->fixer->endChangeset();
3✔
162
                }
1✔
163
            }
1✔
164
        }
1✔
165

166
        // Check if this is a single line or multi-line function call.
167
        if ($this->isMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens) === true) {
3✔
168
            $this->processMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
3✔
169
        } else {
1✔
170
            $this->processSingleLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
3✔
171
        }
172

173
    }//end process()
2✔
174

175

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

196
        return false;
3✔
197

198
    }//end isMultiLineCall()
199

200

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

221
        // If the function call has no arguments or comments, enforce 0 spaces.
222
        $next = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), $closer, true);
3✔
223
        if ($next === false) {
3✔
224
            $requiredSpacesAfterOpen   = 0;
3✔
225
            $requiredSpacesBeforeClose = 0;
3✔
226
        } else {
1✔
227
            $requiredSpacesAfterOpen   = $this->requiredSpacesAfterOpen;
3✔
228
            $requiredSpacesBeforeClose = $this->requiredSpacesBeforeClose;
3✔
229
        }
230

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

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

262
        // Checking this: $value = my_function(...[*]).
263
        $spaceBeforeClose = 0;
3✔
264
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($closer - 1), $openBracket, true);
3✔
265
        if ($tokens[$prev]['code'] === T_END_HEREDOC || $tokens[$prev]['code'] === T_END_NOWDOC) {
3✔
266
            // Need a newline after these tokens, so ignore this rule.
267
            return;
×
268
        }
269

270
        if ($tokens[$prev]['line'] !== $tokens[$closer]['line']) {
3✔
271
            $spaceBeforeClose = 'newline';
×
272
        } else if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) {
3✔
273
            $spaceBeforeClose = $tokens[($closer - 1)]['length'];
3✔
274
        }
1✔
275

276
        if ($spaceBeforeClose !== $requiredSpacesBeforeClose) {
3✔
277
            $error = 'Expected %s spaces before closing parenthesis; %s found';
3✔
278
            $data  = [
1✔
279
                $requiredSpacesBeforeClose,
3✔
280
                $spaceBeforeClose,
3✔
281
            ];
2✔
282
            $fix   = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeCloseBracket', $data);
3✔
283
            if ($fix === true) {
3✔
284
                $padding = str_repeat(' ', $requiredSpacesBeforeClose);
3✔
285

286
                if ($spaceBeforeClose === 0) {
3✔
287
                    $phpcsFile->fixer->addContentBefore($closer, $padding);
3✔
288
                } else if ($spaceBeforeClose === 'newline') {
3✔
289
                    $phpcsFile->fixer->beginChangeset();
×
290

291
                    $closingContent = ')';
×
292

293
                    $next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), null, true);
×
294
                    if ($tokens[$next]['code'] === T_SEMICOLON) {
×
295
                        $closingContent .= ';';
×
296
                        for ($i = ($closer + 1); $i <= $next; $i++) {
×
297
                            $phpcsFile->fixer->replaceToken($i, '');
×
298
                        }
299
                    }
300

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

311
                        $prev--;
×
312
                    }
313

314
                    $phpcsFile->fixer->addContent($prev, $padding.$closingContent);
×
315

316
                    $prevNonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($closer - 1), null, true);
×
317
                    for ($i = ($prevNonWhitespace + 1); $i <= $closer; $i++) {
×
318
                        $phpcsFile->fixer->replaceToken($i, '');
×
319
                    }
320

321
                    $phpcsFile->fixer->endChangeset();
×
322
                } else {
323
                    $phpcsFile->fixer->replaceToken(($closer - 1), $padding);
3✔
324
                }//end if
325
            }//end if
1✔
326
        }//end if
1✔
327

328
    }//end processSingleLineCall()
2✔
329

330

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

363
        $foundFunctionIndent = 0;
3✔
364
        if ($first !== false) {
3✔
365
            if ($tokens[$first]['code'] === T_INLINE_HTML
3✔
366
                || ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING
3✔
367
                && $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING)
3✔
368
            ) {
1✔
369
                $trimmed = ltrim($tokens[$first]['content']);
3✔
370
                if ($trimmed === '') {
3✔
371
                    $foundFunctionIndent = strlen($tokens[$first]['content']);
3✔
372
                } else {
1✔
373
                    $foundFunctionIndent = (strlen($tokens[$first]['content']) - strlen($trimmed));
3✔
374
                }
375
            } else {
1✔
376
                $foundFunctionIndent = ($tokens[$first]['column'] - 1);
3✔
377
            }
378
        }
1✔
379

380
        // Make sure the function indent is divisible by the indent size.
381
        // We round down here because this accounts for times when the
382
        // surrounding code is indented a little too far in, and not correctly
383
        // at a tab stop. Without this, the function will be indented a further
384
        // $indent spaces to the right.
385
        $functionIndent = (int) (floor($foundFunctionIndent / $this->indent) * $this->indent);
3✔
386
        $adjustment     = 0;
3✔
387

388
        if ($foundFunctionIndent !== $functionIndent) {
3✔
389
            $error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
3✔
390
            $data  = [
1✔
391
                $functionIndent,
3✔
392
                $foundFunctionIndent,
3✔
393
            ];
2✔
394

395
            $fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
3✔
396
            if ($fix === true) {
3✔
397
                // Set adjustment for use later to determine whether argument indentation is correct when fixing.
398
                $adjustment = ($functionIndent - $foundFunctionIndent);
3✔
399

400
                $padding = str_repeat(' ', $functionIndent);
3✔
401
                if ($foundFunctionIndent === 0) {
3✔
402
                    $phpcsFile->fixer->addContentBefore($first, $padding);
×
403
                } else if ($tokens[$first]['code'] === T_INLINE_HTML) {
3✔
404
                    $newContent = $padding.ltrim($tokens[$first]['content']);
3✔
405
                    $phpcsFile->fixer->replaceToken($first, $newContent);
3✔
406
                } else {
1✔
407
                    $phpcsFile->fixer->replaceToken(($first - 1), $padding);
3✔
408
                }
409
            }
1✔
410
        }//end if
1✔
411

412
        $nextTokens = Tokens::$emptyTokens;
3✔
413
        if ($this->allowHereDocAfterOpenBracket === true) {
3✔
414
            $nextTokens += [
NEW
415
                T_START_HEREDOC,
×
NEW
416
                T_START_NOWDOC,
×
417
            ];
418
        }
419

420
        $next = $phpcsFile->findNext($nextTokens, ($openBracket + 1), null, true);
3✔
421
        if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
3✔
422
            $error = 'Opening parenthesis of a multi-line function call must be the last content on the line';
3✔
423
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpenBracket');
3✔
424
            if ($fix === true) {
3✔
425
                $phpcsFile->fixer->addContent(
3✔
426
                    $openBracket,
3✔
427
                    $phpcsFile->eolChar.str_repeat(' ', ($foundFunctionIndent + $this->indent))
3✔
428
                );
2✔
429
            }
1✔
430
        }
1✔
431

432
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
3✔
433
        $prev         = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true);
3✔
434
        if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
3✔
435
            $error = 'Closing parenthesis of a multi-line function call must be on a line by itself';
3✔
436
            $fix   = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine');
3✔
437
            if ($fix === true) {
3✔
438
                $phpcsFile->fixer->addContentBefore(
3✔
439
                    $closeBracket,
3✔
440
                    $phpcsFile->eolChar.str_repeat(' ', ($foundFunctionIndent + $this->indent))
3✔
441
                );
2✔
442
            }
1✔
443
        }
1✔
444

445
        // Each line between the parenthesis should be indented n spaces.
446
        $lastLine = ($tokens[$openBracket]['line'] - 1);
3✔
447
        $argStart = null;
3✔
448
        $argEnd   = null;
3✔
449

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

453
        if ($tokens[$i]['line'] > ($tokens[$openBracket]['line'] + 1)) {
3✔
454
            $error = 'The first argument in a multi-line function call must be on the line after the opening parenthesis';
3✔
455
            $fix   = $phpcsFile->addFixableError($error, $i, 'FirstArgumentPosition');
3✔
456
            if ($fix === true) {
3✔
457
                $phpcsFile->fixer->beginChangeset();
3✔
458
                for ($x = ($openBracket + 1); $x < $i; $x++) {
3✔
459
                    if ($tokens[$x]['line'] === $tokens[$openBracket]['line']) {
3✔
460
                        continue;
3✔
461
                    }
462

463
                    if ($tokens[$x]['line'] === $tokens[$i]['line']) {
3✔
464
                        break;
3✔
465
                    }
466

467
                    $phpcsFile->fixer->replaceToken($x, '');
3✔
468
                }
1✔
469

470
                $phpcsFile->fixer->endChangeset();
3✔
471
            }
1✔
472
        }//end if
1✔
473

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

476
        if ($tokens[($i - 1)]['code'] === T_WHITESPACE
3✔
477
            && $tokens[($i - 1)]['line'] === $tokens[$i]['line']
3✔
478
        ) {
1✔
479
            // Make sure we check the indent.
480
            $i--;
3✔
481
        }
1✔
482

483
        for ($i; $i < $closeBracket; $i++) {
3✔
484
            if ($i > $argStart && $i < $argEnd) {
3✔
485
                $inArg = true;
3✔
486
            } else {
1✔
487
                $inArg = false;
3✔
488
            }
489

490
            if ($tokens[$i]['line'] !== $lastLine) {
3✔
491
                $lastLine = $tokens[$i]['line'];
3✔
492

493
                // Ignore heredoc indentation.
494
                if (isset(Tokens::$heredocTokens[$tokens[$i]['code']]) === true) {
3✔
495
                    continue;
3✔
496
                }
497

498
                // Ignore multi-line string indentation.
499
                if (isset(Tokens::$stringTokens[$tokens[$i]['code']]) === true
3✔
500
                    && $tokens[$i]['code'] === $tokens[($i - 1)]['code']
3✔
501
                ) {
1✔
502
                    continue;
3✔
503
                }
504

505
                // Ignore inline HTML.
506
                if ($tokens[$i]['code'] === T_INLINE_HTML) {
3✔
507
                    continue;
3✔
508
                }
509

510
                if ($tokens[$i]['line'] !== $tokens[$openBracket]['line']) {
3✔
511
                    // We changed lines, so this should be a whitespace indent token, but first make
512
                    // sure it isn't a blank line because we don't need to check indent unless there
513
                    // is actually some code to indent.
514
                    if ($tokens[$i]['code'] === T_WHITESPACE) {
3✔
515
                        $nextCode = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), ($closeBracket + 1), true);
3✔
516
                        if ($tokens[$nextCode]['line'] !== $lastLine) {
3✔
517
                            if ($inArg === false) {
3✔
518
                                $error = 'Empty lines are not allowed in multi-line function calls';
3✔
519
                                $fix   = $phpcsFile->addFixableError($error, $i, 'EmptyLine');
3✔
520
                                if ($fix === true) {
3✔
521
                                    $phpcsFile->fixer->replaceToken($i, '');
3✔
522
                                }
1✔
523
                            }
1✔
524

525
                            continue;
3✔
526
                        }
527
                    } else {
1✔
528
                        $nextCode = $i;
3✔
529
                    }
530

531
                    if ($tokens[$nextCode]['line'] === $tokens[$closeBracket]['line']) {
3✔
532
                        // Closing brace needs to be indented to the same level
533
                        // as the function call.
534
                        $inArg          = false;
3✔
535
                        $expectedIndent = ($foundFunctionIndent + $adjustment);
3✔
536
                    } else {
1✔
537
                        $expectedIndent = ($foundFunctionIndent + $this->indent + $adjustment);
3✔
538
                    }
539

540
                    if ($tokens[$i]['code'] !== T_WHITESPACE
3✔
541
                        && $tokens[$i]['code'] !== T_DOC_COMMENT_WHITESPACE
3✔
542
                    ) {
1✔
543
                        // Just check if it is a multi-line block comment. If so, we can
544
                        // calculate the indent from the whitespace before the content.
545
                        if ($tokens[$i]['code'] === T_COMMENT
3✔
546
                            && $tokens[($i - 1)]['code'] === T_COMMENT
3✔
547
                        ) {
1✔
548
                            $trimmedLength = strlen(ltrim($tokens[$i]['content']));
3✔
549
                            if ($trimmedLength === 0) {
3✔
550
                                // This is a blank comment line, so indenting it is
551
                                // pointless.
552
                                continue;
3✔
553
                            }
554

555
                            $foundIndent = (strlen($tokens[$i]['content']) - $trimmedLength);
3✔
556
                        } else {
1✔
557
                            $foundIndent = 0;
3✔
558
                        }
559
                    } else {
1✔
560
                        $foundIndent = $tokens[$i]['length'];
3✔
561
                    }
562

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

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

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

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

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

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

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

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

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

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

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

646
    }//end processMultiLineCall()
2✔
647

648

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