Skip to content

Commit 9e19bd1

Browse files
committed
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.
1 parent 890b579 commit 9e19bd1

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ The file documents changes to the PHP_CodeSniffer project.
147147
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
148148
- Fixed bug #3833 : Generic.PHP.LowerCaseType: fixed potential undefined array index notice
149149
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
150+
- Fixed bug #3846 : PSR2.Classes.ClassDeclaration.CloseBraceAfterBody : fixer will no longer remove indentation on the close brace line
151+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
150152
- Fixed bug #3854 : Fatal error when using Gitblame report in combination with `--basepath` and running from project subdirectory
151153
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
152154
- Fixed bug #3867 : Tokenizer/PHP: union type and intersection type operators were not correctly tokenized for static properties without explicit visibility

src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,12 @@ public function processClose(File $phpcsFile, $stackPtr)
492492

493493
if ($fix === true) {
494494
$phpcsFile->fixer->beginChangeset();
495-
for ($i = ($prevContent + 1); $i < $closeBrace; $i++) {
495+
for ($i = ($prevContent + 1); $tokens[$i]['line'] !== $tokens[$closeBrace]['line']; $i++) {
496496
$phpcsFile->fixer->replaceToken($i, '');
497497
}
498498

499499
if (strpos($tokens[$prevContent]['content'], $phpcsFile->eolChar) === false) {
500-
$phpcsFile->fixer->replaceToken($closeBrace, $phpcsFile->eolChar.$tokens[$closeBrace]['content']);
500+
$phpcsFile->fixer->addNewline($prevContent);
501501
}
502502

503503
$phpcsFile->fixer->endChangeset();

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,12 @@ class Test
248248
readonly class Test
249249
{
250250
}
251+
252+
if (!class_exists('IndentedDeclaration')) {
253+
class IndentedDeclaration
254+
{
255+
function foo() {}
256+
257+
258+
}
259+
}

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,10 @@ readonly class Test
240240
readonly class Test
241241
{
242242
}
243+
244+
if (!class_exists('IndentedDeclaration')) {
245+
class IndentedDeclaration
246+
{
247+
function foo() {}
248+
}
249+
}

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function getErrorList()
6565
235 => 1,
6666
244 => 1,
6767
248 => 1,
68+
258 => 1,
6869
];
6970

7071
}//end getErrorList()

0 commit comments

Comments
 (0)