From b5aaa310069bcfc909d2eb03b866826b2e1d4a71 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 17 Sep 2022 18:59:16 +0200 Subject: [PATCH] PEAR/FunctionCallSignature: bug fix - fixer created parse errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 `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 @@ -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']) { diff --git a/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc b/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc index ed3d2c43e1..8a2d06504b 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc +++ b/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc @@ -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. + + + diff --git a/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc.fixed b/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc.fixed index 8d02e7467d..88575831e9 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc.fixed +++ b/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc.fixed @@ -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. + + + diff --git a/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.php b/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.php index 1dd59188f3..fd0b1dc3b3 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.php +++ b/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.php @@ -131,6 +131,8 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc') 546 => 1, 547 => 1, 548 => 1, + 554 => 1, + 555 => 1, ]; }//end getErrorList()