Skip to content

Commit

Permalink
PEAR/FunctionDeclaration: bug fix - prevent fixer from creating a par…
Browse files Browse the repository at this point in the history
…se error

Issue as described in 3736.

The fixer would try to remove superfluous whitespace remaining after the move of the opening brace to the previous line, but did not take into account that there may not be any whitespace to removed, i.e. that the `$opener +1` token could be the same as the `$next` token.

Fixed now.

Includes unit test.

Fixes 3736
  • Loading branch information
jrfnl committed May 4, 2023
1 parent c85ac6a commit 4845ef0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
$phpcsFile->fixer->addContent($prev, ' {');

// If the opener is on a line by itself, removing it will create
// an empty line, so just remove the entire line instead.
// an empty line, so remove the entire line instead.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($opener - 1), $closeBracket, true);
$next = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), null, true);

Expand All @@ -324,7 +324,9 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
} else {
// Just remove the opener.
$phpcsFile->fixer->replaceToken($opener, '');
if ($tokens[$next]['line'] === $tokens[$opener]['line']) {
if ($tokens[$next]['line'] === $tokens[$opener]['line']
&& ($opener + 1) !== $next
) {
$phpcsFile->fixer->replaceToken(($opener + 1), '');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,12 @@ new ExceptionMessage(),
) {
}
}

// Issue #3736 - prevent the fixer creating a parse error by removing the function close brace.
class Test
{
public function __construct(
protected int $id
)
{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,12 @@ new ExceptionMessage(),
) {
}
}

// Issue #3736 - prevent the fixer creating a parse error by removing the function close brace.
class Test
{
public function __construct(
protected int $id
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function getErrorList($testFile='FunctionDeclarationUnitTest.inc')
371 => 1,
402 => 1,
406 => 1,
475 => 1,
];
} else {
$errors = [
Expand Down

0 comments on commit 4845ef0

Please sign in to comment.