当前位置: 首页>>代码示例>>PHP>>正文


PHP Tokens::getNextTokenOfKind方法代码示例

本文整理汇总了PHP中PhpCsFixer\Tokenizer\Tokens::getNextTokenOfKind方法的典型用法代码示例。如果您正苦于以下问题:PHP Tokens::getNextTokenOfKind方法的具体用法?PHP Tokens::getNextTokenOfKind怎么用?PHP Tokens::getNextTokenOfKind使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PhpCsFixer\Tokenizer\Tokens的用法示例。


在下文中一共展示了Tokens::getNextTokenOfKind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(T_FUNCTION)) {
             continue;
         }
         $startParenthesisIndex = $tokens->getNextTokenOfKind($index, array('('));
         $endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startParenthesisIndex);
         for ($iter = $endParenthesisIndex - 1; $iter > $startParenthesisIndex; --$iter) {
             if (!$tokens[$iter]->isGivenKind(T_VARIABLE)) {
                 continue;
             }
             // skip ... before $variable for variadic parameter
             if (defined('T_ELLIPSIS')) {
                 $prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter);
                 if ($tokens[$prevNonWhitespaceIndex]->isGivenKind(T_ELLIPSIS)) {
                     $iter = $prevNonWhitespaceIndex;
                 }
             }
             // skip & before $variable for parameter passed by reference
             $prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter);
             if ($tokens[$prevNonWhitespaceIndex]->equals('&')) {
                 $iter = $prevNonWhitespaceIndex;
             }
             if (!$tokens[$iter - 1]->equalsAny(array(array(T_WHITESPACE), array(T_COMMENT), array(T_DOC_COMMENT), '(', ','))) {
                 $tokens->insertAt($iter, new Token(array(T_WHITESPACE, ' ')));
             }
         }
     }
 }
开发者ID:thekabal,项目名称:tki,代码行数:34,代码来源:FunctionTypehintSpaceFixer.php

示例2: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = $tokens->count() - 3; $index > 0; --$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(T_NEW)) {
             continue;
         }
         $nextIndex = $tokens->getNextTokenOfKind($index, array(':', ';', ',', '(', ')', '[', ']', array(T_CLOSE_TAG), array(CT_ARRAY_SQUARE_BRACE_OPEN), array(CT_ARRAY_SQUARE_BRACE_CLOSE), array(CT_BRACE_CLASS_INSTANTIATION_OPEN), array(CT_BRACE_CLASS_INSTANTIATION_CLOSE)));
         $nextToken = $tokens[$nextIndex];
         // entrance into array index syntax - need to look for exit
         while ($nextToken->equals('[')) {
             $nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $nextIndex) + 1;
             $nextToken = $tokens[$nextIndex];
         }
         // new statement has a gap in it - advance to the next token
         if ($nextToken->isWhitespace()) {
             $nextIndex = $tokens->getNextNonWhitespace($nextIndex);
             $nextToken = $tokens[$nextIndex];
         }
         // new statement with () - nothing to do
         if ($nextToken->equals('(')) {
             continue;
         }
         $meaningBeforeNextIndex = $tokens->getPrevMeaningfulToken($nextIndex);
         $tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')')));
     }
 }
开发者ID:infinitely-young,项目名称:PHP-CS-Fixer,代码行数:30,代码来源:NewWithBracesFixer.php

示例3: replaceNameOccurrences

 /**
  * Replace occurrences of the name of the classy element by "self" (if possible).
  *
  * @param Tokens $tokens
  * @param string $name
  * @param int    $startIndex
  * @param int    $endIndex
  */
 private function replaceNameOccurrences(Tokens $tokens, $name, $startIndex, $endIndex)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     for ($i = $startIndex; $i < $endIndex; ++$i) {
         $token = $tokens[$i];
         // skip lambda functions (PHP < 5.4 compatibility)
         if ($token->isGivenKind(T_FUNCTION) && $tokensAnalyzer->isLambda($i)) {
             $i = $tokens->getNextTokenOfKind($i, array('{'));
             $i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $i);
             continue;
         }
         if (!$token->equals(array(T_STRING, $name), false)) {
             continue;
         }
         $prevToken = $tokens[$tokens->getPrevMeaningfulToken($i)];
         $nextToken = $tokens[$tokens->getNextMeaningfulToken($i)];
         // skip tokens that are part of a fully qualified name
         if ($prevToken->isGivenKind(T_NS_SEPARATOR) || $nextToken->isGivenKind(T_NS_SEPARATOR)) {
             continue;
         }
         if ($prevToken->isGivenKind(array(T_INSTANCEOF, T_NEW)) || $nextToken->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM)) {
             $token->setContent('self');
         }
     }
 }
开发者ID:amiss18,项目名称:PHP-CS-Fixer,代码行数:33,代码来源:SelfAccessorFixer.php

示例4: transformIntoDollarCloseBrace

 private function transformIntoDollarCloseBrace(Tokens $tokens, Token $token, $index)
 {
     if ($token->isGivenKind(T_DOLLAR_OPEN_CURLY_BRACES)) {
         $nextIndex = $tokens->getNextTokenOfKind($index, array('}'));
         $tokens[$nextIndex]->override(array(CT_DOLLAR_CLOSE_CURLY_BRACES, '}'));
     }
 }
开发者ID:infinitely-young,项目名称:PHP-CS-Fixer,代码行数:7,代码来源:CurlyBraceTransformer.php

示例5: 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'));
     }
 }
开发者ID:cryode,项目名称:PHP-CS-Fixer,代码行数:28,代码来源:EchoToPrintFixer.php

示例6: 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'));
     }
 }
开发者ID:GrahamForks,项目名称:PHP-CS-Fixer,代码行数:37,代码来源:EchoToPrintFixer.php

示例7: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     foreach ($tokensAnalyzer->getImportUseIndexes() as $index) {
         $indent = '';
         // if previous line ends with comment and current line starts with whitespace, use current indent
         if ($tokens[$index - 1]->isWhitespace(" \t") && $tokens[$index - 2]->isGivenKind(T_COMMENT)) {
             $indent = $tokens[$index - 1]->getContent();
         } elseif ($tokens[$index - 1]->isWhitespace()) {
             $indent = Utils::calculateTrailingWhitespaceIndent($tokens[$index - 1]);
         }
         $newline = "\n";
         // Handle insert index for inline T_COMMENT with whitespace after semicolon
         $semicolonIndex = $tokens->getNextTokenOfKind($index, array(';', '{'));
         $insertIndex = $semicolonIndex + 1;
         if ($tokens[$insertIndex]->isWhitespace(" \t") && $tokens[$insertIndex + 1]->isComment()) {
             ++$insertIndex;
         }
         // Increment insert index for inline T_COMMENT or T_DOC_COMMENT
         if ($tokens[$insertIndex]->isComment()) {
             ++$insertIndex;
         }
         $afterSemicolon = $tokens->getNextMeaningfulToken($semicolonIndex);
         if (!$tokens[$afterSemicolon]->isGivenKind(T_USE)) {
             $newline .= "\n";
         }
         if ($tokens[$insertIndex]->isWhitespace()) {
             $nextToken = $tokens[$insertIndex];
             $nextToken->setContent($newline . $indent . ltrim($nextToken->getContent()));
         } elseif ($newline && $indent) {
             $tokens->insertAt($insertIndex, new Token(array(T_WHITESPACE, $newline . $indent)));
         }
     }
 }
开发者ID:infinitely-young,项目名称:PHP-CS-Fixer,代码行数:37,代码来源:SingleLineAfterImportsFixer.php

示例8: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     $uses = array_reverse($tokensAnalyzer->getImportUseIndexes());
     foreach ($uses as $index) {
         $endIndex = $tokens->getNextTokenOfKind($index, array(';'));
         $declarationContent = $tokens->generatePartialCode($index + 1, $endIndex - 1);
         $declarationParts = explode(',', $declarationContent);
         if (1 === count($declarationParts)) {
             continue;
         }
         $declarationContent = array();
         foreach ($declarationParts as $declarationPart) {
             $declarationContent[] = 'use ' . trim($declarationPart) . ';';
         }
         $declarationContent = implode("\n" . $this->detectIndent($tokens, $index), $declarationContent);
         for ($i = $index; $i <= $endIndex; ++$i) {
             $tokens[$i]->clear();
         }
         $declarationTokens = Tokens::fromCode('<?php ' . $declarationContent);
         $declarationTokens[0]->clear();
         $declarationTokens->clearEmptyTokens();
         $tokens->insertAt($index, $declarationTokens);
     }
 }
开发者ID:thekabal,项目名称:tki,代码行数:28,代码来源:SingleImportPerStatementFixer.php

示例9: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(T_FUNCTION)) {
             continue;
         }
         $startParenthesisIndex = $tokens->getNextTokenOfKind($index, array('('));
         $endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startParenthesisIndex);
         $startBraceIndex = $tokens->getNextTokenOfKind($endParenthesisIndex, array(';', '{'));
         $startBraceToken = $tokens[$startBraceIndex];
         if ($startBraceToken->equals('{')) {
             // fix single-line whitespace before {
             // eg: `function foo(){}` => `function foo() {}`
             // eg: `function foo()   {}` => `function foo() {}`
             if (!$tokens[$startBraceIndex - 1]->isWhitespace() || $tokens[$startBraceIndex - 1]->isWhitespace($this->singleLineWhitespaceOptions)) {
                 $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, ' ');
             }
         }
         $afterParenthesisIndex = $tokens->getNextNonWhitespace($endParenthesisIndex);
         $afterParenthesisToken = $tokens[$afterParenthesisIndex];
         if ($afterParenthesisToken->isGivenKind(CT_USE_LAMBDA)) {
             $useStartParenthesisIndex = $tokens->getNextTokenOfKind($afterParenthesisIndex, array('('));
             $useEndParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $useStartParenthesisIndex);
             // fix whitespace after CT_USE_LAMBDA
             $tokens->ensureWhitespaceAtIndex($afterParenthesisIndex + 1, 0, ' ');
             // remove single-line edge whitespaces inside use parentheses
             $this->fixParenthesisInnerEdge($tokens, $useStartParenthesisIndex, $useEndParenthesisIndex);
             // fix whitespace before CT_USE_LAMBDA
             $tokens->ensureWhitespaceAtIndex($afterParenthesisIndex - 1, 1, ' ');
         }
         // remove single-line edge whitespaces inside parameters list parentheses
         $this->fixParenthesisInnerEdge($tokens, $startParenthesisIndex, $endParenthesisIndex);
         if (!$tokensAnalyzer->isLambda($index)) {
             // remove whitespace before (
             // eg: `function foo () {}` => `function foo() {}`
             if ($tokens[$startParenthesisIndex - 1]->isWhitespace()) {
                 $tokens[$startParenthesisIndex - 1]->clear();
             }
         }
         // fix whitespace after T_FUNCTION
         // eg: `function     foo() {}` => `function foo() {}`
         $tokens->ensureWhitespaceAtIndex($index + 1, 0, ' ');
     }
 }
开发者ID:amiss18,项目名称:PHP-CS-Fixer,代码行数:49,代码来源:FunctionDeclarationFixer.php

示例10: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($i = 0, $l = $tokens->count(); $i < $l; ++$i) {
         if (!$tokens[$i]->isGivenKind(T_FUNCTION)) {
             continue;
         }
         $startIndex = $tokens->getNextTokenOfKind($i, array('('));
         $i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
         $this->fixFunctionDefinition($tokens, $startIndex, $i);
     }
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:14,代码来源:NoUnreachableDefaultArgumentValueFixer.php

示例11: removeBetweenUse

 private function removeBetweenUse($index)
 {
     $next = $this->tokens->getNextTokenOfKind($index, array(';', T_CLOSE_TAG));
     if (null === $next || $this->tokens[$next]->isGivenKind(T_CLOSE_TAG)) {
         return;
     }
     $nextUseCandidate = $this->tokens->getNextMeaningfulToken($next);
     if (null === $nextUseCandidate || 1 === $nextUseCandidate - $next || !$this->tokens[$nextUseCandidate]->isGivenKind($this->tokens[$index]->getId())) {
         return;
     }
     return $this->removeEmptyLinesAfterLineWithTokenAt($next);
 }
开发者ID:thekabal,项目名称:tki,代码行数:12,代码来源:NoExtraConsecutiveBlankLinesFixer.php

示例12: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $ending = $this->whitespacesConfig->getLineEnding();
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     foreach ($tokensAnalyzer->getImportUseIndexes() as $index) {
         $indent = '';
         // if previous line ends with comment and current line starts with whitespace, use current indent
         if ($tokens[$index - 1]->isWhitespace(" \t") && $tokens[$index - 2]->isGivenKind(T_COMMENT)) {
             $indent = $tokens[$index - 1]->getContent();
         } elseif ($tokens[$index - 1]->isWhitespace()) {
             $indent = Utils::calculateTrailingWhitespaceIndent($tokens[$index - 1]);
         }
         $semicolonIndex = $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)));
         // Handle insert index for inline T_COMMENT with whitespace after semicolon
         $insertIndex = $semicolonIndex;
         if ($tokens[$semicolonIndex]->isGivenKind(T_CLOSE_TAG)) {
             if ($tokens[$insertIndex - 1]->isWhitespace()) {
                 --$insertIndex;
             }
             $tokens->insertAt($insertIndex, new Token(';'));
         }
         if ($semicolonIndex === count($tokens) - 1) {
             $tokens->insertAt($insertIndex + 1, new Token(array(T_WHITESPACE, $ending . $ending . $indent)));
         } else {
             $newline = $ending;
             $tokens[$semicolonIndex]->isGivenKind(T_CLOSE_TAG) ? --$insertIndex : ++$insertIndex;
             if ($tokens[$insertIndex]->isWhitespace(" \t") && $tokens[$insertIndex + 1]->isComment()) {
                 ++$insertIndex;
             }
             // Increment insert index for inline T_COMMENT or T_DOC_COMMENT
             if ($tokens[$insertIndex]->isComment()) {
                 ++$insertIndex;
             }
             $afterSemicolon = $tokens->getNextMeaningfulToken($semicolonIndex);
             if (null === $afterSemicolon || !$tokens[$afterSemicolon]->isGivenKind(T_USE)) {
                 $newline .= $ending;
             }
             if ($tokens[$insertIndex]->isWhitespace()) {
                 $nextToken = $tokens[$insertIndex];
                 $nextMeaningfulAfterUseIndex = $tokens->getNextMeaningfulToken($insertIndex);
                 if (null !== $nextMeaningfulAfterUseIndex && $tokens[$nextMeaningfulAfterUseIndex]->isGivenKind(T_USE)) {
                     if (substr_count($nextToken->getContent(), "\n") < 2) {
                         $nextToken->setContent($newline . $indent . ltrim($nextToken->getContent()));
                     }
                 } else {
                     $nextToken->setContent($newline . $indent . ltrim($nextToken->getContent()));
                 }
             } else {
                 $tokens->insertAt($insertIndex, new Token(array(T_WHITESPACE, $newline . $indent)));
             }
         }
     }
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:56,代码来源:SingleLineAfterImportsFixer.php

示例13: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(T_FUNCTION)) {
             continue;
         }
         $index = $tokens->getNextTokenOfKind($index, array(';', '{'));
         if ($tokens[$index]->equals('{')) {
             $this->fixFunction($tokens, $index, $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index));
         }
     }
 }
开发者ID:ritcenter,项目名称:PHP-CS-Fixer,代码行数:15,代码来源:NoUselessReturnFixer.php

示例14: replaceClassKeywords

 /**
  * Replaces ::class keyword, namespace by namespace.
  *
  * It uses recursive method to get rid of token index changes.
  *
  * @param Tokens $tokens
  * @param int    $namespaceNumber
  */
 private function replaceClassKeywords(Tokens $tokens, $namespaceNumber = -1)
 {
     $namespaceIndexes = array_keys($tokens->findGivenKind(T_NAMESPACE));
     // Namespace blocks
     if (!empty($namespaceIndexes) && isset($namespaceIndexes[$namespaceNumber])) {
         $startIndex = $namespaceIndexes[$namespaceNumber];
         $namespaceBlockStartIndex = $tokens->getNextTokenOfKind($startIndex, array(';', '{'));
         $endIndex = $tokens[$namespaceBlockStartIndex]->equals('{') ? $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $namespaceBlockStartIndex) : $tokens->getNextTokenOfKind($namespaceBlockStartIndex, array(T_NAMESPACE));
         $endIndex = $endIndex ?: $tokens->count() - 1;
     } elseif (-1 === $namespaceNumber) {
         // Out of any namespace block
         $startIndex = 0;
         $endIndex = !empty($namespaceIndexes) ? $namespaceIndexes[0] : $tokens->count() - 1;
     } else {
         return;
     }
     $this->storeImports($tokens, $startIndex, $endIndex);
     $tokens->rewind();
     $this->replaceClassKeywordsSection($tokens, $startIndex, $endIndex);
     $this->replaceClassKeywords($tokens, $namespaceNumber + 1);
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:29,代码来源:ClassKeywordRemoveFixer.php

示例15: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens as $index => $token) {
         if (!$token->isClassy()) {
             continue;
         }
         $startBraceIndex = $tokens->getNextTokenOfKind($index, array('{'));
         if (!$tokens[$startBraceIndex + 1]->isWhitespace()) {
             continue;
         }
         $this->fixWhitespace($tokens[$startBraceIndex + 1]);
     }
 }
开发者ID:infinitely-young,项目名称:PHP-CS-Fixer,代码行数:16,代码来源:NoBlankLinesAfterClassOpeningFixer.php


注:本文中的PhpCsFixer\Tokenizer\Tokens::getNextTokenOfKind方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。