Skip to content

Commit ed6c437

Browse files
committed
PEAR/FunctionDeclaration: bug fix - prevent fixer from creating a parse 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
1 parent 66b4b36 commit ed6c437

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ The file documents changes to the PHP_CodeSniffer project.
109109
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
110110
- Fixed bug #3728 : PHP 8.2 | PSR1/SideEffects: allow for readonly classes
111111
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
112+
- Fixed bug #3736 : PEAR/FunctionDeclaration: prevent fixer removing the close brace (and creating a parse error) when there is no space between the open brace and close brace of a function
113+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
112114
- Fixed bug #3770 : Squiz/NonExecutableCode: prevent false positives for switching between PHP and HTML
113115
- Thanks to Dan Wallis (@fredden) for the patch
114116
- Fixed bug #3773 : Tokenizer/PHP: tokenization of the readonly keyword when used in combination with PHP 8.2 disjunctive normal types

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
308308
$phpcsFile->fixer->addContent($prev, ' {');
309309

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

@@ -324,7 +324,9 @@ public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
324324
} else {
325325
// Just remove the opener.
326326
$phpcsFile->fixer->replaceToken($opener, '');
327-
if ($tokens[$next]['line'] === $tokens[$opener]['line']) {
327+
if ($tokens[$next]['line'] === $tokens[$opener]['line']
328+
&& ($opener + 1) !== $next
329+
) {
328330
$phpcsFile->fixer->replaceToken(($opener + 1), '');
329331
}
330332
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,12 @@ new ExceptionMessage(),
465465
) {
466466
}
467467
}
468+
469+
// Issue #3736 - prevent the fixer creating a parse error by removing the function close brace.
470+
class Test
471+
{
472+
public function __construct(
473+
protected int $id
474+
)
475+
{}
476+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,12 @@ new ExceptionMessage(),
463463
) {
464464
}
465465
}
466+
467+
// Issue #3736 - prevent the fixer creating a parse error by removing the function close brace.
468+
class Test
469+
{
470+
public function __construct(
471+
protected int $id
472+
) {
473+
}
474+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public function getErrorList($testFile='FunctionDeclarationUnitTest.inc')
9999
371 => 1,
100100
402 => 1,
101101
406 => 1,
102+
475 => 1,
102103
];
103104
} else {
104105
$errors = [

0 commit comments

Comments
 (0)