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


PHP Tokens::count方法代码示例

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


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

示例1: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(T_DOC_COMMENT)) {
             continue;
         }
         if (1 === preg_match('/inheritdoc/i', $token->getContent())) {
             continue;
         }
         $index = $tokens->getNextMeaningfulToken($index);
         if (null === $index) {
             return;
         }
         while ($tokens[$index]->isGivenKind(array(T_ABSTRACT, T_FINAL, T_PRIVATE, T_PROTECTED, T_PUBLIC, T_STATIC, T_VAR))) {
             $index = $tokens->getNextMeaningfulToken($index);
         }
         if (!$tokens[$index]->isGivenKind(T_FUNCTION)) {
             continue;
         }
         $openIndex = $tokens->getNextTokenOfKind($index, array('('));
         $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);
         $arguments = array();
         foreach ($this->getArguments($tokens, $openIndex, $index) as $start => $end) {
             $argumentInfo = $this->prepareArgumentInformation($tokens, $start, $end);
             if (!$this->configuration['only_untyped'] || '' === $argumentInfo['type']) {
                 $arguments[$argumentInfo['name']] = $argumentInfo;
             }
         }
         if (!count($arguments)) {
             continue;
         }
         $doc = new DocBlock($token->getContent());
         $lastParamLine = null;
         foreach ($doc->getAnnotationsOfType('param') as $annotation) {
             $pregMatched = preg_match('/^[^$]+(\\$\\w+).*$/s', $annotation->getContent(), $matches);
             if (1 === $pregMatched) {
                 unset($arguments[$matches[1]]);
             }
             $lastParamLine = max($lastParamLine, $annotation->getEnd());
         }
         if (!count($arguments)) {
             continue;
         }
         $lines = $doc->getLines();
         $linesCount = count($lines);
         preg_match('/^(\\s*).*$/', $lines[$linesCount - 1]->getContent(), $matches);
         $indent = $matches[1];
         $newLines = array();
         foreach ($arguments as $argument) {
             $type = $argument['type'] ?: 'mixed';
             if ('?' !== $type[0] && 'null' === strtolower($argument['default'])) {
                 $type = 'null|' . $type;
             }
             $newLines[] = new Line(sprintf('%s* @param %s %s%s', $indent, $type, $argument['name'], $this->whitespacesConfig->getLineEnding()));
         }
         array_splice($lines, $lastParamLine ? $lastParamLine + 1 : $linesCount - 1, 0, $newLines);
         $token->setContent(implode('', $lines));
     }
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:63,代码来源:PhpdocAddMissingParamAnnotationFixer.php

示例2: find

 /**
  * Looks up Tokens sequence for suitable candidates and delivers boundaries information,
  * which can be supplied by other methods in this abstract class.
  *
  * @param string   $functionNameToSearch
  * @param Tokens   $tokens
  * @param int      $start
  * @param int|null $end
  *
  * @return int[]|null returns $functionName, $openParenthesis, $closeParenthesis packed into array
  */
 protected function find($functionNameToSearch, Tokens $tokens, $start = 0, $end = null)
 {
     // make interface consistent with findSequence
     $end = null === $end ? $tokens->count() : $end;
     // find raw sequence which we can analyse for context
     $candidateSequence = array(array(T_STRING, $functionNameToSearch), '(');
     $matches = $tokens->findSequence($candidateSequence, $start, $end, false);
     if (null === $matches) {
         // not found, simply return without further attempts
         return;
     }
     // translate results for humans
     list($functionName, $openParenthesis) = array_keys($matches);
     // first criteria check: shall look like function call
     $functionNamePrefix = $tokens->getPrevMeaningfulToken($functionName);
     $functionNamePrecedingToken = $tokens[$functionNamePrefix];
     if ($functionNamePrecedingToken->isGivenKind(array(T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION))) {
         // this expression is differs from expected, resume
         return $this->find($functionNameToSearch, $tokens, $openParenthesis, $end);
     }
     // second criteria check: ensure namespace is the root one
     if ($functionNamePrecedingToken->isGivenKind(T_NS_SEPARATOR)) {
         $namespaceCandidate = $tokens->getPrevMeaningfulToken($functionNamePrefix);
         $namespaceCandidateToken = $tokens[$namespaceCandidate];
         if ($namespaceCandidateToken->isGivenKind(array(T_NEW, T_STRING, CT::T_NAMESPACE_OPERATOR))) {
             // here can be added complete namespace scan
             // this expression is differs from expected, resume
             return $this->find($functionNameToSearch, $tokens, $openParenthesis, $end);
         }
     }
     // final step: find closing parenthesis
     $closeParenthesis = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesis);
     return array($functionName, $openParenthesis, $closeParenthesis);
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:45,代码来源:AbstractFunctionReferenceFixer.php

示例3: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     static $nativeFunctionNames = null;
     if (null === $nativeFunctionNames) {
         $nativeFunctionNames = $this->getNativeFunctionNames();
     }
     for ($index = 0, $count = $tokens->count(); $index < $count; ++$index) {
         // test if we are at a function all
         if (!$tokens[$index]->isGivenKind(T_STRING)) {
             continue;
         }
         $next = $tokens->getNextMeaningfulToken($index);
         if (!$tokens[$next]->equals('(')) {
             $index = $next;
             continue;
         }
         $functionNamePrefix = $tokens->getPrevMeaningfulToken($index);
         if ($tokens[$functionNamePrefix]->isGivenKind(array(T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION))) {
             continue;
         }
         // do not though the function call if it is to a function in a namespace other than the default
         if ($tokens[$functionNamePrefix]->isGivenKind(T_NS_SEPARATOR) && $tokens[$tokens->getPrevMeaningfulToken($functionNamePrefix)]->isGivenKind(T_STRING)) {
             continue;
         }
         // test if the function call is to a native PHP function
         $lower = strtolower($tokens[$index]->getContent());
         if (!array_key_exists($lower, $nativeFunctionNames)) {
             continue;
         }
         $tokens[$index]->setContent($nativeFunctionNames[$lower]);
         $index = $next;
     }
 }
开发者ID:thekabal,项目名称:tki,代码行数:36,代码来源:NativeFunctionCasingFixer.php

示例4: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         if (!$tokens[$index]->isGivenKind(T_UNSET)) {
             continue;
         }
         $previousUnsetCall = $this->getPreviousUnsetCall($tokens, $index);
         if (is_int($previousUnsetCall)) {
             $index = $previousUnsetCall;
             continue;
         }
         list($previousUnset, $previousUnsetBraceStart, $previousUnsetBraceEnd, $previousUnsetSemicolon) = $previousUnsetCall;
         // Merge the tokens inside the 'unset' call into the previous one 'unset' call.
         $tokensAddCount = $this->moveTokens($tokens, $nextUnsetContentStart = $tokens->getNextTokenOfKind($index, array('(')), $nextUnsetContentEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextUnsetContentStart), $previousUnsetBraceEnd - 1);
         if (!$tokens[$previousUnsetBraceEnd]->isWhitespace()) {
             $tokens->insertAt($previousUnsetBraceEnd, new Token(array(T_WHITESPACE, ' ')));
             ++$tokensAddCount;
         }
         $tokens->insertAt($previousUnsetBraceEnd, new Token(','));
         ++$tokensAddCount;
         // Remove 'unset', '(', ')' and (possibly) ';' from the merged 'unset' call.
         $this->clearOffsetTokens($tokens, $tokensAddCount, array($index, $nextUnsetContentStart, $nextUnsetContentEnd));
         $nextUnsetSemicolon = $tokens->getNextMeaningfulToken($nextUnsetContentEnd);
         if (null !== $nextUnsetSemicolon && $tokens[$nextUnsetSemicolon]->equals(';')) {
             $tokens->clearTokenAndMergeSurroundingWhitespace($nextUnsetSemicolon);
         }
         $index = $previousUnset + 1;
     }
 }
开发者ID:thekabal,项目名称:tki,代码行数:32,代码来源:CombineConsecutiveUnsetsFixer.php

示例5: 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

示例6: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $lineEnding = $this->whitespacesConfig->getLineEnding();
     for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(T_RETURN)) {
             continue;
         }
         $prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)];
         if (!$prevNonWhitespaceToken->equalsAny(array(';', '}'))) {
             continue;
         }
         $prevToken = $tokens[$index - 1];
         if ($prevToken->isWhitespace()) {
             $parts = explode("\n", $prevToken->getContent());
             $countParts = count($parts);
             if (1 === $countParts) {
                 $prevToken->setContent(rtrim($prevToken->getContent(), " \t") . $lineEnding . $lineEnding);
             } elseif (count($parts) <= 2) {
                 $prevToken->setContent($lineEnding . $prevToken->getContent());
             }
         } else {
             $tokens->insertAt($index, new Token(array(T_WHITESPACE, $lineEnding . $lineEnding)));
             ++$index;
             ++$limit;
         }
     }
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:31,代码来源:BlankLineBeforeReturnFixer.php

示例7: injectAlignmentPlaceholders

 /**
  * Inject into the text placeholders of candidates of vertical alignment.
  *
  * @param Tokens $tokens
  */
 private function injectAlignmentPlaceholders(Tokens $tokens)
 {
     $deepestLevel = 0;
     $limit = $tokens->count();
     for ($index = 0; $index < $limit; ++$index) {
         $token = $tokens[$index];
         if ($token->equals('=')) {
             $token->setContent(sprintf(self::ALIGNABLE_PLACEHOLDER, $deepestLevel) . $token->getContent());
             continue;
         }
         if ($token->isGivenKind(T_FUNCTION)) {
             ++$deepestLevel;
             continue;
         }
         if ($token->equals('(')) {
             $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
             continue;
         }
         if ($token->equals('[')) {
             $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $index);
             continue;
         }
         if ($token->isGivenKind(CT_ARRAY_SQUARE_BRACE_OPEN)) {
             $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index);
             continue;
         }
     }
     $this->deepestLevel = $deepestLevel;
 }
开发者ID:infinitely-young,项目名称:PHP-CS-Fixer,代码行数:34,代码来源:AlignEqualsFixer.php

示例8: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $count = $tokens->count();
     if ($count && !$tokens[$count - 1]->isGivenKind(array(T_INLINE_HTML, T_CLOSE_TAG, T_OPEN_TAG))) {
         $tokens->ensureWhitespaceAtIndex($count - 1, 1, $this->whitespacesConfig->getLineEnding());
     }
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:10,代码来源:SingleBlankLineAtEofFixer.php

示例9: 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

示例10: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         if ($tokens[$index]->isGivenKind(array(T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN))) {
             $this->fixSpacing($index, $tokens);
         }
     }
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:11,代码来源:NoWhitespaceBeforeCommaInArrayFixer.php

示例11: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = 0, $c = $tokens->count(); $index < $c; ++$index) {
         if ($tokens[$index]->isGivenKind(array(T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN))) {
             self::fixArray($tokens, $index);
         }
     }
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:11,代码来源:TrimArraySpacesFixer.php

示例12: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         $token = $tokens[$index];
         if ($token->isGivenKind(T_NAMESPACE)) {
             $this->fixLinesBeforeNamespace($tokens, $index, 2);
         }
     }
 }
开发者ID:cryode,项目名称:PHP-CS-Fixer,代码行数:12,代码来源:SingleBlankLineBeforeNamespaceFixer.php

示例13: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         if ($tokensAnalyzer->isArray($index)) {
             $this->fixArray($tokens, $index);
         }
     }
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:12,代码来源:TrailingCommaInMultilineArrayFixer.php

示例14: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $callBack = $this->fixCallback;
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         if ($tokens[$index]->equals('.')) {
             $this->{$callBack}($tokens, $index);
         }
     }
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:12,代码来源:ConcatSpaceFixer.php

示例15: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $callback = $this->fixCallback;
     for ($index = $tokens->count() - 1; 0 <= $index; --$index) {
         if ($tokens[$index]->isGivenKind($this->candidateTokenKind)) {
             $this->{$callback}($tokens, $index);
         }
     }
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:12,代码来源:ArraySyntaxFixer.php


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