PhpCsFixer\Tokenizer\Tokens::getTokenOfKindSibling PHP Method

getTokenOfKindSibling() public method

Get index for closest sibling token of given kind.
public getTokenOfKindSibling ( integer $index, integer $direction, array $tokens = [], boolean $caseSensitive = true ) : integer | null
$index integer token index
$direction integer direction for looking, +1 or -1
$tokens array possible tokens
$caseSensitive boolean perform a case sensitive comparison
return integer | null
    public function getTokenOfKindSibling($index, $direction, array $tokens = array(), $caseSensitive = true)
    {
        while (true) {
            $index += $direction;
            if (!$this->offsetExists($index)) {
                return;
            }
            $token = $this[$index];
            if ($token->equalsAny($tokens, $caseSensitive)) {
                return $index;
            }
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @param Tokens $tokens
  * @param int    $index           Index of the token to check
  * @param int    $lowerLimitIndex Lower limit index. Since the token to check will always be in a conditional we must stop checking at this index.
  *
  * @return bool
  */
 private function isInConditional(Tokens $tokens, $index, $lowerLimitIndex)
 {
     $candidateIndex = $tokens->getTokenOfKindSibling($index, -1, array(')', ';', ':'));
     if ($tokens[$candidateIndex]->equals(':')) {
         return true;
     }
     if (!$tokens[$candidateIndex]->equals(')')) {
         return false;
         // token is ';' or close tag
     }
     // token is always ')' here.
     // If it is part of the condition the token is always in, return false.
     // If it is not it is a nested condition so return true
     $open = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $candidateIndex, false);
     return $tokens->getPrevMeaningfulToken($open) > $lowerLimitIndex;
 }