PhpCsFixer\Tokenizer\TokensAnalyzer::isUnaryPredecessorOperator PHP Method

isUnaryPredecessorOperator() public method

Checks if there is an unary predecessor operator under given index.
public isUnaryPredecessorOperator ( integer $index ) : boolean
$index integer
return boolean
    public function isUnaryPredecessorOperator($index)
    {
        static $potentialSuccessorOperator = array(T_INC, T_DEC);
        static $potentialBinaryOperator = array('+', '-', '&', array(CT::T_RETURN_REF));
        static $otherOperators;
        if (null === $otherOperators) {
            $otherOperators = array('!', '~', '@');
            if (defined('T_ELLIPSIS')) {
                $otherOperators[] = array(T_ELLIPSIS);
            }
        }
        static $disallowedPrevTokens;
        if (null === $disallowedPrevTokens) {
            $disallowedPrevTokens = array(']', '}', ')', '"', '`', array(CT::T_ARRAY_SQUARE_BRACE_CLOSE), array(CT::T_DYNAMIC_PROP_BRACE_CLOSE), array(CT::T_DYNAMIC_VAR_BRACE_CLOSE), array(T_CLASS_C), array(T_CONSTANT_ENCAPSED_STRING), array(T_DEC), array(T_DIR), array(T_DNUMBER), array(T_FILE), array(T_FUNC_C), array(T_INC), array(T_LINE), array(T_LNUMBER), array(T_METHOD_C), array(T_NS_C), array(T_STRING), array(T_VARIABLE));
            if (defined('T_TRAIT_C')) {
                $disallowedPrevTokens[] = array(T_TRAIT_C);
            }
        }
        $tokens = $this->tokens;
        $token = $tokens[$index];
        if ($token->isGivenKind($potentialSuccessorOperator)) {
            return !$this->isUnarySuccessorOperator($index);
        }
        if ($token->equalsAny($otherOperators)) {
            return true;
        }
        if (!$token->equalsAny($potentialBinaryOperator)) {
            return false;
        }
        $prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
        if (!$prevToken->equalsAny($disallowedPrevTokens)) {
            return true;
        }
        if (!$token->equals('&') || !$prevToken->isGivenKind(T_STRING)) {
            return false;
        }
        static $searchTokens = array(';', '{', '}', array(T_FUNCTION), array(T_OPEN_TAG), array(T_OPEN_TAG_WITH_ECHO));
        $prevToken = $tokens[$tokens->getPrevTokenOfKind($index, $searchTokens)];
        return $prevToken->isGivenKind(T_FUNCTION);
    }

Usage Example

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