private function fixElse(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(';') || $tokens[$tokens->getPrevMeaningfulToken($previous)]->equals('{')) {
return;
}
$candidateIndex = $tokens->getPrevTokenOfKind($previous, array(';', array(T_CLOSE_TAG), array(T_IF), array(T_BREAK), array(T_CONTINUE), array(T_EXIT), array(T_GOTO), array(T_RETURN), array(T_THROW)));
if (null === $candidateIndex || $tokens[$candidateIndex]->equalsAny(array(';', array(T_CLOSE_TAG), array(T_IF))) || $this->isInConditional($tokens, $candidateIndex, $previousBlockStart)) {
return;
}
// implicit continue, i.e. delete candidate
} while (!$tokens[$previousBlockStart]->isGivenKind(T_IF));
// if we made it to here the 'else' can be removed
$this->clearElse($tokens, $index);
}