本文整理汇总了PHP中PhpCsFixer\Tokenizer\Tokens::clearTokenAndMergeSurroundingWhitespace方法的典型用法代码示例。如果您正苦于以下问题:PHP Tokens::clearTokenAndMergeSurroundingWhitespace方法的具体用法?PHP Tokens::clearTokenAndMergeSurroundingWhitespace怎么用?PHP Tokens::clearTokenAndMergeSurroundingWhitespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpCsFixer\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::clearTokenAndMergeSurroundingWhitespace方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
// Checks if specific statements are set and uses them in this case.
$loops = array_intersect_key(self::$loops, array_flip($this->controlStatements));
foreach ($tokens as $index => $token) {
if (!$token->equals('(')) {
continue;
}
$blockStartIndex = $index;
$index = $tokens->getPrevMeaningfulToken($index);
$token = $tokens[$index];
foreach ($loops as $loop) {
if (!$token->isGivenKind($loop['lookupTokens'])) {
continue;
}
$blockEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $blockStartIndex);
$blockEndNextIndex = $tokens->getNextMeaningfulToken($blockEndIndex);
if (!$tokens[$blockEndNextIndex]->equalsAny($loop['neededSuccessors'])) {
continue;
}
if ($tokens[$blockStartIndex - 1]->isWhitespace() || $tokens[$blockStartIndex - 1]->isComment()) {
$tokens->clearTokenAndMergeSurroundingWhitespace($blockStartIndex);
} else {
// Adds a space to prevent broken code like `return2`.
$tokens->overrideAt($blockStartIndex, array(T_WHITESPACE, ' '));
}
$tokens->clearTokenAndMergeSurroundingWhitespace($blockEndIndex);
}
}
}
示例2: fixFunction
/**
* @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;
}
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
$tokens->clearTokenAndMergeSurroundingWhitespace($nextAt);
}
}
示例3: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_DOC_COMMENT)) {
continue;
}
if (preg_match('#^/\\*\\*[\\s\\*]*\\*/$#', $token->getContent())) {
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
}
}
}
示例4: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_ARRAY)) {
continue;
}
$openIndex = $tokens->getNextTokenOfKind($index, array('('));
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);
$tokens->overrideAt($openIndex, array(CT::T_ARRAY_SQUARE_BRACE_OPEN, '['));
$tokens->overrideAt($closeIndex, array(CT::T_ARRAY_SQUARE_BRACE_CLOSE, ']'));
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
}
}
示例5: fixComment
/**
* @param Tokens $tokens
* @param int $index T_COMMENT index
*/
private function fixComment(Tokens $tokens, $index)
{
$content = $tokens[$index]->getContent();
// single line comment starting with '#'
if ('#' === $content[0]) {
if (preg_match('|^#\\s*$|', $content)) {
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
}
return;
}
// single line comment starting with '//'
if ('/' === $content[1]) {
if (preg_match('|^//\\s*$|', $content)) {
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
}
return;
}
// comment starting with '/*' and ending with '*/' (but not a PHPDoc)
if (preg_match('|^/\\*\\s*\\*/$|', $content)) {
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
}
}
示例6: removeFunctionCall
/**
* @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);
}
示例7: removeUseDeclaration
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");
$content = preg_replace("#^\r\n|^\n#", '', $content, 1);
$nextToken->setContent($content);
}
if ($prevToken->isWhitespace() && $nextToken->isWhitespace()) {
$tokens->overrideAt($nextIndex, array(T_WHITESPACE, $prevToken->getContent() . $nextToken->getContent()));
$prevToken->clear();
}
}
示例8: clearOffsetTokens
/**
* @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);
}
}
示例9: clearElse
/**
* @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);
}
示例10: fixSemicolonAfterCurlyBraceClose
/**
* 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
}
}
}
示例11: fixToShortArraySyntax
/**
* @param Tokens $tokens
* @param int $index
*/
private function fixToShortArraySyntax(Tokens $tokens, $index)
{
$openIndex = $tokens->getNextTokenOfKind($index, array('('));
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);
$tokens->overrideAt($openIndex, array(CT::T_ARRAY_SQUARE_BRACE_OPEN, '['));
$tokens->overrideAt($closeIndex, array(CT::T_ARRAY_SQUARE_BRACE_CLOSE, ']'));
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
}