PhpCsFixer\Tokenizer\Tokens::ensureWhitespaceAtIndex PHP Method

ensureWhitespaceAtIndex() public method

If there is a whitespace then it's content will be modified. If not - the new Token will be added.
public ensureWhitespaceAtIndex ( integer $index, integer $indexOffset, string $whitespace ) : boolean
$index integer index
$indexOffset integer index offset for Token insertion
$whitespace string whitespace to set
return boolean if new Token was added
    public function ensureWhitespaceAtIndex($index, $indexOffset, $whitespace)
    {
        $removeLastCommentLine = function (Token $token, $indexOffset) {
            // because comments tokens are greedy and may consume single \n if we are putting whitespace after it let trim that \n
            if (1 === $indexOffset && $token->isComment()) {
                $token->setContent(preg_replace("#\r\n\$|\n\$#", '', $token->getContent(), 1));
            }
        };
        if ($this[$index]->isWhitespace()) {
            $removeLastCommentLine($this[$index - 1], $indexOffset);
            $this->overrideAt($index, array(T_WHITESPACE, $whitespace));
            return false;
        }
        $removeLastCommentLine($this[$index], $indexOffset);
        $this->insertAt($index + $indexOffset, array(new Token(array(T_WHITESPACE, $whitespace))));
        return true;
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $count = $tokens->count();
     if ($count && !$tokens[$count - 1]->isGivenKind(array(T_INLINE_HTML, T_CLOSE_TAG, T_OPEN_TAG))) {
         $tokens->ensureWhitespaceAtIndex($count - 1, 1, $this->whitespacesConfig->getLineEnding());
     }
 }
All Usage Examples Of PhpCsFixer\Tokenizer\Tokens::ensureWhitespaceAtIndex