PhpCsFixer\Tokenizer\Tokens::findBlockEnd PHP Method

findBlockEnd() public method

Find block end.
public findBlockEnd ( integer $type, integer $searchIndex, boolean $findEnd = true ) : integer
$type integer type of block, one of BLOCK_TYPE_*
$searchIndex integer index of opening brace
$findEnd boolean if method should find block's end, default true, otherwise method find block's start
return integer index of closing brace
    public function findBlockEnd($type, $searchIndex, $findEnd = true)
    {
        $blockEdgeDefinitions = self::getBlockEdgeDefinitions();
        if (!isset($blockEdgeDefinitions[$type])) {
            throw new \InvalidArgumentException(sprintf('Invalid param type: %s.', $type));
        }
        $startEdge = $blockEdgeDefinitions[$type]['start'];
        $endEdge = $blockEdgeDefinitions[$type]['end'];
        $startIndex = $searchIndex;
        $endIndex = $this->count() - 1;
        $indexOffset = 1;
        if (!$findEnd) {
            list($startEdge, $endEdge) = array($endEdge, $startEdge);
            $indexOffset = -1;
            $endIndex = 0;
        }
        if (!$this[$startIndex]->equals($startEdge)) {
            throw new \InvalidArgumentException(sprintf('Invalid param $startIndex - not a proper block %s.', $findEnd ? 'start' : 'end'));
        }
        $blockLevel = 0;
        for ($index = $startIndex; $index !== $endIndex; $index += $indexOffset) {
            $token = $this[$index];
            if ($token->equals($startEdge)) {
                ++$blockLevel;
                continue;
            }
            if ($token->equals($endEdge)) {
                --$blockLevel;
                if (0 === $blockLevel) {
                    break;
                }
                continue;
            }
        }
        if (!$this[$index]->equals($endEdge)) {
            throw new \UnexpectedValueException(sprintf('Missing block %s.', $findEnd ? 'end' : 'start'));
        }
        return $index;
    }

Usage Example

 /**
  * 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;
 }
All Usage Examples Of PhpCsFixer\Tokenizer\Tokens::findBlockEnd