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

PHPCSStandards / PHP_CodeSniffer / 9101918600

15 May 2024 07:59PM UTC coverage: 73.231% (-0.1%) from 73.363%
9101918600

push

github

jrfnl
Generic/SpaceAfterNot: improve code coverage

17793 of 24297 relevant lines covered (73.23%)

69.17 hits per line

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

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

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

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

16
class FunctionCallSignatureSniff implements Sniff
17
{
18

19
    /**
20
     * 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::$functionNameTokens;
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, $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::$emptyTokens, ($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::$emptyTokens;
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 (($stackPtr + 1) !== $openBracket) {
3✔
114
            // Checking this: $value = my_function[*](...).
115
            $error = 'Space before opening parenthesis of function call prohibited';
3✔
116
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeOpenBracket');
3✔
117
            if ($fix === true) {
3✔
118
                $phpcsFile->fixer->beginChangeset();
3✔
119
                for ($i = ($stackPtr + 1); $i < $openBracket; $i++) {
3✔
120
                    $phpcsFile->fixer->replaceToken($i, '');
3✔
121
                }
122

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

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

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

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

156
    }//end process()
1✔
157

158

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

179
        return false;
3✔
180

181
    }//end isMultiLineCall()
182

183

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

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

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

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

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

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

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

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

274
                    $closingContent = ')';
×
275

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

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

294
                        $prev--;
×
295
                    }
296

297
                    $phpcsFile->fixer->addContent($prev, $padding.$closingContent);
×
298

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

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

311
    }//end processSingleLineCall()
1✔
312

313

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

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

363
        // Make sure the function indent is divisible by the indent size.
364
        // We round down here because this accounts for times when the
365
        // surrounding code is indented a little too far in, and not correctly
366
        // at a tab stop. Without this, the function will be indented a further
367
        // $indent spaces to the right.
368
        $functionIndent = (int) (floor($foundFunctionIndent / $this->indent) * $this->indent);
3✔
369
        $adjustment     = 0;
3✔
370

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

500
                            continue;
3✔
501
                        }
502
                    } else {
503
                        $nextCode = $i;
3✔
504
                    }
505

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

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

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

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

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

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

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

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

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

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

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

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

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

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

621
    }//end processMultiLineCall()
1✔
622

623

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

© 2025 Coveralls, Inc