Skip to content

Commit 024d26f

Browse files
authored
Fix docblock indentation loss when fixing empty enclosing line (#52)
* Fix docblock indentation loss when fixing empty enclosing line When EmptyEnclosingLine sniff fixed empty lines after class opening brace, it removed all tokens including the newline that should remain. Combined with other fixers (like DisallowTabIndent that converts tabs to spaces), this caused the first content line (typically a docblock) to lose its indentation entirely. The fix explicitly preserves one newline after the opening brace by replacing the first token with a newline character, then removing the remaining tokens up to the content line. * Add tests for tabs indentation preservation Adds test cases to verify that the EmptyEnclosingLine sniff correctly preserves indentation when removing empty lines after class opening braces, particularly when using tabs.
1 parent 6c2f79f commit 024d26f

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

PhpCollective/Sniffs/WhiteSpace/EmptyEnclosingLineSniff.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public function process(File $phpcsFile, $stackPtr): void
9191
if ($contentLine < $braceLine + 1) {
9292
$phpcsFile->fixer->addNewline($curlyBraceStartIndex);
9393
} else {
94-
for ($i = $curlyBraceStartIndex + 1; $i < $beginningOfLine - 1; $i++) {
94+
// Replace first token with newline, remove the rest
95+
$phpcsFile->fixer->replaceToken($curlyBraceStartIndex + 1, $phpcsFile->eolChar);
96+
for ($i = $curlyBraceStartIndex + 2; $i < $beginningOfLine; $i++) {
9597
$phpcsFile->fixer->replaceToken($i, '');
9698
}
9799
}

tests/PhpCollective/Sniffs/WhiteSpace/EmptyEnclosingLineSniffTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,31 @@ public function testEmptyEnclosingLineFixer(): void
2727
{
2828
$this->assertSnifferCanFixErrors(new EmptyEnclosingLineSniff());
2929
}
30+
31+
/**
32+
* Tests that tabs are preserved when fixing empty enclosing line.
33+
*
34+
* @return void
35+
*/
36+
public function testTabsPreservedSniffer(): void
37+
{
38+
$this->prefix = 'tabs-';
39+
$this->assertSnifferFindsErrors(new EmptyEnclosingLineSniff(), 1);
40+
$this->prefix = null;
41+
}
42+
43+
/**
44+
* Tests fixer preserves indentation with tabs.
45+
*
46+
* This test verifies the fix for the bug where indentation was lost
47+
* when removing empty lines after opening braces.
48+
*
49+
* @return void
50+
*/
51+
public function testTabsPreservedFixer(): void
52+
{
53+
$this->prefix = 'tabs-';
54+
$this->assertSnifferCanFixErrors(new EmptyEnclosingLineSniff());
55+
$this->prefix = null;
56+
}
3057
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PhpCollective;
4+
5+
class TabsTest {
6+
/**
7+
* @param string $foo The foo.
8+
*
9+
* @return void
10+
*/
11+
public function one(string $foo): void
12+
{
13+
echo $foo;
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PhpCollective;
4+
5+
class TabsTest {
6+
7+
/**
8+
* @param string $foo The foo.
9+
*
10+
* @return void
11+
*/
12+
public function one(string $foo): void
13+
{
14+
echo $foo;
15+
}
16+
}

0 commit comments

Comments
 (0)