PhpCsFixer\Tokenizer\TokensAnalyzer::isBinaryOperator PHP Метод

isBinaryOperator() публичный Метод

Checks if there is a binary operator under given index.
public isBinaryOperator ( integer $index ) : boolean
$index integer
Результат boolean
    public function isBinaryOperator($index)
    {
        static $nonArrayOperators = array('=' => true, '*' => true, '/' => true, '%' => true, '<' => true, '>' => true, '|' => true, '^' => true);
        static $potentialUnaryNonArrayOperators = array('+' => true, '-' => true, '&' => true);
        static $arrayOperators;
        if (null === $arrayOperators) {
            $arrayOperators = array(T_AND_EQUAL => true, T_BOOLEAN_AND => true, T_BOOLEAN_OR => true, T_CONCAT_EQUAL => true, T_DIV_EQUAL => true, T_DOUBLE_ARROW => true, T_IS_EQUAL => true, T_IS_GREATER_OR_EQUAL => true, T_IS_IDENTICAL => true, T_IS_NOT_EQUAL => true, T_IS_NOT_IDENTICAL => true, T_IS_SMALLER_OR_EQUAL => true, T_LOGICAL_AND => true, T_LOGICAL_OR => true, T_LOGICAL_XOR => true, T_MINUS_EQUAL => true, T_MOD_EQUAL => true, T_MUL_EQUAL => true, T_OR_EQUAL => true, T_PLUS_EQUAL => true, T_SL => true, T_SL_EQUAL => true, T_SR => true, T_SR_EQUAL => true, T_XOR_EQUAL => true);
            if (defined('T_POW')) {
                $arrayOperators[T_POW] = true;
                // **
                $arrayOperators[T_POW_EQUAL] = true;
                // **=
            }
            if (defined('T_SPACESHIP')) {
                $arrayOperators[T_SPACESHIP] = true;
                // <=>
            }
            if (defined('T_COALESCE')) {
                $arrayOperators[T_COALESCE] = true;
                // ??
            }
        }
        $tokens = $this->tokens;
        $token = $tokens[$index];
        if ($token->isArray()) {
            return isset($arrayOperators[$token->getId()]);
        }
        if (isset($nonArrayOperators[$token->getContent()])) {
            return true;
        }
        if (isset($potentialUnaryNonArrayOperators[$token->getContent()])) {
            return !$this->isUnaryPredecessorOperator($index);
        }
        return false;
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         if (!$tokensAnalyzer->isBinaryOperator($index)) {
             continue;
         }
         if (!$tokens[$index + 1]->isWhitespace()) {
             $tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
         }
         if (!$tokens[$index - 1]->isWhitespace()) {
             $tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
         }
     }
 }
All Usage Examples Of PhpCsFixer\Tokenizer\TokensAnalyzer::isBinaryOperator