Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PSR2/ClassDeclaration: bug fix - blank line fixer also removes indent
Browse files Browse the repository at this point in the history
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.
jrfnl committed Nov 10, 2023
1 parent 4ac5785 commit 44173aa
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php
Original file line number Diff line number Diff line change
@@ -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();
9 changes: 9 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
@@ -248,3 +248,12 @@ class Test
readonly class Test
{
}

if (!class_exists('IndentedDeclaration')) {
class IndentedDeclaration
{
function foo() {}


}
}
Original file line number Diff line number Diff line change
@@ -240,3 +240,10 @@ readonly class Test
readonly class Test
{
}

if (!class_exists('IndentedDeclaration')) {
class IndentedDeclaration
{
function foo() {}
}
}
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@ public function getErrorList()
235 => 1,
244 => 1,
248 => 1,
258 => 1,
];

}//end getErrorList()

0 comments on commit 44173aa

Please sign in to comment.