diff --git a/src/AbstractLinesBeforeNamespaceFixer.php b/src/AbstractLinesBeforeNamespaceFixer.php index d9c53014175..18e79036f7d 100644 --- a/src/AbstractLinesBeforeNamespaceFixer.php +++ b/src/AbstractLinesBeforeNamespaceFixer.php @@ -27,13 +27,14 @@ abstract class AbstractLinesBeforeNamespaceFixer extends AbstractFixer implements WhitespacesAwareFixerInterface { /** - * Make sure the expected number of new lines prefix a namespace. + * Make sure # of line breaks prefixing namespace is within given range. * * @param Tokens $tokens * @param int $index - * @param int $expected + * @param int $expectedMin min. # of line breaks + * @param int $expectedMax max. # of line breaks */ - protected function fixLinesBeforeNamespace(Tokens $tokens, $index, $expected) + protected function fixLinesBeforeNamespace(Tokens $tokens, $index, $expectedMin, $expectedMax) { // Let's determine the total numbers of new lines before the namespace // and the opening token @@ -59,14 +60,14 @@ protected function fixLinesBeforeNamespace(Tokens $tokens, $index, $expected) } } - if ($expected === $precedingNewlines) { + if ($precedingNewlines >= $expectedMin && $precedingNewlines <= $expectedMax) { return; } $previousIndex = $index - 1; $previous = $tokens[$previousIndex]; - if (0 === $expected) { + if (0 === $expectedMax) { // Remove all the previous new lines if ($previous->isWhitespace()) { $tokens->clearAt($previousIndex); @@ -80,7 +81,7 @@ protected function fixLinesBeforeNamespace(Tokens $tokens, $index, $expected) } $lineEnding = $this->whitespacesConfig->getLineEnding(); - $newlinesForWhitespaceToken = $expected; + $newlinesForWhitespaceToken = $expectedMax; if (null !== $openingToken) { // Use the configured line ending for the PHP opening tag $content = rtrim($openingToken->getContent()); diff --git a/src/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixer.php b/src/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixer.php index db52b4173f5..4d515ba3d84 100644 --- a/src/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixer.php +++ b/src/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixer.php @@ -57,7 +57,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) continue; } - $this->fixLinesBeforeNamespace($tokens, $index, 1); + $this->fixLinesBeforeNamespace($tokens, $index, 0, 1); } } } diff --git a/src/Fixer/NamespaceNotation/SingleBlankLineBeforeNamespaceFixer.php b/src/Fixer/NamespaceNotation/SingleBlankLineBeforeNamespaceFixer.php index c794d27018e..46de8479206 100644 --- a/src/Fixer/NamespaceNotation/SingleBlankLineBeforeNamespaceFixer.php +++ b/src/Fixer/NamespaceNotation/SingleBlankLineBeforeNamespaceFixer.php @@ -53,7 +53,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) $token = $tokens[$index]; if ($token->isGivenKind(T_NAMESPACE)) { - $this->fixLinesBeforeNamespace($tokens, $index, 2); + $this->fixLinesBeforeNamespace($tokens, $index, 2, 2); } } } diff --git a/tests/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixerTest.php b/tests/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixerTest.php index a4b568532e2..9a3828bd6ac 100644 --- a/tests/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixerTest.php +++ b/tests/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixerTest.php @@ -46,6 +46,7 @@ public function testFix($expected, $input = null, WhitespacesFixerConfig $whites public function provideFixCases() { return array( + array('