/** * Clear comment token, but preserve trailing linebreak if there is any. * * @param Tokens $tokens * @param int $index T_COMMENT index * * @deprecated Will be removed in the 2.0 */ private function clearCommentToken(Tokens $tokens, $index) { if ("\n" !== substr($tokens[$index]->getContent(), -1, 1)) { $tokens->clearTokenAndMergeSurroundingWhitespace($index); return; } // if previous not-cleared token is whitespace; // append line break to content $previous = $tokens->getNonEmptySibling($index, -1); if ($tokens[$previous]->isWhitespace()) { $tokens[$previous]->setContent($tokens[$previous]->getContent() . "\n"); $tokens->clearTokenAndMergeSurroundingWhitespace($index); return; } // elseif the next not-cleared token is whitespace; // prepend with line break $next = $tokens->getNonEmptySibling($index, 1); if (null !== $next && $tokens[$next]->isWhitespace()) { $tokens[$next]->setContent("\n" . $tokens[$next]->getContent()); $tokens->clearTokenAndMergeSurroundingWhitespace($index); return; } // else // override with whitespace token linebreak $tokens->overrideAt($index, array(T_WHITESPACE, "\n", $tokens[$index]->getLine())); }
/** * @param Tokens $tokens * @param int $start Token index of the opening brace token of the function * @param int $end Token index of the closing brace token of the function */ private function fixFunction(Tokens $tokens, $start, $end) { for ($index = $end; $index > $start; --$index) { if (!$tokens[$index]->isGivenKind(T_RETURN)) { continue; } $nextAt = $tokens->getNextMeaningfulToken($index); if (!$tokens[$nextAt]->equals(';')) { continue; } if ($tokens->getNextMeaningfulToken($nextAt) !== $end) { continue; } $previous = $tokens->getPrevMeaningfulToken($index); if ($tokens[$previous]->equalsAny(array(array(T_ELSE), ')'))) { continue; } $tokens->clearTokenAndMergeSurroundingWhitespace($index); $tokens->clearTokenAndMergeSurroundingWhitespace($nextAt); } }
/** * @param Tokens $tokens * @param int|false $callNSIndex * @param int $callIndex * @param int $openIndex * @param int $closeIndex */ private function removeFunctionCall(Tokens $tokens, $callNSIndex, $callIndex, $openIndex, $closeIndex) { $tokens->clearTokenAndMergeSurroundingWhitespace($callIndex); if (false !== $callNSIndex) { $tokens->clearTokenAndMergeSurroundingWhitespace($callNSIndex); } $tokens->clearTokenAndMergeSurroundingWhitespace($openIndex); $tokens->clearTokenAndMergeSurroundingWhitespace($closeIndex); }
/** * Fix semicolon after closing curly brace if needed. * * Test for the following cases * - just '{' '}' block (following open tag or ';') * - if, else, elseif * - interface, trait, class (but not anonymous) * - catch, finally (but not try) * - for, foreach, while (but not 'do - while') * - switch * - function (declaration, but not lambda) * - declare (with '{' '}') * - namespace (with '{' '}') * * @param Tokens $tokens * @param int $index Semicolon index * @param int $curlyCloseIndex */ private function fixSemicolonAfterCurlyBraceClose(Tokens $tokens, $index, $curlyCloseIndex) { static $beforeCurlyOpeningKinds = null; if (null === $beforeCurlyOpeningKinds) { $beforeCurlyOpeningKinds = array(T_ELSE, T_NAMESPACE, T_OPEN_TAG); if (defined('T_FINALLY')) { $beforeCurlyOpeningKinds[] = T_FINALLY; } } $curlyOpeningIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $curlyCloseIndex, false); $beforeCurlyOpening = $tokens->getPrevMeaningfulToken($curlyOpeningIndex); if ($tokens[$beforeCurlyOpening]->isGivenKind($beforeCurlyOpeningKinds) || $tokens[$beforeCurlyOpening]->equalsAny(array(';', '{', '}'))) { $tokens->clearTokenAndMergeSurroundingWhitespace($index); return; } // check for namespaces and class, interface and trait definitions if ($tokens[$beforeCurlyOpening]->isGivenKind(T_STRING)) { $classyTest = $tokens->getPrevMeaningfulToken($beforeCurlyOpening); while ($tokens[$classyTest]->equals(',') || $tokens[$classyTest]->isGivenKind(array(T_STRING, T_NS_SEPARATOR, T_EXTENDS, T_IMPLEMENTS))) { $classyTest = $tokens->getPrevMeaningfulToken($classyTest); } if ($tokens[$classyTest]->isGivenKind(T_NAMESPACE) || $tokens[$classyTest]->isClassy() && !$this->isAnonymousClass($tokens, $classyTest)) { $tokens->clearTokenAndMergeSurroundingWhitespace($index); } return; } // early return check, below only control structures with conditions are fixed if (!$tokens[$beforeCurlyOpening]->equals(')')) { return; } $openingBrace = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $beforeCurlyOpening, false); $beforeOpeningBrace = $tokens->getPrevMeaningfulToken($openingBrace); if ($tokens[$beforeOpeningBrace]->isGivenKind(array(T_IF, T_ELSEIF, T_FOR, T_FOREACH, T_WHILE, T_SWITCH, T_CATCH, T_DECLARE))) { $tokens->clearTokenAndMergeSurroundingWhitespace($index); return; } // check for function definition if ($tokens[$beforeOpeningBrace]->isGivenKind(T_STRING)) { $beforeString = $tokens->getPrevMeaningfulToken($beforeOpeningBrace); if ($tokens[$beforeString]->isGivenKind(T_FUNCTION)) { $tokens->clearTokenAndMergeSurroundingWhitespace($index); // implicit return } } }
/** * @param Tokens $tokens * @param int $index index of T_ELSE */ private function clearElse(Tokens $tokens, $index) { $tokens->clearTokenAndMergeSurroundingWhitespace($index); // clear T_ELSE and the '{' '}' if there are any $next = $tokens->getNextMeaningfulToken($index); if (!$tokens[$next]->equals('{')) { return; } $tokens->clearTokenAndMergeSurroundingWhitespace($tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $next)); $tokens->clearTokenAndMergeSurroundingWhitespace($next); }
private function removeUseDeclaration(Tokens $tokens, array $useDeclaration) { for ($index = $useDeclaration['end'] - 1; $index >= $useDeclaration['start']; --$index) { $tokens->clearTokenAndMergeSurroundingWhitespace($index); } if ($tokens[$useDeclaration['end']]->equals(';')) { $tokens[$useDeclaration['end']]->clear(); } $prevToken = $tokens[$useDeclaration['start'] - 1]; if ($prevToken->isWhitespace()) { $prevToken->setContent(rtrim($prevToken->getContent(), " \t")); } if (!isset($tokens[$useDeclaration['end'] + 1])) { return; } $nextIndex = $useDeclaration['end'] + 1; $nextToken = $tokens[$nextIndex]; if ($nextToken->isWhitespace()) { $content = ltrim($nextToken->getContent(), " \t"); if ($content && "\n" === $content[0]) { $content = substr($content, 1); } $nextToken->setContent($content); } if ($prevToken->isWhitespace() && $nextToken->isWhitespace()) { $tokens->overrideAt($nextIndex, array(T_WHITESPACE, $prevToken->getContent() . $nextToken->getContent(), $prevToken->getLine())); $prevToken->clear(); } }
/** * @param Tokens $tokens * @param int $offset * @param int[] $indices */ private function clearOffsetTokens(Tokens $tokens, $offset, array $indices) { foreach ($indices as $index) { $tokens->clearTokenAndMergeSurroundingWhitespace($index + $offset); } }