From 4845ef085fac4353e93286f82915b3defabff065 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 5 Jan 2023 03:40:05 +0100 Subject: [PATCH] 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 --- .../PEAR/Sniffs/Functions/FunctionDeclarationSniff.php | 6 ++++-- .../PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc | 9 +++++++++ .../Functions/FunctionDeclarationUnitTest.inc.fixed | 9 +++++++++ .../PEAR/Tests/Functions/FunctionDeclarationUnitTest.php | 1 + 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php index 14479e2c0a..d907fca2c6 100644 --- a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php +++ b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php @@ -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); @@ -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), ''); } } diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc index fb7cb52f4d..52beaff772 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc @@ -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 + ) + {} +} diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed index f82b5665a5..7d90f9a412 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed @@ -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 + ) { + } +} diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php index 01ab3e8482..519b38e4e6 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php @@ -99,6 +99,7 @@ public function getErrorList($testFile='FunctionDeclarationUnitTest.inc') 371 => 1, 402 => 1, 406 => 1, + 475 => 1, ]; } else { $errors = [