Skip to content

Commit

Permalink
minor PHP-CS-Fixer#1180 create Tokens::overrideAt method (keradus)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.8 branch.

Discussion
----------

create Tokens::overrideAt method

To increase similarity between 1.x and 2.x

Commits
-------

acbc1d3 create Tokens::overrideAt method
  • Loading branch information
keradus committed May 3, 2015
2 parents f28e707 + acbc1d3 commit 25cfa73
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Symfony/CS/Fixer/Contrib/LongArraySyntaxFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public function fix(\SplFileInfo $file, $content)

$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_SQUARE_BRACE, $index);

$tokens[$index]->setContent('(');
$tokens[$closeIndex]->setContent(')');
$tokens->overrideAt($index, '(');
$tokens->overrideAt($closeIndex, ')');

$tokens->insertAt($index, new Token(array(T_ARRAY, 'array')));
}

Expand Down
5 changes: 3 additions & 2 deletions Symfony/CS/Fixer/Contrib/ShortArraySyntaxFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public function fix(\SplFileInfo $file, $content)
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);

$token->clear();
$tokens[$openIndex]->setContent('[');
$tokens[$closeIndex]->setContent(']');

$tokens->overrideAt($openIndex, '[');
$tokens->overrideAt($closeIndex, ']');
}

return $tokens->generateCode();
Expand Down
4 changes: 2 additions & 2 deletions Symfony/CS/Fixer/Contrib/StrictFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public function fix(\SplFileInfo $file, $content)

$tokens = Tokens::fromCode($content);

foreach ($tokens as $token) {
foreach ($tokens as $index => $token) {
$tokenId = $token->getId();

if (isset($map[$tokenId])) {
$token->override(array($map[$tokenId]['id'], $map[$tokenId]['content'], $token->getLine()));
$tokens->overrideAt($index, array($map[$tokenId]['id'], $map[$tokenId]['content'], $token->getLine()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/PSR2/ElseifFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function fix(\SplFileInfo $file, $content)
$tokens[$index + 1]->clear();

// 2. change token from T_ELSE into T_ELSEIF
$token->override(array(T_ELSEIF, 'elseif', $token->getLine()));
$tokens->overrideAt($index, array(T_ELSEIF, 'elseif', $token->getLine()));

// 3. clear succeeding T_IF
$nextToken->clear();
Expand Down
4 changes: 2 additions & 2 deletions Symfony/CS/Fixer/Symfony/PhpdocToCommentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function fix(\SplFileInfo $file, $content)
$nextToken = null !== $nextIndex ? $tokens[$nextIndex] : null;

if (null === $nextToken || $nextToken->equals('}')) {
$token->override(array(T_COMMENT, '/*'.ltrim($token->getContent(), '/*'), $token->getLine()));
$tokens->overrideAt($index, array(T_COMMENT, '/*'.ltrim($token->getContent(), '/*'), $token->getLine()));
continue;
}

Expand All @@ -67,7 +67,7 @@ public function fix(\SplFileInfo $file, $content)
continue;
}

$token->override(array(T_COMMENT, '/*'.ltrim($token->getContent(), '/*'), $token->getLine()));
$tokens->overrideAt($index, array(T_COMMENT, '/*'.ltrim($token->getContent(), '/*'), $token->getLine()));
}

return $tokens->generateCode();
Expand Down
2 changes: 2 additions & 0 deletions Symfony/CS/Tokenizer/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ public function isWhitespace(array $opts = array())
/**
* Override token.
*
* If called on Token inside Tokens collection please use `Tokens::overrideAt` instead.
*
* @param string|array $prototype token prototype
*/
public function override($prototype)
Expand Down
18 changes: 13 additions & 5 deletions Symfony/CS/Tokenizer/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,14 @@ public function ensureWhitespaceAtIndex($index, $indexOffset, $whitespace)
}
};

$token = $this[$index];

if ($token->isWhitespace()) {
if ($this[$index]->isWhitespace()) {
$removeLastCommentLine($this[$index - 1], $indexOffset);
$token->override(array(T_WHITESPACE, $whitespace, $token->getLine()));
$this->overrideAt($index, array(T_WHITESPACE, $whitespace, $this[$index]->getLine()));

return false;
}

$removeLastCommentLine($token, $indexOffset);
$removeLastCommentLine($this[$index], $indexOffset);

$this->insertAt(
$index + $indexOffset,
Expand Down Expand Up @@ -1182,6 +1180,16 @@ public function isBinaryOperator($index)
return false;
}

/*
* Override token at given index and register it.
*
* @param Token|array|string $token token prototype
*/
public function overrideAt($index, $token)
{
$this[$index]->override($token);
}

/**
* Removes all the leading whitespace.
*
Expand Down

0 comments on commit 25cfa73

Please sign in to comment.