Skip to content

Commit 598fb1a

Browse files
committed
bug #4132 BlankLineAfterNamespaceFixer - do not remove indent, handle comments (kubawerlos)
This PR was merged into the 2.12 branch. Discussion ---------- BlankLineAfterNamespaceFixer - do not remove indent, handle comments prove of #4130 Commits ------- 9df07a8 BlankLineAfterOpeningTagFixer - do not remove indent, handle comments
2 parents 74666dc + 9df07a8 commit 598fb1a

File tree

4 files changed

+164
-12
lines changed

4 files changed

+164
-12
lines changed

src/Fixer/NamespaceNotation/BlankLineAfterNamespaceFixer.php

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
1717
use PhpCsFixer\FixerDefinition\CodeSample;
1818
use PhpCsFixer\FixerDefinition\FixerDefinition;
19+
use PhpCsFixer\Preg;
1920
use PhpCsFixer\Tokenizer\Token;
2021
use PhpCsFixer\Tokenizer\Tokens;
2122

@@ -62,7 +63,6 @@ public function isCandidate(Tokens $tokens)
6263
*/
6364
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
6465
{
65-
$ending = $this->whitespacesConfig->getLineEnding();
6666
$lastIndex = $tokens->count() - 1;
6767

6868
for ($index = $lastIndex; $index >= 0; --$index) {
@@ -75,21 +75,70 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
7575
$semicolonIndex = $tokens->getNextTokenOfKind($index, [';', '{', [T_CLOSE_TAG]]);
7676
$semicolonToken = $tokens[$semicolonIndex];
7777

78-
if (!isset($tokens[$semicolonIndex + 1]) || !$semicolonToken->equals(';')) {
78+
if (!$semicolonToken->equals(';')) {
7979
continue;
8080
}
8181

82-
$nextIndex = $semicolonIndex + 1;
83-
$nextToken = $tokens[$nextIndex];
82+
$indexToEnsureBlankLineAfter = $this->getIndexToEnsureBlankLineAfter($tokens, $semicolonIndex);
83+
$indexToEnsureBlankLine = $tokens->getNonEmptySibling($indexToEnsureBlankLineAfter, 1);
8484

85-
if (!$nextToken->isWhitespace()) {
86-
$tokens->insertAt($semicolonIndex + 1, new Token([T_WHITESPACE, $ending.$ending]));
85+
if (null !== $indexToEnsureBlankLine && $tokens[$indexToEnsureBlankLine]->isWhitespace()) {
86+
$tokens[$indexToEnsureBlankLine] = $this->getTokenToInsert($tokens[$indexToEnsureBlankLine]->getContent(), $indexToEnsureBlankLine === $lastIndex);
8787
} else {
88-
$tokens[$nextIndex] = new Token([
89-
T_WHITESPACE,
90-
($nextIndex === $lastIndex ? $ending : $ending.$ending).ltrim($nextToken->getContent()),
91-
]);
88+
$tokens->insertAt($indexToEnsureBlankLineAfter + 1, $this->getTokenToInsert('', $indexToEnsureBlankLineAfter === $lastIndex));
9289
}
9390
}
9491
}
92+
93+
/**
94+
* @param Tokens $tokens
95+
* @param int $index
96+
*
97+
* @return int
98+
*/
99+
private function getIndexToEnsureBlankLineAfter(Tokens $tokens, $index)
100+
{
101+
$indexToEnsureBlankLine = $index;
102+
$nextIndex = $tokens->getNonEmptySibling($indexToEnsureBlankLine, 1);
103+
104+
while (null !== $nextIndex) {
105+
$token = $tokens[$nextIndex];
106+
107+
if ($token->isWhitespace()) {
108+
if (1 === Preg::match('/\R/', $token->getContent())) {
109+
break;
110+
}
111+
$nextNextIndex = $tokens->getNonEmptySibling($nextIndex, 1);
112+
113+
if (!$tokens[$nextNextIndex]->isComment()) {
114+
break;
115+
}
116+
}
117+
118+
if (!$token->isWhitespace() && !$token->isComment()) {
119+
break;
120+
}
121+
122+
$indexToEnsureBlankLine = $nextIndex;
123+
$nextIndex = $tokens->getNonEmptySibling($indexToEnsureBlankLine, 1);
124+
}
125+
126+
return $indexToEnsureBlankLine;
127+
}
128+
129+
/**
130+
* @param string $currentContent
131+
* @param bool $isLastIndex
132+
*
133+
* @return Token
134+
*/
135+
private function getTokenToInsert($currentContent, $isLastIndex)
136+
{
137+
$ending = $this->whitespacesConfig->getLineEnding();
138+
139+
$emptyLines = $isLastIndex ? $ending : $ending.$ending;
140+
$indent = 1 === Preg::match('/^.*\R( *)$/s', $currentContent, $matches) ? $matches[1] : '';
141+
142+
return new Token([T_WHITESPACE, $emptyLines.$indent]);
143+
}
95144
}

src/Fixer/PhpTag/BlankLineAfterOpeningTagFixer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
8585
$tokens[0] = new Token([$token->getId(), rtrim($token->getContent()).$lineEnding]);
8686
}
8787

88-
if (!$tokens[1]->isWhitespace() && false === strpos($tokens[1]->getContent(), "\n")) {
89-
$tokens->insertAt(1, new Token([T_WHITESPACE, $lineEnding]));
88+
if (false === strpos($tokens[1]->getContent(), "\n")) {
89+
if ($tokens[1]->isWhitespace()) {
90+
$tokens[1] = new Token([T_WHITESPACE, $lineEnding.$tokens[1]->getContent()]);
91+
} else {
92+
$tokens->insertAt(1, new Token([T_WHITESPACE, $lineEnding]));
93+
}
9094
}
9195
}
9296
}

tests/Fixer/NamespaceNotation/BlankLineAfterNamespaceFixerTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ class C {}\r",
129129
[
130130
'<?php
131131
namespace Foo;
132+
',
133+
'<?php
134+
namespace Foo;',
135+
],
136+
[
137+
'<?php
138+
namespace Foo;
132139
133140
?>',
134141
'<?php
@@ -138,6 +145,75 @@ class C {}\r",
138145
139146
?>',
140147
],
148+
[
149+
'<?php
150+
namespace Foo;
151+
152+
class Bar {}',
153+
],
154+
[
155+
'<?php
156+
namespace Foo;
157+
158+
class Bar {}',
159+
'<?php
160+
namespace Foo;
161+
class Bar {}',
162+
],
163+
[
164+
'<?php
165+
namespace My\NS;
166+
167+
class X extends Y {}',
168+
],
169+
[
170+
'<?php
171+
namespace My\NS; // comment
172+
173+
class X extends Y {}',
174+
],
175+
[
176+
'<?php
177+
namespace My\NS; /* comment */
178+
179+
class X extends Y {}',
180+
],
181+
[
182+
'<?php
183+
namespace My\NS; /*
184+
comment 1
185+
comment 2
186+
*/
187+
188+
class X extends Y {}',
189+
'<?php
190+
namespace My\NS; /*
191+
comment 1
192+
comment 2
193+
*/
194+
class X extends Y {}',
195+
],
196+
[
197+
'<?php
198+
namespace My\NS; /** comment */
199+
200+
class X extends Y {}',
201+
],
202+
[
203+
'<?php
204+
namespace My\NS; /**
205+
comment 1
206+
comment 2
207+
*/
208+
209+
class X extends Y {}',
210+
'<?php
211+
namespace My\NS; /**
212+
comment 1
213+
comment 2
214+
*/
215+
class X extends Y {}',
216+
],
141217
];
142218
}
143219

tests/Fixer/PhpTag/BlankLineAfterOpeningTagFixerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ public function provideFixCases()
4242
[
4343
'<?php
4444
45+
$a = 0;
46+
echo 1;',
47+
'<?php
48+
$a = 0;
49+
echo 1;',
50+
],
51+
[
52+
'<?php
53+
54+
$b = 2;
55+
echo 3;',
56+
'<?php $b = 2;
57+
echo 3;',
58+
],
59+
[
60+
'<?php
61+
'.'
62+
$c = 4;
63+
echo 5;',
64+
],
65+
[
66+
'<?php
67+
4568
$a = function(){
4669
echo 1;
4770
};',

0 commit comments

Comments
 (0)