Skip to content

Commit fc17f86

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 b0d171b commit fc17f86

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

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)