Skip to content

Commit

Permalink
PEAR/FunctionCallSignature: bug fix - fixer created parse errors
Browse files Browse the repository at this point in the history
The fixer for the `OpeningIndent` error for multi-line function calls could inadvertently remove a PHP close tag (or other inline HTML content) on the previous line if the first non-whitespace token on a line was `T_INLINE_HTML`.
In the case of a PHP close tag, this would cause a parse error in the file.

This parse error would then result in the file being incorrectly tokenized for the second fixer loop, with the `<?php` after the inline HTML being tokenized as below, which causes problems with other sniffs:
```
 13 | L1 | C 43 | CC 1 | ( 0) | T_WHITESPACE               | [  3]: ⸱⸱⸱
 14 | L1 | C 46 | CC 1 | ( 0) | T_LESS_THAN                | [  1]: <
 15 | L1 | C 47 | CC 1 | ( 0) | T_INLINE_THEN              | [  1]: ?
 16 | L1 | C 48 | CC 1 | ( 0) | T_STRING                   | [  3]: php
```

Fixed now.

Includes unit test.

Includes minor defensive coding fix (`$first !== false`) on line 345 and adding of an inline comment to clarify the code within the fixer.
  • Loading branch information
jrfnl committed Sep 17, 2022
1 parent 1985539 commit b5aaa31
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
// call itself is, so we can work out how far to
// indent the arguments.
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
if ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING
if ($first !== false
&& $tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING
&& $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING
) {
// We are in a multi-line string, so find the start and use
Expand Down Expand Up @@ -386,15 +387,19 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $

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

$padding = str_repeat(' ', $functionIndent);
if ($foundFunctionIndent === 0) {
$phpcsFile->fixer->addContentBefore($first, $padding);
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
$phpcsFile->fixer->replaceToken($first, $padding.$trimmed);
} else {
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
}
}
}
}//end if

$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,10 @@ array_fill_keys(
), value: true,
);
// phpcs:set PEAR.Functions.FunctionCallSignature allowMultipleArguments true

// If the first token is inline HTML, the token itself should be adjusted, not the token before.
<?php if (check_me() == 'value'): ?>
<?php include get_file_path(
'my_file.php'
); ?>
<?php endif; ?>
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,10 @@ array_fill_keys(
value: true,
);
// phpcs:set PEAR.Functions.FunctionCallSignature allowMultipleArguments true

// If the first token is inline HTML, the token itself should be adjusted, not the token before.
<?php if (check_me() == 'value'): ?>
<?php include get_file_path(
'my_file.php'
); ?>
<?php endif; ?>
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc')
546 => 1,
547 => 1,
548 => 1,
554 => 1,
555 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit b5aaa31

Please sign in to comment.