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 Dec 5, 2023
1 parent 890b579 commit 9e19bd1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ The file documents changes to the PHP_CodeSniffer project.
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3833 : Generic.PHP.LowerCaseType: fixed potential undefined array index notice
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3846 : PSR2.Classes.ClassDeclaration.CloseBraceAfterBody : fixer will no longer remove indentation on the close brace line
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3854 : Fatal error when using Gitblame report in combination with `--basepath` and running from project subdirectory
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3867 : Tokenizer/PHP: union type and intersection type operators were not correctly tokenized for static properties without explicit visibility
Expand Down
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 9e19bd1

Please sign in to comment.