src/AbstractNoUselessElseFixer.php
protected function isSuperfluousElse(Tokens $tokens, $index)
    {
        $previousBlockStart = $index;
        do {
            // Check if all 'if', 'else if ' and 'elseif' blocks above this 'else' always end,
            // if so this 'else' is overcomplete.
            list($previousBlockStart, $previousBlockEnd) = $this->getPreviousBlock($tokens, $previousBlockStart);

            // short 'if' detection
            $previous = $previousBlockEnd;
            if ($tokens[$previous]->equals('}')) {
                $previous = $tokens->getPrevMeaningfulToken($previous);
            }

            if (
                !$tokens[$previous]->equals(';') ||                              // 'if' block doesn't end with semicolon, keep 'else'
                $tokens[$tokens->getPrevMeaningfulToken($previous)]->equals('{') // empty 'if' block, keep 'else'
            ) {
                return false;
            }

            $candidateIndex = $tokens->getPrevTokenOfKind(
                $previous,
                [
                    ';',
                    [T_BREAK],
                    [T_CLOSE_TAG],
                    [T_CONTINUE],
                    [T_EXIT],
                    [T_GOTO],
                    [T_IF],
                    [T_RETURN],
                    [T_THROW],
                ]
            );

            if (
                null === $candidateIndex
                || $tokens[$candidateIndex]->equalsAny([';', [T_CLOSE_TAG], [T_IF]])
                || $this->isInConditional($tokens, $candidateIndex, $previousBlockStart)
                || $this->isInConditionWithoutBraces($tokens, $candidateIndex, $previousBlockStart)
            ) {
                return false;
            }

            // implicit continue, i.e. delete candidate
        } while (!$tokens[$previousBlockStart]->isGivenKind(T_IF));

        return true;
    }
protected function isSuperfluousElse(Tokens $tokens, $index)
    {
        $previousBlockStart = $index;
        do {
            // Check if all 'if', 'else if ' and 'elseif' blocks above this 'else' always end,
            // if so this 'else' is overcomplete.
            list($previousBlockStart, $previousBlockEnd) = $this->getPreviousBlock($tokens, $previousBlockStart);

            // short 'if' detection
            $previous = $previousBlockEnd;
            if ($tokens[$previous]->equals('}')) {
                $previous = $tokens->getPrevMeaningfulToken($previous);
            }

            if (
                !$tokens[$previous]->equals(';') ||                              // 'if' block doesn't end with semicolon, keep 'else'
                $tokens[$tokens->getPrevMeaningfulToken($previous)]->equals('{') // empty 'if' block, keep 'else'
            ) {
                return false;
            }

            $candidateIndex = $tokens->getPrevTokenOfKind(
                $previous,
                [
                    ';',
                    [T_BREAK],
                    [T_CLOSE_TAG],
                    [T_CONTINUE],
                    [T_EXIT],
                    [T_GOTO],
                    [T_IF],
                    [T_RETURN],
                    [T_THROW],
                ]
            );

            if (
                null === $candidateIndex
                || $tokens[$candidateIndex]->equalsAny([';', [T_CLOSE_TAG], [T_IF]])
                || $this->isInConditional($tokens, $candidateIndex, $previousBlockStart)
                || $this->isInConditionWithoutBraces($tokens, $candidateIndex, $previousBlockStart)
            ) {
                return false;
            }

            // implicit continue, i.e. delete candidate
        } while (!$tokens[$previousBlockStart]->isGivenKind(T_IF));

        return true;
    }
protected function isSuperfluousElse ( Tokens $ tokens , $ index ) { $ previousBlockStart = $ index ; do { // Check if all 'if', 'else if ' and 'elseif' blocks above this 'else' always end, // if so this 'else' is overcomplete. list ( $ previousBlockStart , $ previousBlockEnd ) = $ this -> getPreviousBlock ( $ tokens , $ previousBlockStart ) ; // short 'if' detection $ previous = $ previousBlockEnd ; if ( $ tokens [ $ previous ] -> equals ( '}' ) ) { $ previous = $ tokens -> getPrevMeaningfulToken ( $ previous ) ; } if ( ! $ tokens [ $ previous ] -> equals ( ';' ) || // 'if' block doesn't end with semicolon, keep 'else' $ tokens [ $ tokens -> getPrevMeaningfulToken ( $ previous ) ] -> equals ( '{' ) // empty 'if' block, keep 'else' ) { return false ; } $ candidateIndex = $ tokens -> getPrevTokenOfKind ( $ previous , [ ';' , [ T_BREAK ] , [ T_CLOSE_TAG ] , [ T_CONTINUE ] , [ T_EXIT ] , [ T_GOTO ] , [ T_IF ] , [ T_RETURN ] , [ T_THROW ] , ] ) ; if ( null === $ candidateIndex || $ tokens [ $ candidateIndex ] -> equalsAny ( [ ';' , [ T_CLOSE_TAG ] , [ T_IF ] ] ) || $ this -> isInConditional ( $ tokens , $ candidateIndex , $ previousBlockStart ) || $ this -> isInConditionWithoutBraces ( $ tokens , $ candidateIndex , $ previousBlockStart ) ) { return false ; } // implicit continue, i.e. delete candidate } while ( ! $ tokens [ $ previousBlockStart ] -> isGivenKind ( T_IF ) ) ; return true ; }
@param Tokens $tokens
@param int    $index

@return bool
@param Tokens $tokens @param int $index