From 23f45ddf144f43d9517de7d8695db7e9aae7a1e5 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 23 Jun 2023 03:26:46 +0200 Subject: [PATCH] PSR2/ClassDeclaration: bug fix - blank line fixer also removes indent The `PSR2.Classes.ClassDeclaration.CloseBraceAfterBody` related logic checks that there is no blank line between the last content within the class and the close brace. The fixer for this error code, however, does not take indented class declarations into account and inadvertently also removes (correct) indentation. Fixed now. Includes unit test. --- .../PSR2/Sniffs/Classes/ClassDeclarationSniff.php | 4 ++-- .../PSR2/Tests/Classes/ClassDeclarationUnitTest.inc | 9 +++++++++ .../Tests/Classes/ClassDeclarationUnitTest.inc.fixed | 7 +++++++ .../PSR2/Tests/Classes/ClassDeclarationUnitTest.php | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php index c620582d41..a7f570cc03 100644 --- a/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php @@ -492,12 +492,12 @@ public function processClose(File $phpcsFile, $stackPtr) if ($fix === true) { $phpcsFile->fixer->beginChangeset(); - for ($i = ($prevContent + 1); $i < $closeBrace; $i++) { + for ($i = ($prevContent + 1); $tokens[$i]['line'] !== $tokens[$closeBrace]['line']; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } if (strpos($tokens[$prevContent]['content'], $phpcsFile->eolChar) === false) { - $phpcsFile->fixer->replaceToken($closeBrace, $phpcsFile->eolChar.$tokens[$closeBrace]['content']); + $phpcsFile->fixer->addNewline($prevContent); } $phpcsFile->fixer->endChangeset(); diff --git a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc index dd78809366..f241a1a2fc 100644 --- a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc +++ b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc @@ -248,3 +248,12 @@ class Test readonly class Test { } + +if (!class_exists('IndentedDeclaration')) { + class IndentedDeclaration + { + function foo() {} + + + } +} diff --git a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed index 3be473639e..bb72318720 100644 --- a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed @@ -240,3 +240,10 @@ readonly class Test readonly class Test { } + +if (!class_exists('IndentedDeclaration')) { + class IndentedDeclaration + { + function foo() {} + } +} diff --git a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php index 95c225edb4..a25daa92ac 100644 --- a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php +++ b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php @@ -65,6 +65,7 @@ public function getErrorList() 235 => 1, 244 => 1, 248 => 1, + 258 => 1, ]; }//end getErrorList()