src/Fixer/ControlStructure/YodaStyleFixer.php
private function isVariable(Tokens $tokens, $start, $end, $strict)
    {
        $tokenAnalyzer = new TokensAnalyzer($tokens);

        if ($start === $end) {
            return $tokens[$start]->isGivenKind(T_VARIABLE);
        }

        if ($strict) {
            if ($tokens[$start]->equals('(')) {
                return false;
            }

            for ($index = $start; $index <= $end; ++$index) {
                if (
                    $tokens[$index]->isCast()
                    || $tokens[$index]->isGivenKind(T_INSTANCEOF)
                    || $tokens[$index]->equals('!')
                    || $tokenAnalyzer->isBinaryOperator($index)
                ) {
                    return false;
                }
            }
        }

        $index = $start;

        // handle multiple braces around statement ((($a === 1)))
        while (
            $tokens[$index]->equals('(')
            && $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index) === $end
        ) {
            $index = $tokens->getNextMeaningfulToken($index);
            $end = $tokens->getPrevMeaningfulToken($end);
        }

        $expectString = false;
        while ($index <= $end) {
            $current = $tokens[$index];
            if ($current->isComment() || $current->isWhitespace() || $tokens->isEmptyAt($index)) {
                ++$index;

                continue;
            }

            // check if this is the last token
            if ($index === $end) {
                return $current->isGivenKind($expectString ? T_STRING : T_VARIABLE);
            }

            if ($current->isGivenKind([T_LIST, CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN, CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE])) {
                return false;
            }

            $nextIndex = $tokens->getNextMeaningfulToken($index);
            $next = $tokens[$nextIndex];

            // self:: or ClassName::
            if ($current->isGivenKind(T_STRING) && $next->isGivenKind(T_DOUBLE_COLON)) {
                $index = $tokens->getNextMeaningfulToken($nextIndex);

                continue;
            }

            // \ClassName
            if ($current->isGivenKind(T_NS_SEPARATOR) && $next->isGivenKind(T_STRING)) {
                $index = $nextIndex;

                continue;
            }

            // ClassName\
            if ($current->isGivenKind(T_STRING) && $next->isGivenKind(T_NS_SEPARATOR)) {
                $index = $nextIndex;

                continue;
            }

            // $a-> or a-> (as in $b->a->c)
            if ($current->isGivenKind([T_STRING, T_VARIABLE]) && $next->isGivenKind(T_OBJECT_OPERATOR)) {
                $index = $tokens->getNextMeaningfulToken($nextIndex);
                $expectString = true;

                continue;
            }

            // $a[...], a[...] (as in $c->a[$b]), $a{...} or a{...} (as in $c->a{$b})
            if (
                $current->isGivenKind($expectString ? T_STRING : T_VARIABLE)
                && $next->equalsAny(['[', [CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN, '{']])
            ) {
                $index = $tokens->findBlockEnd(
                    $next->equals('[') ? Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE : Tokens::BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE,
                    $nextIndex
                );

                if ($index === $end) {
                    return true;
                }

                $index = $tokens->getNextMeaningfulToken($index);

                if (!$tokens[$index]->equalsAny([[T_OBJECT_OPERATOR, '->'], '[', [CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN, '{']])) {
                    return false;
                }

                $index = $tokens->getNextMeaningfulToken($index);
                $expectString = true;

                continue;
            }

            // $a(...) or $a->b(...)
            if ($strict && $current->isGivenKind([T_STRING, T_VARIABLE]) && $next->equals('(')) {
                return false;
            }

            // {...} (as in $a->{$b})
            if ($expectString && $current->isGivenKind(CT::T_DYNAMIC_PROP_BRACE_OPEN)) {
                $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_DYNAMIC_PROP_BRACE, $index);
                if ($index === $end) {
                    return true;
                }

                $index = $tokens->getNextMeaningfulToken($index);

                if (!$tokens[$index]->isGivenKind(T_OBJECT_OPERATOR)) {
                    return false;
                }

                $index = $tokens->getNextMeaningfulToken($index);
                $expectString = true;

                continue;
            }

            break;
        }

        return !$this->isConstant($tokens, $start, $end);
    }
private function isVariable(Tokens $tokens, $start, $end, $strict)
    {
        $tokenAnalyzer = new TokensAnalyzer($tokens);

        if ($start === $end) {
            return $tokens[$start]->isGivenKind(T_VARIABLE);
        }

        if ($strict) {
            if ($tokens[$start]->equals('(')) {
                return false;
            }

            for ($index = $start; $index <= $end; ++$index) {
                if (
                    $tokens[$index]->isCast()
                    || $tokens[$index]->isGivenKind(T_INSTANCEOF)
                    || $tokens[$index]->equals('!')
                    || $tokenAnalyzer->isBinaryOperator($index)
                ) {
                    return false;
                }
            }
        }

        $index = $start;

        // handle multiple braces around statement ((($a === 1)))
        while (
            $tokens[$index]->equals('(')
            && $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index) === $end
        ) {
            $index = $tokens->getNextMeaningfulToken($index);
            $end = $tokens->getPrevMeaningfulToken($end);
        }

        $expectString = false;
        while ($index <= $end) {
            $current = $tokens[$index];
            if ($current->isComment() || $current->isWhitespace() || $tokens->isEmptyAt($index)) {
                ++$index;

                continue;
            }

            // check if this is the last token
            if ($index === $end) {
                return $current->isGivenKind($expectString ? T_STRING : T_VARIABLE);
            }

            if ($current->isGivenKind([T_LIST, CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN, CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE])) {
                return false;
            }

            $nextIndex = $tokens->getNextMeaningfulToken($index);
            $next = $tokens[$nextIndex];

            // self:: or ClassName::
            if ($current->isGivenKind(T_STRING) && $next->isGivenKind(T_DOUBLE_COLON)) {
                $index = $tokens->getNextMeaningfulToken($nextIndex);

                continue;
            }

            // \ClassName
            if ($current->isGivenKind(T_NS_SEPARATOR) && $next->isGivenKind(T_STRING)) {
                $index = $nextIndex;

                continue;
            }

            // ClassName\
            if ($current->isGivenKind(T_STRING) && $next->isGivenKind(T_NS_SEPARATOR)) {
                $index = $nextIndex;

                continue;
            }

            // $a-> or a-> (as in $b->a->c)
            if ($current->isGivenKind([T_STRING, T_VARIABLE]) && $next->isGivenKind(T_OBJECT_OPERATOR)) {
                $index = $tokens->getNextMeaningfulToken($nextIndex);
                $expectString = true;

                continue;
            }

            // $a[...], a[...] (as in $c->a[$b]), $a{...} or a{...} (as in $c->a{$b})
            if (
                $current->isGivenKind($expectString ? T_STRING : T_VARIABLE)
                && $next->equalsAny(['[', [CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN, '{']])
            ) {
                $index = $tokens->findBlockEnd(
                    $next->equals('[') ? Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE : Tokens::BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE,
                    $nextIndex
                );

                if ($index === $end) {
                    return true;
                }

                $index = $tokens->getNextMeaningfulToken($index);

                if (!$tokens[$index]->equalsAny([[T_OBJECT_OPERATOR, '->'], '[', [CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN, '{']])) {
                    return false;
                }

                $index = $tokens->getNextMeaningfulToken($index);
                $expectString = true;

                continue;
            }

            // $a(...) or $a->b(...)
            if ($strict && $current->isGivenKind([T_STRING, T_VARIABLE]) && $next->equals('(')) {
                return false;
            }

            // {...} (as in $a->{$b})
            if ($expectString && $current->isGivenKind(CT::T_DYNAMIC_PROP_BRACE_OPEN)) {
                $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_DYNAMIC_PROP_BRACE, $index);
                if ($index === $end) {
                    return true;
                }

                $index = $tokens->getNextMeaningfulToken($index);

                if (!$tokens[$index]->isGivenKind(T_OBJECT_OPERATOR)) {
                    return false;
                }

                $index = $tokens->getNextMeaningfulToken($index);
                $expectString = true;

                continue;
            }

            break;
        }

        return !$this->isConstant($tokens, $start, $end);
    }
private function isVariable ( Tokens $ tokens , $ start , $ end , $ strict ) { $ tokenAnalyzer = new TokensAnalyzer ( $ tokens ) ; if ( $ start === $ end ) { return $ tokens [ $ start ] -> isGivenKind ( T_VARIABLE ) ; } if ( $ strict ) { if ( $ tokens [ $ start ] -> equals ( '(' ) ) { return false ; } for ( $ index = $ start ; $ index <= $ end ; ++ $ index ) { if ( $ tokens [ $ index ] -> isCast ( ) || $ tokens [ $ index ] -> isGivenKind ( T_INSTANCEOF ) || $ tokens [ $ index ] -> equals ( '!' ) || $ tokenAnalyzer -> isBinaryOperator ( $ index ) ) { return false ; } } } $ index = $ start ; // handle multiple braces around statement ((($a === 1))) while ( $ tokens [ $ index ] -> equals ( '(' ) && $ tokens -> findBlockEnd ( Tokens :: BLOCK_TYPE_PARENTHESIS_BRACE , $ index ) === $ end ) { $ index = $ tokens -> getNextMeaningfulToken ( $ index ) ; $ end = $ tokens -> getPrevMeaningfulToken ( $ end ) ; } $ expectString = false ; while ( $ index <= $ end ) { $ current = $ tokens [ $ index ] ; if ( $ current -> isComment ( ) || $ current -> isWhitespace ( ) || $ tokens -> isEmptyAt ( $ index ) ) { ++ $ index ; continue ; } // check if this is the last token if ( $ index === $ end ) { return $ current -> isGivenKind ( $ expectString ? T_STRING : T_VARIABLE ) ; } if ( $ current -> isGivenKind ( [ T_LIST , CT :: T_DESTRUCTURING_SQUARE_BRACE_OPEN , CT :: T_DESTRUCTURING_SQUARE_BRACE_CLOSE ] ) ) { return false ; } $ nextIndex = $ tokens -> getNextMeaningfulToken ( $ index ) ; $ next = $ tokens [ $ nextIndex ] ; // self:: or ClassName:: if ( $ current -> isGivenKind ( T_STRING ) && $ next -> isGivenKind ( T_DOUBLE_COLON ) ) { $ index = $ tokens -> getNextMeaningfulToken ( $ nextIndex ) ; continue ; } // \ClassName if ( $ current -> isGivenKind ( T_NS_SEPARATOR ) && $ next -> isGivenKind ( T_STRING ) ) { $ index = $ nextIndex ; continue ; } // ClassName\ if ( $ current -> isGivenKind ( T_STRING ) && $ next -> isGivenKind ( T_NS_SEPARATOR ) ) { $ index = $ nextIndex ; continue ; } // $a-> or a-> (as in $b->a->c) if ( $ current -> isGivenKind ( [ T_STRING , T_VARIABLE ] ) && $ next -> isGivenKind ( T_OBJECT_OPERATOR ) ) { $ index = $ tokens -> getNextMeaningfulToken ( $ nextIndex ) ; $ expectString = true ; continue ; } // $a[...], a[...] (as in $c->a[$b]), $a{...} or a{...} (as in $c->a{$b}) if ( $ current -> isGivenKind ( $ expectString ? T_STRING : T_VARIABLE ) && $ next -> equalsAny ( [ '[' , [ CT :: T_ARRAY_INDEX_CURLY_BRACE_OPEN , '{' ] ] ) ) { $ index = $ tokens -> findBlockEnd ( $ next -> equals ( '[' ) ? Tokens :: BLOCK_TYPE_INDEX_SQUARE_BRACE : Tokens :: BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE , $ nextIndex ) ; if ( $ index === $ end ) { return true ; } $ index = $ tokens -> getNextMeaningfulToken ( $ index ) ; if ( ! $ tokens [ $ index ] -> equalsAny ( [ [ T_OBJECT_OPERATOR , '->' ] , '[' , [ CT :: T_ARRAY_INDEX_CURLY_BRACE_OPEN , '{' ] ] ) ) { return false ; } $ index = $ tokens -> getNextMeaningfulToken ( $ index ) ; $ expectString = true ; continue ; } // $a(...) or $a->b(...) if ( $ strict && $ current -> isGivenKind ( [ T_STRING , T_VARIABLE ] ) && $ next -> equals ( '(' ) ) { return false ; } // {...} (as in $a->{$b}) if ( $ expectString && $ current -> isGivenKind ( CT :: T_DYNAMIC_PROP_BRACE_OPEN ) ) { $ index = $ tokens -> findBlockEnd ( Tokens :: BLOCK_TYPE_DYNAMIC_PROP_BRACE , $ index ) ; if ( $ index === $ end ) { return true ; } $ index = $ tokens -> getNextMeaningfulToken ( $ index ) ; if ( ! $ tokens [ $ index ] -> isGivenKind ( T_OBJECT_OPERATOR ) ) { return false ; } $ index = $ tokens -> getNextMeaningfulToken ( $ index ) ; $ expectString = true ; continue ; } break ; } return ! $ this -> isConstant ( $ tokens , $ start , $ end ) ; }
Checks whether the tokens between the given start and end describe a
variable.

@param Tokens $tokens The token list
@param int    $start  The first index of the possible variable
@param int    $end    The last index of the possible variable
@param bool   $strict Enable strict variable detection

@return bool Whether the tokens describe a variable
Checks whether the tokens between the given start and end describe a variable .