本文整理汇总了PHP中PhpCsFixer\Tokenizer\Tokens::insertAt方法的典型用法代码示例。如果您正苦于以下问题:PHP Tokens::insertAt方法的具体用法?PHP Tokens::insertAt怎么用?PHP Tokens::insertAt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpCsFixer\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::insertAt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fix
/**
* {@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, ' ')));
}
}
}
}
示例2: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];
if ($tokensAnalyzer->isUnaryPredecessorOperator($index) && $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, ' ')));
}
}
}
}
示例3: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
if (!$tokensAnalyzer->isBinaryOperator($index)) {
continue;
}
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, ' ')));
}
}
}
示例4: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$lineEnding = $this->whitespacesConfig->getLineEnding();
for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_RETURN)) {
continue;
}
$prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)];
if (!$prevNonWhitespaceToken->equalsAny(array(';', '}'))) {
continue;
}
$prevToken = $tokens[$index - 1];
if ($prevToken->isWhitespace()) {
$parts = explode("\n", $prevToken->getContent());
$countParts = count($parts);
if (1 === $countParts) {
$prevToken->setContent(rtrim($prevToken->getContent(), " \t") . $lineEnding . $lineEnding);
} elseif (count($parts) <= 2) {
$prevToken->setContent($lineEnding . $prevToken->getContent());
}
} else {
$tokens->insertAt($index, new Token(array(T_WHITESPACE, $lineEnding . $lineEnding)));
++$index;
++$limit;
}
}
}
示例5: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_FUNCTION)) {
continue;
}
$startParenthesisIndex = $tokens->getNextTokenOfKind($index, array('('));
$endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startParenthesisIndex);
for ($iter = $endParenthesisIndex - 1; $iter > $startParenthesisIndex; --$iter) {
if (!$tokens[$iter]->isGivenKind(T_VARIABLE)) {
continue;
}
// skip ... before $variable for variadic parameter
if (defined('T_ELLIPSIS')) {
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter);
if ($tokens[$prevNonWhitespaceIndex]->isGivenKind(T_ELLIPSIS)) {
$iter = $prevNonWhitespaceIndex;
}
}
// skip & before $variable for parameter passed by reference
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter);
if ($tokens[$prevNonWhitespaceIndex]->equals('&')) {
$iter = $prevNonWhitespaceIndex;
}
if (!$tokens[$iter - 1]->equalsAny(array(array(T_WHITESPACE), array(T_COMMENT), array(T_DOC_COMMENT), '(', ','))) {
$tokens->insertAt($iter, new Token(array(T_WHITESPACE, ' ')));
}
}
}
}
示例6: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 3; $index > 0; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_NEW)) {
continue;
}
$nextIndex = $tokens->getNextTokenOfKind($index, array(':', ';', ',', '(', ')', '[', ']', array(T_CLOSE_TAG), array(CT_ARRAY_SQUARE_BRACE_OPEN), array(CT_ARRAY_SQUARE_BRACE_CLOSE), array(CT_BRACE_CLASS_INSTANTIATION_OPEN), array(CT_BRACE_CLASS_INSTANTIATION_CLOSE)));
$nextToken = $tokens[$nextIndex];
// entrance into array index syntax - need to look for exit
while ($nextToken->equals('[')) {
$nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $nextIndex) + 1;
$nextToken = $tokens[$nextIndex];
}
// new statement has a gap in it - advance to the next token
if ($nextToken->isWhitespace()) {
$nextIndex = $tokens->getNextNonWhitespace($nextIndex);
$nextToken = $tokens[$nextIndex];
}
// new statement with () - nothing to do
if ($nextToken->equals('(')) {
continue;
}
$meaningBeforeNextIndex = $tokens->getPrevMeaningfulToken($nextIndex);
$tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')')));
}
}
示例7: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
foreach ($tokensAnalyzer->getImportUseIndexes() as $index) {
$indent = '';
// if previous line ends with comment and current line starts with whitespace, use current indent
if ($tokens[$index - 1]->isWhitespace(" \t") && $tokens[$index - 2]->isGivenKind(T_COMMENT)) {
$indent = $tokens[$index - 1]->getContent();
} elseif ($tokens[$index - 1]->isWhitespace()) {
$indent = Utils::calculateTrailingWhitespaceIndent($tokens[$index - 1]);
}
$newline = "\n";
// Handle insert index for inline T_COMMENT with whitespace after semicolon
$semicolonIndex = $tokens->getNextTokenOfKind($index, array(';', '{'));
$insertIndex = $semicolonIndex + 1;
if ($tokens[$insertIndex]->isWhitespace(" \t") && $tokens[$insertIndex + 1]->isComment()) {
++$insertIndex;
}
// Increment insert index for inline T_COMMENT or T_DOC_COMMENT
if ($tokens[$insertIndex]->isComment()) {
++$insertIndex;
}
$afterSemicolon = $tokens->getNextMeaningfulToken($semicolonIndex);
if (!$tokens[$afterSemicolon]->isGivenKind(T_USE)) {
$newline .= "\n";
}
if ($tokens[$insertIndex]->isWhitespace()) {
$nextToken = $tokens[$insertIndex];
$nextToken->setContent($newline . $indent . ltrim($nextToken->getContent()));
} elseif ($newline && $indent) {
$tokens->insertAt($insertIndex, new Token(array(T_WHITESPACE, $newline . $indent)));
}
}
}
示例8: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$lineEnding = $this->whitespacesConfig->getLineEnding();
// ignore files with short open tag and ignore non-monolithic files
if (!$tokens[0]->isGivenKind(T_OPEN_TAG) || !$tokens->isMonolithicPhp()) {
return;
}
$newlineFound = false;
/** @var Token $token */
foreach ($tokens as $token) {
if ($token->isWhitespace() && false !== strpos($token->getContent(), "\n")) {
$newlineFound = true;
break;
}
}
// ignore one-line files
if (!$newlineFound) {
return;
}
$token = $tokens[0];
if (false === strpos($token->getContent(), "\n")) {
$token->setContent(rtrim($token->getContent()) . $lineEnding);
}
if (!$tokens[1]->isWhitespace() && false === strpos($tokens[1]->getContent(), "\n")) {
$tokens->insertAt(1, new Token(array(T_WHITESPACE, $lineEnding)));
}
}
示例9: clearIncludies
private function clearIncludies(Tokens $tokens, array $includies)
{
foreach (array_reverse($includies) as $includy) {
if ($includy['end']) {
$tokens->removeLeadingWhitespace($includy['end']);
}
$braces = $includy['braces'];
if ($braces) {
$nextToken = $tokens[$tokens->getNextMeaningfulToken($braces['close'])];
if ($nextToken->equals(';')) {
$tokens->removeLeadingWhitespace($braces['open']);
$tokens->removeTrailingWhitespace($braces['open']);
$tokens->removeLeadingWhitespace($braces['close']);
$tokens->removeTrailingWhitespace($braces['close']);
$tokens[$braces['open']] = new Token(array(T_WHITESPACE, ' '));
$tokens[$braces['close']]->clear();
}
}
$nextIndex = $includy['begin'] + 1;
$nextToken = $tokens[$nextIndex];
while ($nextToken->isEmpty()) {
$nextToken = $tokens[++$nextIndex];
}
if ($nextToken->isWhitespace()) {
$nextToken->setContent(' ');
} elseif ($braces || $tokens[$nextIndex]->isGivenKind(array(T_VARIABLE, T_CONSTANT_ENCAPSED_STRING, T_COMMENT))) {
$tokens->insertAt($includy['begin'] + 1, new Token(array(T_WHITESPACE, ' ')));
}
}
}
示例10: ensureWhitespaceExistence
private function ensureWhitespaceExistence(Tokens $tokens, $index, $after)
{
$indexChange = $after ? 0 : 1;
$token = $tokens[$index];
if ($token->isWhitespace()) {
return;
}
$tokens->insertAt($index + $indexChange, new Token(array(T_WHITESPACE, ' ')));
}
示例11: fixShortCastToBoolCast
private function fixShortCastToBoolCast(Tokens $tokens, $start, $end)
{
for (; $start <= $end; ++$start) {
if (!$tokens[$start]->isComment() && !($tokens[$start]->isWhitespace() && $tokens[$start - 1]->isComment())) {
$tokens[$start]->clear();
}
}
$tokens->insertAt($start, new Token(array(T_BOOL_CAST, '(bool)')));
}
示例12: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$ending = $this->whitespacesConfig->getLineEnding();
$tokensAnalyzer = new TokensAnalyzer($tokens);
foreach ($tokensAnalyzer->getImportUseIndexes() as $index) {
$indent = '';
// if previous line ends with comment and current line starts with whitespace, use current indent
if ($tokens[$index - 1]->isWhitespace(" \t") && $tokens[$index - 2]->isGivenKind(T_COMMENT)) {
$indent = $tokens[$index - 1]->getContent();
} elseif ($tokens[$index - 1]->isWhitespace()) {
$indent = Utils::calculateTrailingWhitespaceIndent($tokens[$index - 1]);
}
$semicolonIndex = $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)));
// Handle insert index for inline T_COMMENT with whitespace after semicolon
$insertIndex = $semicolonIndex;
if ($tokens[$semicolonIndex]->isGivenKind(T_CLOSE_TAG)) {
if ($tokens[$insertIndex - 1]->isWhitespace()) {
--$insertIndex;
}
$tokens->insertAt($insertIndex, new Token(';'));
}
if ($semicolonIndex === count($tokens) - 1) {
$tokens->insertAt($insertIndex + 1, new Token(array(T_WHITESPACE, $ending . $ending . $indent)));
} else {
$newline = $ending;
$tokens[$semicolonIndex]->isGivenKind(T_CLOSE_TAG) ? --$insertIndex : ++$insertIndex;
if ($tokens[$insertIndex]->isWhitespace(" \t") && $tokens[$insertIndex + 1]->isComment()) {
++$insertIndex;
}
// Increment insert index for inline T_COMMENT or T_DOC_COMMENT
if ($tokens[$insertIndex]->isComment()) {
++$insertIndex;
}
$afterSemicolon = $tokens->getNextMeaningfulToken($semicolonIndex);
if (null === $afterSemicolon || !$tokens[$afterSemicolon]->isGivenKind(T_USE)) {
$newline .= $ending;
}
if ($tokens[$insertIndex]->isWhitespace()) {
$nextToken = $tokens[$insertIndex];
$nextMeaningfulAfterUseIndex = $tokens->getNextMeaningfulToken($insertIndex);
if (null !== $nextMeaningfulAfterUseIndex && $tokens[$nextMeaningfulAfterUseIndex]->isGivenKind(T_USE)) {
if (substr_count($nextToken->getContent(), "\n") < 2) {
$nextToken->setContent($newline . $indent . ltrim($nextToken->getContent()));
}
} else {
$nextToken->setContent($newline . $indent . ltrim($nextToken->getContent()));
}
} else {
$tokens->insertAt($insertIndex, new Token(array(T_WHITESPACE, $newline . $indent)));
}
}
}
}
示例13: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
if (!$tokensAnalyzer->isBinaryOperator($index)) {
continue;
}
// skip `declare(foo ==bar)`
$prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevMeaningfulIndex]->isGivenKind(T_STRING)) {
$prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($prevMeaningfulIndex);
if ($tokens[$prevMeaningfulIndex]->equals('(')) {
$prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($prevMeaningfulIndex);
if ($tokens[$prevMeaningfulIndex]->isGivenKind(T_DECLARE)) {
continue;
}
}
}
// fix white space after operator
if ($tokens[$index + 1]->isWhitespace()) {
$content = $tokens[$index + 1]->getContent();
if (' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getNextNonWhitespace($index + 1)]->isComment()) {
$tokens[$index + 1]->setContent(' ');
}
} else {
$tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
}
// fix white space before operator
if ($tokens[$index - 1]->isWhitespace()) {
$content = $tokens[$index - 1]->getContent();
if (' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment()) {
$tokens[$index - 1]->setContent(' ');
}
} else {
$tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
}
--$index;
// skip check for binary operator on the whitespace token that is fixed.
}
}
示例14: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 1; 0 <= $index; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
continue;
}
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index);
$tokens->overrideAt($index, '(');
$tokens->overrideAt($closeIndex, ')');
$tokens->insertAt($index, new Token(array(T_ARRAY, 'array')));
}
}
示例15: fixWhiteSpaceAroundOperator
private function fixWhiteSpaceAroundOperator(Tokens $tokens, $index)
{
// do not change the alignment of `=>` or `=`, see `(un)align_double_arrow`, `(un)align_equals`
$preserveAlignment = $tokens[$index]->isGivenKind(T_DOUBLE_ARROW) || $tokens[$index]->equals('=');
// fix white space after operator
if ($tokens[$index + 1]->isWhitespace()) {
$content = $tokens[$index + 1]->getContent();
if (!$preserveAlignment && ' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getNextNonWhitespace($index + 1)]->isComment()) {
$tokens[$index + 1]->setContent(' ');
}
} else {
$tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
}
// fix white space before operator
if ($tokens[$index - 1]->isWhitespace()) {
$content = $tokens[$index - 1]->getContent();
if (!$preserveAlignment && ' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment()) {
$tokens[$index - 1]->setContent(' ');
}
} else {
$tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
}
}