clearTokenAndMergeSurroundingWhitespace() public méthode

Clear token and merge surrounding whitespace tokens.
 /**
  * {@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);
         }
     }
 }
 /**
  * @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);
     }
 }
 /**
  * {@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);
         }
     }
 }
 /**
  * {@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);
     }
 }
Exemple #5
0
 /**
  * @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);
     }
 }
 /**
  * @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);
 }
 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();
     }
 }
 /**
  * @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);
     }
 }
 /**
  * @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);
 }
 /**
  * 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
  */
 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);
 }