PhpCsFixer\Tokenizer\Tokens::findGivenKind PHP Method

findGivenKind() public method

Find tokens of given kind.
public findGivenKind ( integer | array $possibleKind, integer $start, integer | null $end = null ) : array
$possibleKind integer | array kind or array of kind
$start integer optional offset
$end integer | null optional limit
return array array of tokens of given kinds or assoc array of arrays
    public function findGivenKind($possibleKind, $start = 0, $end = null)
    {
        $this->rewind();
        if (null === $end) {
            $end = $this->count();
        }
        $elements = array();
        $possibleKinds = (array) $possibleKind;
        foreach ($possibleKinds as $kind) {
            $elements[$kind] = array();
        }
        for ($i = $start; $i < $end; ++$i) {
            $token = $this[$i];
            if ($token->isGivenKind($possibleKinds)) {
                $elements[$token->getId()][$i] = $token;
            }
        }
        return is_array($possibleKind) ? $elements : $elements[$possibleKind];
    }

Usage Example

Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     /** @var $token \PhpCsFixer\Tokenizer\Token */
     foreach ($tokens->findGivenKind(T_STRING) as $index => $token) {
         // skip expressions without parameters list
         $nextToken = $tokens[$tokens->getNextMeaningfulToken($index)];
         if (!$nextToken->equals('(')) {
             continue;
         }
         // skip expressions which are not function reference
         $prevTokenIndex = $tokens->getPrevMeaningfulToken($index);
         $prevToken = $tokens[$prevTokenIndex];
         if ($prevToken->isGivenKind(array(T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION))) {
             continue;
         }
         // handle function reference with namespaces
         if ($prevToken->isGivenKind(array(T_NS_SEPARATOR))) {
             $twicePrevTokenIndex = $tokens->getPrevMeaningfulToken($prevTokenIndex);
             $twicePrevToken = $tokens[$twicePrevTokenIndex];
             if ($twicePrevToken->isGivenKind(array(T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION, T_STRING, CT::T_NAMESPACE_OPERATOR))) {
                 continue;
             }
         }
         // check mapping hit
         $tokenContent = strtolower($token->getContent());
         if (!isset(self::$aliases[$tokenContent])) {
             continue;
         }
         $token->setContent(self::$aliases[$tokenContent]);
     }
 }
All Usage Examples Of PhpCsFixer\Tokenizer\Tokens::findGivenKind