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

PHPCSStandards / PHP_CodeSniffer / 17339299324

30 Aug 2025 04:21AM UTC coverage: 79.154% (+0.6%) from 78.578%
17339299324

Pull #1204

github

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

7 of 8 new or added lines in 1 file covered. (87.5%)

16 existing lines in 2 files now uncovered.

25293 of 31954 relevant lines covered (79.15%)

70.58 hits per line

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

92.92
/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 bool
61
     */
62
    public $allowHereDocAfterOpenBracket = false;
63

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

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

78
        return $tokens;
3✔
79

80
    }//end register()
81

82

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

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

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

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

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

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

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

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

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

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

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

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

172
    }//end process()
2✔
173

174

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

195
        return false;
3✔
196

197
    }//end isMultiLineCall()
198

199

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

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

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

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

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

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

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

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

UNCOV
290
                    $closingContent = ')';
×
291

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

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

UNCOV
310
                        $prev--;
×
311
                    }
312

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

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

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

327
    }//end processSingleLineCall()
2✔
328

329

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

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

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

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

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

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

411
        $nextTokens = Tokens::$emptyTokens;
3✔
412
        if ($this->allowHereDocAfterOpenBracket) {
3✔
NEW
413
            $nextTokens += [T_START_HEREDOC, T_START_NOWDOC];
×
414
        }
415
        $next = $phpcsFile->findNext($nextTokens, ($openBracket + 1), null, true);
3✔
416
        if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
3✔
417
            $error = 'Opening parenthesis of a multi-line function call must be the last content on the line';
3✔
418
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpenBracket');
3✔
419
            if ($fix === true) {
3✔
420
                $phpcsFile->fixer->addContent(
3✔
421
                    $openBracket,
3✔
422
                    $phpcsFile->eolChar.str_repeat(' ', ($foundFunctionIndent + $this->indent))
3✔
423
                );
2✔
424
            }
1✔
425
        }
1✔
426

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

440
        // Each line between the parenthesis should be indented n spaces.
441
        $lastLine = ($tokens[$openBracket]['line'] - 1);
3✔
442
        $argStart = null;
3✔
443
        $argEnd   = null;
3✔
444

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

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

458
                    if ($tokens[$x]['line'] === $tokens[$i]['line']) {
3✔
459
                        break;
3✔
460
                    }
461

462
                    $phpcsFile->fixer->replaceToken($x, '');
3✔
463
                }
1✔
464

465
                $phpcsFile->fixer->endChangeset();
3✔
466
            }
1✔
467
        }//end if
1✔
468

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

471
        if ($tokens[($i - 1)]['code'] === T_WHITESPACE
3✔
472
            && $tokens[($i - 1)]['line'] === $tokens[$i]['line']
3✔
473
        ) {
1✔
474
            // Make sure we check the indent.
475
            $i--;
3✔
476
        }
1✔
477

478
        for ($i; $i < $closeBracket; $i++) {
3✔
479
            if ($i > $argStart && $i < $argEnd) {
3✔
480
                $inArg = true;
3✔
481
            } else {
1✔
482
                $inArg = false;
3✔
483
            }
484

485
            if ($tokens[$i]['line'] !== $lastLine) {
3✔
486
                $lastLine = $tokens[$i]['line'];
3✔
487

488
                // Ignore heredoc indentation.
489
                if (isset(Tokens::$heredocTokens[$tokens[$i]['code']]) === true) {
3✔
490
                    continue;
3✔
491
                }
492

493
                // Ignore multi-line string indentation.
494
                if (isset(Tokens::$stringTokens[$tokens[$i]['code']]) === true
3✔
495
                    && $tokens[$i]['code'] === $tokens[($i - 1)]['code']
3✔
496
                ) {
1✔
497
                    continue;
3✔
498
                }
499

500
                // Ignore inline HTML.
501
                if ($tokens[$i]['code'] === T_INLINE_HTML) {
3✔
502
                    continue;
3✔
503
                }
504

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

520
                            continue;
3✔
521
                        }
522
                    } else {
1✔
523
                        $nextCode = $i;
3✔
524
                    }
525

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

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

550
                            $foundIndent = (strlen($tokens[$i]['content']) - $trimmedLength);
3✔
551
                        } else {
1✔
552
                            $foundIndent = 0;
3✔
553
                        }
554
                    } else {
1✔
555
                        $foundIndent = $tokens[$i]['length'];
3✔
556
                    }
557

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

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

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

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

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

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

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

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

624
                                $phpcsFile->fixer->replaceToken($x, '');
3✔
625
                            }
1✔
626

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

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

641
    }//end processMultiLineCall()
2✔
642

643

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