Skip to content

Commit

Permalink
AbstractLinesBeforeNamespaceFixer - add min. and max. option, not onl…
Browse files Browse the repository at this point in the history
…y single target count
  • Loading branch information
SpacePossum authored and keradus committed Nov 6, 2017
1 parent eace538 commit d3a7101
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/AbstractLinesBeforeNamespaceFixer.php
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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());
Expand Down
Expand Up @@ -57,7 +57,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
continue;
}

$this->fixLinesBeforeNamespace($tokens, $index, 1);
$this->fixLinesBeforeNamespace($tokens, $index, 0, 1);
}
}
}
Expand Up @@ -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);
}
}
}
Expand Down
Expand Up @@ -46,6 +46,7 @@ public function testFix($expected, $input = null, WhitespacesFixerConfig $whites
public function provideFixCases()
{
return array(
array('<?php namespace Some\Name\Space;'),
array("<?php\nnamespace X;"),
array("<?php\nnamespace X;", "<?php\n\n\n\nnamespace X;"),
array("<?php\r\nnamespace X;"),
Expand Down

0 comments on commit d3a7101

Please sign in to comment.