PhpCsFixer\Tokenizer\Tokens::clearTokenAndMergeSurroundingWhitespace PHP Méthode

clearTokenAndMergeSurroundingWhitespace() public méthode

Clear token and merge surrounding whitespace tokens.
    public function clearTokenAndMergeSurroundingWhitespace($index)
    {
        $count = count($this);
        $this[$index]->clear();
        if ($index === $count - 1) {
            return;
        }
        $nextIndex = $this->getNonEmptySibling($index, 1);
        if (null === $nextIndex || !$this[$nextIndex]->isWhitespace()) {
            return;
        }
        $prevIndex = $this->getNonEmptySibling($index, -1);
        if ($this[$prevIndex]->isWhitespace()) {
            $this[$prevIndex]->setContent($this[$prevIndex]->getContent() . $this[$nextIndex]->getContent());
        } elseif ($this[$prevIndex + 1]->isEmpty()) {
            $this[$prevIndex + 1]->override(array(T_WHITESPACE, $this[$nextIndex]->getContent()));
        }
        $this[$nextIndex]->clear();
    }

Usage Example

 /**
  * {@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);
         }
     }
 }
All Usage Examples Of PhpCsFixer\Tokenizer\Tokens::clearTokenAndMergeSurroundingWhitespace