PhpCsFixer\Tokenizer\Tokens::insertAt PHP Method

insertAt() public method

Insert instances of Token inside collection.
public insertAt ( integer $index, Tokens | PhpCsFixer\Tokenizer\Token[] | PhpCsFixer\Tokenizer\Token $items )
$index integer start inserting index
$items Tokens | PhpCsFixer\Tokenizer\Token[] | PhpCsFixer\Tokenizer\Token instances of Token to insert
    public function insertAt($index, $items)
    {
        $items = is_array($items) || $items instanceof self ? $items : array($items);
        $itemsCnt = count($items);
        if (0 === $itemsCnt) {
            return;
        }
        $oldSize = count($this);
        $this->changed = true;
        $this->setSize($oldSize + $itemsCnt);
        for ($i = $oldSize + $itemsCnt - 1; $i >= $index; --$i) {
            $this[$i] = isset($this[$i - $itemsCnt]) ? $this[$i - $itemsCnt] : new Token('');
        }
        for ($i = 0; $i < $itemsCnt; ++$i) {
            if ('' === $items[$i]->getContent()) {
                throw new \InvalidArgumentException('Must not add empty token to collection.');
            }
            $this[$i + $index] = $items[$i];
        }
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         $token = $tokens[$index];
         if ($token->equals('.')) {
             if (!$tokens[$index + 1]->isWhitespace()) {
                 $tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
             }
             if (!$tokens[$index - 1]->isWhitespace()) {
                 $tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
             }
         }
     }
 }
All Usage Examples Of PhpCsFixer\Tokenizer\Tokens::insertAt