本文整理汇总了PHP中PhpCsFixer\Tokenizer\Tokens::overrideAt方法的典型用法代码示例。如果您正苦于以下问题:PHP Tokens::overrideAt方法的具体用法?PHP Tokens::overrideAt怎么用?PHP Tokens::overrideAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpCsFixer\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::overrideAt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if ($token->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)) {
$tokens->overrideAt($index, new Token('['));
} elseif ($token->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE)) {
$tokens->overrideAt($index, new Token(']'));
}
}
}
示例2: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 1; 0 <= $index; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
continue;
}
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index);
$tokens->overrideAt($index, '(');
$tokens->overrideAt($closeIndex, ')');
$tokens->insertAt($index, new Token(array(T_ARRAY, 'array')));
}
}
示例3: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_ARRAY)) {
continue;
}
$openIndex = $tokens->getNextTokenOfKind($index, array('('));
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);
$token->clear();
$tokens->overrideAt($openIndex, array(CT_ARRAY_SQUARE_BRACE_OPEN, '['));
$tokens->overrideAt($closeIndex, array(CT_ARRAY_SQUARE_BRACE_CLOSE, ']'));
}
}
示例4: fix
/**
* Replace all `else if` (T_ELSE T_IF) with `elseif` (T_ELSEIF).
*
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_ELSE)) {
continue;
}
$ifTokenIndex = $tokens->getNextMeaningfulToken($index);
$beforeIfTokenIndex = $tokens->getPrevNonWhitespace($ifTokenIndex);
// if next meaning token is not T_IF - continue searching, this is not the case for fixing
if (!$tokens[$ifTokenIndex]->isGivenKind(T_IF)) {
continue;
}
// now we have T_ELSE following by T_IF so we could fix this
// 1. clear whitespaces between T_ELSE and T_IF
$tokens[$index + 1]->clear();
// 2. change token from T_ELSE into T_ELSEIF
$tokens->overrideAt($index, array(T_ELSEIF, 'elseif'));
// 3. clear succeeding T_IF
$tokens[$ifTokenIndex]->clear();
// 4. clear extra whitespace after T_IF in T_COMMENT,T_WHITESPACE?,T_IF,T_WHITESPACE sequence
if ($tokens[$beforeIfTokenIndex]->isComment() && $tokens[$ifTokenIndex + 1]->isWhitespace()) {
$tokens[$ifTokenIndex + 1]->clear();
}
}
}
示例5: fix
/**
* Replace all `else if` (T_ELSE T_IF) with `elseif` (T_ELSEIF).
*
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_ELSE)) {
continue;
}
$ifTokenIndex = $tokens->getNextMeaningfulToken($index);
$beforeIfTokenIndex = $tokens->getPrevNonWhitespace($ifTokenIndex);
// if next meaning token is not T_IF - continue searching, this is not the case for fixing
if (!$tokens[$ifTokenIndex]->isGivenKind(T_IF)) {
continue;
}
// now we have T_ELSE following by T_IF so we could fix this
// 1. clear whitespaces between T_ELSE and T_IF
$tokens[$index + 1]->clear();
// 2. change token from T_ELSE into T_ELSEIF
$tokens->overrideAt($index, array(T_ELSEIF, 'elseif'));
// 3. clear succeeding T_IF
$tokens[$ifTokenIndex]->clear();
// 4. clear extra whitespace after T_IF in T_COMMENT,T_WHITESPACE?,T_IF,T_WHITESPACE sequence
if ($tokens[$beforeIfTokenIndex]->isComment() && $tokens[$ifTokenIndex + 1]->isWhitespace()) {
$tokens[$ifTokenIndex + 1]->clear();
}
}
// handle `T_ELSE T_WHITESPACE T_IF` treated as single `T_ELSEIF` by HHVM
// see https://github.com/facebook/hhvm/issues/4796
if (defined('HHVM_VERSION')) {
foreach ($tokens->findGivenKind(T_ELSEIF) as $token) {
$token->setContent('elseif');
}
}
}
示例6: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_ECHO)) {
continue;
}
$nextTokenIndex = $tokens->getNextMeaningfulToken($index);
$endTokenIndex = $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)));
$canBeConverted = true;
for ($i = $nextTokenIndex; $i < $endTokenIndex; ++$i) {
if ($tokens[$i]->equalsAny(array('(', array(CT_ARRAY_SQUARE_BRACE_OPEN)))) {
$blockType = $tokens->detectBlockType($tokens[$i]);
$i = $tokens->findBlockEnd($blockType['type'], $i);
}
if ($tokens[$i]->equals(',')) {
$canBeConverted = false;
break;
}
}
if (false === $canBeConverted) {
continue;
}
$tokens->overrideAt($index, array(T_PRINT, 'print'));
}
}
示例7: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_ECHO)) {
continue;
}
/*
* HHVM parses '<?=' as T_ECHO instead of T_OPEN_TAG_WITH_ECHO
*
* @see https://github.com/facebook/hhvm/issues/4809
* @see https://github.com/facebook/hhvm/issues/7161
*/
if (defined('HHVM_VERSION') && 0 === strpos($token->getContent(), '<?=')) {
continue;
}
$nextTokenIndex = $tokens->getNextMeaningfulToken($index);
$endTokenIndex = $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)));
$canBeConverted = true;
for ($i = $nextTokenIndex; $i < $endTokenIndex; ++$i) {
if ($tokens[$i]->equalsAny(array('(', array(CT_ARRAY_SQUARE_BRACE_OPEN)))) {
$blockType = Tokens::detectBlockType($tokens[$i]);
$i = $tokens->findBlockEnd($blockType['type'], $i);
}
if ($tokens[$i]->equals(',')) {
$canBeConverted = false;
break;
}
}
if (false === $canBeConverted) {
continue;
}
$tokens->overrideAt($index, array(T_PRINT, 'print'));
}
}
示例8: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
static $controlStructures = array(T_FOREACH, T_IF, T_SWITCH, T_WHILE, T_FOR);
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_DOC_COMMENT)) {
continue;
}
$nextIndex = $tokens->getNextMeaningfulToken($index);
$nextToken = null !== $nextIndex ? $tokens[$nextIndex] : null;
if (null === $nextToken || $nextToken->equals('}')) {
$tokens->overrideAt($index, array(T_COMMENT, '/*' . ltrim($token->getContent(), '/*')));
continue;
}
if ($this->isStructuralElement($nextToken)) {
continue;
}
if ($nextToken->isGivenKind($controlStructures) && $this->isValidControl($tokens, $token, $nextIndex)) {
continue;
}
if ($nextToken->isGivenKind(T_VARIABLE) && $this->isValidVariable($tokens, $token, $nextIndex)) {
continue;
}
if ($nextToken->isGivenKind(T_LIST) && $this->isValidList($tokens, $token, $nextIndex)) {
continue;
}
// First docblock after open tag can be file-level docblock, so its left as is.
$prevIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevIndex]->isGivenKind(array(T_OPEN_TAG, T_NAMESPACE))) {
continue;
}
$tokens->overrideAt($index, array(T_COMMENT, '/*' . ltrim($token->getContent(), '/*')));
}
}
示例9: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
static $map = array(T_IS_EQUAL => array('id' => T_IS_IDENTICAL, 'content' => '==='), T_IS_NOT_EQUAL => array('id' => T_IS_NOT_IDENTICAL, 'content' => '!=='));
foreach ($tokens as $index => $token) {
$tokenId = $token->getId();
if (isset($map[$tokenId])) {
$tokens->overrideAt($index, array($map[$tokenId]['id'], $map[$tokenId]['content']));
}
}
}
示例10: fixClass
/**
* @param Tokens $tokens
* @param int $classIndex
* @param int $classOpenIndex
* @param int $classCloseIndex
*/
private function fixClass(Tokens $tokens, $classIndex, $classOpenIndex, $classCloseIndex)
{
for ($index = $classOpenIndex + 1; $index < $classCloseIndex; ++$index) {
if ($tokens[$index]->equals('{')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
continue;
}
if (!$tokens[$index]->isGivenKind(T_PROTECTED)) {
continue;
}
$tokens->overrideAt($index, array(T_PRIVATE, 'private'));
}
}
示例11: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_PRINT)) {
continue;
}
$prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
if (!$prevToken->equalsAny(array(';', '{', '}', array(T_OPEN_TAG)))) {
continue;
}
$tokens->overrideAt($index, array(T_ECHO, 'echo'));
}
}
示例12: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
/** @var Token $token */
foreach ($tokens as $index => $token) {
if ($token->isGivenKind(T_DOC_COMMENT)) {
$token->setContent($this->fixTokenContent($token->getContent()));
continue;
}
if (!$token->isGivenKind(T_COMMENT)) {
continue;
}
$content = $token->getContent();
$fixedContent = $this->fixTokenContent($content);
if ($content !== $fixedContent) {
$tokens->overrideAt($index, array(T_DOC_COMMENT, $fixedContent));
}
}
}
示例13: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(array(T_CASE, T_DEFAULT))) {
continue;
}
$ternariesCount = 0;
for ($colonIndex = $index + 1;; ++$colonIndex) {
// We have to skip ternary case for colons.
if ($tokens[$colonIndex]->equals('?')) {
++$ternariesCount;
}
if ($tokens[$colonIndex]->equalsAny(array(':', ';'))) {
if (0 === $ternariesCount) {
break;
}
--$ternariesCount;
}
}
if ($tokens[$colonIndex]->equals(';')) {
$tokens->overrideAt($colonIndex, ':');
}
}
}
示例14: removeUseDeclaration
private function removeUseDeclaration(Tokens $tokens, array $useDeclaration)
{
for ($index = $useDeclaration['end'] - 1; $index >= $useDeclaration['start']; --$index) {
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
}
if ($tokens[$useDeclaration['end']]->equals(';')) {
$tokens[$useDeclaration['end']]->clear();
}
$prevToken = $tokens[$useDeclaration['start'] - 1];
if ($prevToken->isWhitespace()) {
$prevToken->setContent(rtrim($prevToken->getContent(), " \t"));
}
if (!isset($tokens[$useDeclaration['end'] + 1])) {
return;
}
$nextIndex = $useDeclaration['end'] + 1;
$nextToken = $tokens[$nextIndex];
if ($nextToken->isWhitespace()) {
$content = ltrim($nextToken->getContent(), " \t");
$content = preg_replace("#^\r\n|^\n#", '', $content, 1);
$nextToken->setContent($content);
}
if ($prevToken->isWhitespace() && $nextToken->isWhitespace()) {
$tokens->overrideAt($nextIndex, array(T_WHITESPACE, $prevToken->getContent() . $nextToken->getContent()));
$prevToken->clear();
}
}
示例15: fixMultipleUse
private function fixMultipleUse(Tokens $tokens, $index, $endIndex)
{
$ending = $this->whitespacesConfig->getLineEnding();
for ($i = $endIndex - 1; $i > $index; --$i) {
if (!$tokens[$i]->equals(',')) {
continue;
}
$tokens->overrideAt($i, new Token(';'));
$i = $tokens->getNextMeaningfulToken($i);
$tokens->insertAt($i, new Token(array(T_USE, 'use')));
$tokens->insertAt($i + 1, new Token(array(T_WHITESPACE, ' ')));
$indent = $this->detectIndent($tokens, $index);
if ($tokens[$i - 1]->isWhitespace()) {
$tokens[$i - 1]->setContent($ending . $indent);
continue;
}
if (false === strpos($tokens[$i - 1]->getContent(), "\n")) {
$tokens->insertAt($i, new Token(array(T_WHITESPACE, $ending . $indent)));
}
}
}