Skip to content

Commit

Permalink
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.
  • Loading branch information
jrfnl committed Nov 11, 2023
1 parent b0d171b commit bcd5868
Show file tree
Hide file tree
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
Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -65,6 +65,7 @@ public function getErrorList()
235 => 1,
244 => 1,
248 => 1,
258 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit bcd5868

Please sign in to comment.