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


PHP Tokens::getPrevTokenOfKind方法代码示例

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


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

示例1: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     $classes = array_keys($tokens->findGivenKind(T_CLASS));
     $numClasses = count($classes);
     for ($i = 0; $i < $numClasses; ++$i) {
         $index = $classes[$i];
         // is it an an anonymous class definition?
         if ($tokensAnalyzer->isAnonymousClass($index)) {
             continue;
         }
         // is it inside a namespace?
         $nspIndex = $tokens->getPrevTokenOfKind($index, array(array(T_NAMESPACE, 'namespace')));
         if (null !== $nspIndex) {
             $nspIndex = $tokens->getNextMeaningfulToken($nspIndex);
             // make sure it's not the global namespace, as PHP4 constructors are allowed in there
             if (!$tokens[$nspIndex]->equals('{')) {
                 // unless it's the global namespace, the index currently points to the name
                 $nspIndex = $tokens->getNextTokenOfKind($nspIndex, array(';', '{'));
                 if ($tokens[$nspIndex]->equals(';')) {
                     // the class is inside a (non-block) namespace, no PHP4-code should be in there
                     break;
                 }
                 // the index points to the { of a block-namespace
                 $nspEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $nspIndex);
                 if ($index < $nspEnd) {
                     // the class is inside a block namespace, skip other classes that might be in it
                     for ($j = $i + 1; $j < $numClasses; ++$j) {
                         if ($classes[$j] < $nspEnd) {
                             ++$i;
                         }
                     }
                     // and continue checking the classes that might follow
                     continue;
                 }
             }
         }
         $classNameIndex = $tokens->getNextMeaningfulToken($index);
         $className = $tokens[$classNameIndex]->getContent();
         $classStart = $tokens->getNextTokenOfKind($classNameIndex, array('{'));
         $classEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $classStart);
         $this->fixConstructor($tokens, $className, $classStart, $classEnd);
         $this->fixParent($tokens, $classStart, $classEnd);
     }
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:48,代码来源:NoPhp4ConstructorFixer.php

示例2: isTypehintedNullableVariable

 /**
  * @param Tokens $tokens
  * @param int    $index  Index of "="
  *
  * @return bool
  */
 private function isTypehintedNullableVariable(Tokens $tokens, $index)
 {
     $nextToken = $tokens[$tokens->getNextMeaningfulToken($index)];
     if (!$nextToken->equals(array(T_STRING, 'null'), false)) {
         return false;
     }
     $variableIndex = $tokens->getPrevMeaningfulToken($index);
     $searchTokens = array(',', '(', array(T_STRING), array(CT::T_ARRAY_TYPEHINT));
     $typehintKinds = array(T_STRING, CT::T_ARRAY_TYPEHINT);
     if (defined('T_CALLABLE')) {
         $searchTokens[] = array(T_CALLABLE);
         $typehintKinds[] = T_CALLABLE;
     }
     $prevIndex = $tokens->getPrevTokenOfKind($variableIndex, $searchTokens);
     return $tokens[$prevIndex]->isGivenKind($typehintKinds);
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:22,代码来源:NoUnreachableDefaultArgumentValueFixer.php

示例3: makeClassyInheritancePartMultiLine

 /**
  * @param Tokens $tokens
  * @param int    $startIndex
  * @param int    $endIndex
  */
 private function makeClassyInheritancePartMultiLine(Tokens $tokens, $startIndex, $endIndex)
 {
     for ($i = $endIndex; $i > $startIndex; --$i) {
         $previousInterfaceImplementingIndex = $tokens->getPrevTokenOfKind($i, array(',', array(T_IMPLEMENTS), array(T_EXTENDS)));
         $breakAtIndex = $tokens->getNextMeaningfulToken($previousInterfaceImplementingIndex);
         // make the part of a ',' or 'implements' single line
         $this->makeClassyDefinitionSingleLine($tokens, $breakAtIndex, $i);
         // make sure the part is on its own line
         $isOnOwnLine = false;
         for ($j = $breakAtIndex; $j > $previousInterfaceImplementingIndex; --$j) {
             if (false !== strpos($tokens[$j]->getContent(), "\n")) {
                 $isOnOwnLine = true;
                 break;
             }
         }
         if (!$isOnOwnLine) {
             if ($tokens[$breakAtIndex - 1]->isWhitespace()) {
                 $tokens[$breakAtIndex - 1]->setContent($this->whitespacesConfig->getLineEnding() . $this->whitespacesConfig->getIndent());
             } else {
                 $tokens->insertAt($breakAtIndex, new Token(array(T_WHITESPACE, $this->whitespacesConfig->getLineEnding() . $this->whitespacesConfig->getIndent())));
             }
         }
         $i = $previousInterfaceImplementingIndex + 1;
     }
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:30,代码来源:ClassDefinitionFixer.php

示例4: fixPropertyVisibility

 /**
  * @param Tokens $tokens
  * @param int    $index
  */
 private function fixPropertyVisibility(Tokens $tokens, $index)
 {
     $prevIndex = $tokens->getPrevTokenOfKind($index, array(';', ',', '{'));
     if (null === $prevIndex || !$tokens[$prevIndex]->equals(',')) {
         $this->overrideAttribs($tokens, $index, $this->grabAttribsBeforePropertyToken($tokens, $index));
     }
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:11,代码来源:VisibilityRequiredFixer.php

示例5: isInConditional

 /**
  * @param Tokens $tokens
  * @param int    $index           Index of the token to check
  * @param int    $lowerLimitIndex Lower limit index. Since the token to check will always be in a conditional we must stop checking at this index
  *
  * @return bool
  */
 private function isInConditional(Tokens $tokens, $index, $lowerLimitIndex)
 {
     $candidateIndex = $tokens->getPrevTokenOfKind($index, array(')', ';', ':'));
     if ($tokens[$candidateIndex]->equals(':')) {
         return true;
     }
     if (!$tokens[$candidateIndex]->equals(')')) {
         return false;
         // token is ';' or close tag
     }
     // token is always ')' here.
     // If it is part of the condition the token is always in, return false.
     // If it is not it is a nested condition so return true
     $open = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $candidateIndex, false);
     return $tokens->getPrevMeaningfulToken($open) > $lowerLimitIndex;
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:23,代码来源:NoUselessElseFixer.php

示例6: getPreviousBlock

 /**
  * Return the first and last token index of the previous block.
  *
  * [0] First is either T_IF, T_ELSE or T_ELSEIF
  * [1] Last is either '}' or ';' / T_CLOSE_TAG for short notation blocks
  *
  * @param Tokens $tokens
  * @param int    $index  T_IF, T_ELSE, T_ELSEIF
  *
  * @return int[]
  */
 private function getPreviousBlock(Tokens $tokens, $index)
 {
     $close = $previous = $tokens->getPrevMeaningfulToken($index);
     // short 'if' detection
     if ($tokens[$close]->equals('}')) {
         $previous = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $close, false);
     }
     $open = $tokens->getPrevTokenOfKind($previous, array(array(T_IF), array(T_ELSE), array(T_ELSEIF)));
     if ($tokens[$open]->isGivenKind(T_IF)) {
         $elseCandidate = $tokens->getPrevMeaningfulToken($open);
         if ($tokens[$elseCandidate]->isGivenKind(T_ELSE)) {
             $open = $elseCandidate;
         }
     }
     return array($open, $close);
 }
开发者ID:thekabal,项目名称:tki,代码行数:27,代码来源:NoUselessElseFixer.php

示例7: detectIndent

 /**
  * @param Tokens $tokens
  * @param int    $index
  *
  * @return string
  */
 private function detectIndent(Tokens $tokens, $index)
 {
     while (true) {
         $whitespaceIndex = $tokens->getPrevTokenOfKind($index, array(array(T_WHITESPACE)));
         if (null === $whitespaceIndex) {
             return '';
         }
         $whitespaceToken = $tokens[$whitespaceIndex];
         if (false !== strpos($whitespaceToken->getContent(), "\n")) {
             break;
         }
         $prevToken = $tokens[$whitespaceIndex - 1];
         if ($prevToken->isGivenKind(array(T_OPEN_TAG, T_COMMENT)) && "\n" === substr($prevToken->getContent(), -1)) {
             break;
         }
         $index = $whitespaceIndex;
     }
     $explodedContent = explode("\n", $whitespaceToken->getContent());
     return end($explodedContent);
 }
开发者ID:GrahamForks,项目名称:PHP-CS-Fixer,代码行数:26,代码来源:BracesFixer.php

示例8: fixElement

 /**
  * @param Tokens $tokens
  * @param int    $index
  */
 private function fixElement(Tokens $tokens, $index)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     $repeatIndex = $index;
     while (true) {
         $repeatIndex = $tokens->getNextMeaningfulToken($repeatIndex);
         $repeatToken = $tokens[$repeatIndex];
         if ($tokensAnalyzer->isArray($repeatIndex)) {
             if ($repeatToken->isGivenKind(T_ARRAY)) {
                 $repeatIndex = $tokens->getNextTokenOfKind($repeatIndex, array('('));
                 $repeatIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $repeatIndex);
             } else {
                 $repeatIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $repeatIndex);
             }
             continue;
         }
         if ($repeatToken->equals(';')) {
             return;
             // no repeating found, no fixing needed
         }
         if ($repeatToken->equals(',')) {
             break;
         }
     }
     $start = $tokens->getPrevTokenOfKind($index, array(';', '{', '}'));
     $this->expandElement($tokens, $tokens->getNextMeaningfulToken($start), $tokens->getNextTokenOfKind($index, array(';')));
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:31,代码来源:SingleClassElementPerStatementFixer.php


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