Skip to content

Commit b5aaa31

Browse files
committed
PEAR/FunctionCallSignature: bug fix - fixer created parse errors
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.
1 parent 1985539 commit b5aaa31

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

src/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
340340
// call itself is, so we can work out how far to
341341
// indent the arguments.
342342
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
343-
if ($tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING
343+
if ($first !== false
344+
&& $tokens[$first]['code'] === T_CONSTANT_ENCAPSED_STRING
344345
&& $tokens[($first - 1)]['code'] === T_CONSTANT_ENCAPSED_STRING
345346
) {
346347
// We are in a multi-line string, so find the start and use
@@ -386,15 +387,19 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
386387

387388
$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
388389
if ($fix === true) {
390+
// Set adjustment for use later to determine whether argument indentation is correct when fixing.
389391
$adjustment = ($functionIndent - $foundFunctionIndent);
390-
$padding = str_repeat(' ', $functionIndent);
392+
393+
$padding = str_repeat(' ', $functionIndent);
391394
if ($foundFunctionIndent === 0) {
392395
$phpcsFile->fixer->addContentBefore($first, $padding);
396+
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
397+
$phpcsFile->fixer->replaceToken($first, $padding.$trimmed);
393398
} else {
394399
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
395400
}
396401
}
397-
}
402+
}//end if
398403

399404
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
400405
if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {

src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,10 @@ array_fill_keys(
548548
), value: true,
549549
);
550550
// phpcs:set PEAR.Functions.FunctionCallSignature allowMultipleArguments true
551+
552+
// If the first token is inline HTML, the token itself should be adjusted, not the token before.
553+
<?php if (check_me() == 'value'): ?>
554+
<?php include get_file_path(
555+
'my_file.php'
556+
); ?>
557+
<?php endif; ?>

src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,10 @@ array_fill_keys(
563563
value: true,
564564
);
565565
// phpcs:set PEAR.Functions.FunctionCallSignature allowMultipleArguments true
566+
567+
// If the first token is inline HTML, the token itself should be adjusted, not the token before.
568+
<?php if (check_me() == 'value'): ?>
569+
<?php include get_file_path(
570+
'my_file.php'
571+
); ?>
572+
<?php endif; ?>

src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc')
131131
546 => 1,
132132
547 => 1,
133133
548 => 1,
134+
554 => 1,
135+
555 => 1,
134136
];
135137

136138
}//end getErrorList()

0 commit comments

Comments
 (0)