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
 /**
  * {@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]);
     }
 }
示例#2
0
 /**
  * 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');
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) {
         $doc = new DocBlock($token->getContent());
         $annotations = $doc->getAnnotationsOfType(static::$search);
         if (empty($annotations)) {
             continue;
         }
         foreach ($annotations as $annotation) {
             $annotation->getTag()->setName(static::$replace);
         }
         $token->setContent($doc->getContent());
     }
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     if (!$tokens->isMonolithicPhp()) {
         return;
     }
     $closeTags = $tokens->findGivenKind(T_CLOSE_TAG);
     if (empty($closeTags)) {
         return;
     }
     list($index, $token) = each($closeTags);
     $tokens->removeLeadingWhitespace($index);
     $token->clear();
     $prevIndex = $tokens->getPrevNonWhitespace($index);
     $prevToken = $tokens[$prevIndex];
     if (!$prevToken->equalsAny(array(';', '}'))) {
         $tokens->insertAt($prevIndex + 1, new Token(';'));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $foundNamespace = $tokens->findGivenKind(T_NAMESPACE);
     if (empty($foundNamespace)) {
         return;
     }
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     $firstNamespaceIdx = key($foundNamespace);
     $usesIdxs = $tokensAnalyzer->getImportUseIndexes();
     foreach ($usesIdxs as $idx) {
         if ($idx < $firstNamespaceIdx) {
             continue;
         }
         $nextTokenIdx = $tokens->getNextNonWhitespace($idx);
         $nextToken = $tokens[$nextTokenIdx];
         if ($nextToken->isGivenKind(T_NS_SEPARATOR)) {
             $nextToken->clear();
         }
     }
 }
示例#6
0
 private function findIncludies(Tokens $tokens)
 {
     static $includyTokenKinds = array(T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE);
     $includies = array();
     foreach ($tokens->findGivenKind($includyTokenKinds) as $includyTokens) {
         foreach ($includyTokens as $index => $token) {
             $includy = array('begin' => $index, 'braces' => null, 'end' => $tokens->getNextTokenOfKind($index, array(';')));
             $nextTokenIndex = $tokens->getNextMeaningfulToken($index);
             $nextToken = $tokens[$nextTokenIndex];
             if ($nextToken->equals('(')) {
                 // Don't remove braces when the statement is wrapped.
                 // Include is also legal as function parameter or condition statement but requires being wrapped then.
                 $braceCloseIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextTokenIndex);
                 if ($tokens[$tokens->getNextMeaningfulToken($braceCloseIndex)]->equals(';')) {
                     $includy['braces'] = array('open' => $nextTokenIndex, 'close' => $braceCloseIndex);
                 }
             }
             $includies[] = $includy;
         }
     }
     return $includies;
 }
 /**
  * Returns an array with `implements` data.
  *
  * Returns array:
  * * int  'breakAt'            index of the Token of type T_IMPLEMENTS for the definition, or 0
  * * int  'numberOfInterfaces'
  * * bool 'multiLine'
  *
  * @param Tokens $tokens
  * @param int    $start
  * @param int    $classyOpen
  *
  * @return array
  */
 private function getMultiLineInfo(Tokens $tokens, $start, $classyOpen)
 {
     $implementsInfo = array('breakAt' => 0, 'numberOfInterfaces' => 0, 'multiLine' => false);
     $breakAtToken = $tokens->findGivenKind($tokens[$start]->isGivenKind(T_INTERFACE) ? T_EXTENDS : T_IMPLEMENTS, $start, $classyOpen);
     if (count($breakAtToken) < 1) {
         return $implementsInfo;
     }
     $implementsInfo['breakAt'] = key($breakAtToken);
     $classyOpen = $tokens->getPrevNonWhitespace($classyOpen);
     for ($j = $implementsInfo['breakAt'] + 1; $j < $classyOpen; ++$j) {
         if ($tokens[$j]->isGivenKind(T_STRING)) {
             ++$implementsInfo['numberOfInterfaces'];
             continue;
         }
         if (!$implementsInfo['multiLine'] && ($tokens[$j]->isWhitespace() || $tokens[$j]->isComment()) && false !== strpos($tokens[$j]->getContent(), "\n")) {
             $implementsInfo['multiLine'] = true;
         }
     }
     return $implementsInfo;
 }
 /**
  * Fix calls to the parent constructor within a class.
  *
  * @param Tokens $tokens     the Tokens instance
  * @param int    $classStart the class start index
  * @param int    $classEnd   the class end index
  */
 private function fixParent(Tokens $tokens, $classStart, $classEnd)
 {
     // check calls to the parent constructor
     foreach ($tokens->findGivenKind(T_EXTENDS) as $index => $token) {
         $parentIndex = $tokens->getNextMeaningfulToken($index);
         $parentClass = $tokens[$parentIndex]->getContent();
         // using parent::ParentClassName() or ParentClassName::ParentClassName()
         $parentSeq = $tokens->findSequence(array(array(T_STRING), array(T_DOUBLE_COLON), array(T_STRING, $parentClass), '('), $classStart, $classEnd, array(2 => false));
         if (null !== $parentSeq) {
             // we only need indexes
             $parentSeq = array_keys($parentSeq);
             // match either of the possibilities
             if ($tokens[$parentSeq[0]]->equalsAny(array(array(T_STRING, 'parent'), array(T_STRING, $parentClass)), false)) {
                 // replace with parent::__construct
                 $tokens[$parentSeq[0]]->setContent('parent');
                 $tokens[$parentSeq[2]]->setContent('__construct');
             }
         }
         // using $this->ParentClassName()
         $parentSeq = $tokens->findSequence(array(array(T_VARIABLE, '$this'), array(T_OBJECT_OPERATOR), array(T_STRING, $parentClass), '('), $classStart, $classEnd, array(2 => false));
         if (null !== $parentSeq) {
             // we only need indexes
             $parentSeq = array_keys($parentSeq);
             // replace call with parent::__construct()
             $tokens[$parentSeq[0]] = new Token(array(T_STRING, 'parent'));
             $tokens[$parentSeq[1]] = new Token(array(T_DOUBLE_COLON, '::'));
             $tokens[$parentSeq[2]]->setContent('__construct');
         }
     }
 }
 /**
  * @param Tokens $tokens
  * @param int    $classyIndex
  *
  * @return array
  */
 private function getClassyDefinitionInfo(Tokens $tokens, $classyIndex)
 {
     $openIndex = $tokens->getNextTokenOfKind($classyIndex, array('{'));
     $prev = $tokens->getPrevMeaningfulToken($classyIndex);
     $startIndex = $tokens[$prev]->isGivenKind(array(T_FINAL, T_ABSTRACT)) ? $prev : $classyIndex;
     $extends = false;
     $implements = false;
     $anonymousClass = false;
     if (!(defined('T_TRAIT') && $tokens[$classyIndex]->isGivenKind(T_TRAIT))) {
         $extends = $tokens->findGivenKind(T_EXTENDS, $classyIndex, $openIndex);
         $extends = count($extends) ? $this->getClassyInheritanceInfo($tokens, key($extends), $openIndex, 'numberOfExtends') : false;
         if (!$tokens[$classyIndex]->isGivenKind(T_INTERFACE)) {
             $implements = $tokens->findGivenKind(T_IMPLEMENTS, $classyIndex, $openIndex);
             $implements = count($implements) ? $this->getClassyInheritanceInfo($tokens, key($implements), $openIndex, 'numberOfImplements') : false;
             $tokensAnalyzer = new TokensAnalyzer($tokens);
             $anonymousClass = $tokensAnalyzer->isAnonymousClass($classyIndex);
         }
     }
     return array('start' => $startIndex, 'classy' => $classyIndex, 'open' => $openIndex, 'extends' => $extends, 'implements' => $implements, 'anonymousClass' => $anonymousClass);
 }
 /**
  * @param Tokens $tokens
  * @param int    $startIndex
  * @param int    $endIndex
  */
 private function replaceClassKeywordsSection(Tokens $tokens, $startIndex, $endIndex)
 {
     $CTClassTokens = $tokens->findGivenKind(CT::T_CLASS_CONSTANT, $startIndex, $endIndex);
     if (!empty($CTClassTokens)) {
         $this->replaceClassKeyword($tokens, current(array_keys($CTClassTokens)));
         $this->replaceClassKeywordsSection($tokens, $startIndex, $endIndex);
     }
 }