PhpCsFixer\Tokenizer\Tokens::detectBlockType PHP Method

detectBlockType() public static method

Detect type of block.
public static detectBlockType ( PhpCsFixer\Tokenizer\Token $token ) : null | array
$token PhpCsFixer\Tokenizer\Token token
return null | array array with 'type' and 'isStart' keys or null if not found
    public static function detectBlockType(Token $token)
    {
        foreach (self::getBlockEdgeDefinitions() as $type => $definition) {
            if ($token->equals($definition['start'])) {
                return array('type' => $type, 'isStart' => true);
            }
            if ($token->equals($definition['end'])) {
                return array('type' => $type, 'isStart' => false);
            }
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @param Tokens $tokens
  * @param int    $index
  *
  * @return int
  */
 private function findStart(Tokens $tokens, $index)
 {
     do {
         $index = $tokens->getPrevMeaningfulToken($index);
         $token = $tokens[$index];
         $blockType = $tokens->detectBlockType($token);
         if (null !== $blockType && !$blockType['isStart']) {
             $index = $tokens->findBlockEnd($blockType['type'], $index, false);
             $token = $tokens[$index];
         }
     } while (!$token->equalsAny(array('$', array(T_VARIABLE))));
     $prevIndex = $tokens->getPrevMeaningfulToken($index);
     $prevToken = $tokens[$prevIndex];
     if ($prevToken->equals('$')) {
         $index = $prevIndex;
         $prevIndex = $tokens->getPrevMeaningfulToken($index);
         $prevToken = $tokens[$prevIndex];
     }
     if ($prevToken->isGivenKind(T_OBJECT_OPERATOR)) {
         return $this->findStart($tokens, $prevIndex);
     }
     if ($prevToken->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM)) {
         $prevPrevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
         if (!$tokens[$prevPrevIndex]->isGivenKind(T_STRING)) {
             return $this->findStart($tokens, $prevIndex);
         }
         $index = $tokens->getTokenNotOfKindSibling($prevIndex, -1, array(array(T_NS_SEPARATOR), array(T_STRING)));
         $index = $tokens->getNextMeaningfulToken($index);
     }
     return $index;
 }
All Usage Examples Of PhpCsFixer\Tokenizer\Tokens::detectBlockType