本文整理汇总了PHP中PhpCsFixer\Tokenizer\Tokens::getPrevNonWhitespace方法的典型用法代码示例。如果您正苦于以下问题:PHP Tokens::getPrevNonWhitespace方法的具体用法?PHP Tokens::getPrevNonWhitespace怎么用?PHP Tokens::getPrevNonWhitespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpCsFixer\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::getPrevNonWhitespace方法的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->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, ' ')));
}
}
}
}
示例2: fix
/**
* Replace all `else if` (T_ELSE T_IF) with `elseif` (T_ELSEIF).
*
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_ELSE)) {
continue;
}
$ifTokenIndex = $tokens->getNextMeaningfulToken($index);
$beforeIfTokenIndex = $tokens->getPrevNonWhitespace($ifTokenIndex);
// if next meaning token is not T_IF - continue searching, this is not the case for fixing
if (!$tokens[$ifTokenIndex]->isGivenKind(T_IF)) {
continue;
}
// now we have T_ELSE following by T_IF so we could fix this
// 1. clear whitespaces between T_ELSE and T_IF
$tokens[$index + 1]->clear();
// 2. change token from T_ELSE into T_ELSEIF
$tokens->overrideAt($index, array(T_ELSEIF, 'elseif'));
// 3. clear succeeding T_IF
$tokens[$ifTokenIndex]->clear();
// 4. clear extra whitespace after T_IF in T_COMMENT,T_WHITESPACE?,T_IF,T_WHITESPACE sequence
if ($tokens[$beforeIfTokenIndex]->isComment() && $tokens[$ifTokenIndex + 1]->isWhitespace()) {
$tokens[$ifTokenIndex + 1]->clear();
}
}
// handle `T_ELSE T_WHITESPACE T_IF` treated as single `T_ELSEIF` by HHVM
// see https://github.com/facebook/hhvm/issues/4796
if (defined('HHVM_VERSION')) {
foreach ($tokens->findGivenKind(T_ELSEIF) as $token) {
$token->setContent('elseif');
}
}
}
示例3: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->equalsAny(array('[', array(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)))) {
continue;
}
if (in_array('inside', $this->configuration, true)) {
if ($token->equals('[')) {
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $index);
} else {
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE, $index);
}
// remove space after opening `[` or `{`
$this->removeWhitespaceToken($tokens[$index + 1]);
// remove space before closing `]` or `}`
$this->removeWhitespaceToken($tokens[$endIndex - 1]);
}
if (in_array('outside', $this->configuration, true)) {
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($index);
if ($tokens[$prevNonWhitespaceIndex]->isComment()) {
continue;
}
$tokens->removeLeadingWhitespace($index);
}
}
}
示例4: fix
/**
* Replace all `else if` (T_ELSE T_IF) with `elseif` (T_ELSEIF).
*
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_ELSE)) {
continue;
}
$ifTokenIndex = $tokens->getNextMeaningfulToken($index);
$beforeIfTokenIndex = $tokens->getPrevNonWhitespace($ifTokenIndex);
// if next meaning token is not T_IF - continue searching, this is not the case for fixing
if (!$tokens[$ifTokenIndex]->isGivenKind(T_IF)) {
continue;
}
// now we have T_ELSE following by T_IF so we could fix this
// 1. clear whitespaces between T_ELSE and T_IF
$tokens[$index + 1]->clear();
// 2. change token from T_ELSE into T_ELSEIF
$tokens->overrideAt($index, array(T_ELSEIF, 'elseif'));
// 3. clear succeeding T_IF
$tokens[$ifTokenIndex]->clear();
// 4. clear extra whitespace after T_IF in T_COMMENT,T_WHITESPACE?,T_IF,T_WHITESPACE sequence
if ($tokens[$beforeIfTokenIndex]->isComment() && $tokens[$ifTokenIndex + 1]->isWhitespace()) {
$tokens[$ifTokenIndex + 1]->clear();
}
}
}
示例5: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$functionyTokens = $this->getFunctionyTokenKinds();
$languageConstructionTokens = $this->getLanguageConstructionTokenKinds();
foreach ($tokens as $index => $token) {
// looking for start brace
if (!$token->equals('(')) {
continue;
}
// last non-whitespace token
$lastTokenIndex = $tokens->getPrevNonWhitespace($index);
if (null === $lastTokenIndex) {
continue;
}
// check for ternary operator
$endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
$nextNonWhiteSpace = $tokens->getNextMeaningfulToken($endParenthesisIndex);
if (null !== $nextNonWhiteSpace && $tokens[$nextNonWhiteSpace]->equals('?') && $tokens[$lastTokenIndex]->isGivenKind($languageConstructionTokens)) {
continue;
}
// check if it is a function call
if ($tokens[$lastTokenIndex]->isGivenKind($functionyTokens)) {
$this->fixFunctionCall($tokens, $index);
} elseif ($tokens[$lastTokenIndex]->isGivenKind(T_STRING)) {
// for real function calls or definitions
$possibleDefinitionIndex = $tokens->getPrevMeaningfulToken($lastTokenIndex);
if (!$tokens[$possibleDefinitionIndex]->isGivenKind(T_FUNCTION)) {
$this->fixFunctionCall($tokens, $index);
}
}
}
}
示例6: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(array(T_CASE, T_DEFAULT))) {
continue;
}
$ternariesCount = 0;
for ($colonIndex = $index + 1;; ++$colonIndex) {
// We have to skip ternary case for colons.
if ($tokens[$colonIndex]->equals('?')) {
++$ternariesCount;
}
if ($tokens[$colonIndex]->equalsAny(array(':', ';'))) {
if (0 === $ternariesCount) {
break;
}
--$ternariesCount;
}
}
$valueIndex = $tokens->getPrevNonWhitespace($colonIndex);
if (2 + $valueIndex === $colonIndex) {
$tokens[$valueIndex + 1]->clear();
}
}
}
示例7: fixSpace
/**
* Method to insert space after comma and remove space before comma.
*
* @param Tokens $tokens
* @param int $index
*/
public function fixSpace(Tokens $tokens, $index)
{
// remove space before comma if exist
if ($tokens[$index - 1]->isWhitespace()) {
$prevIndex = $tokens->getPrevNonWhitespace($index - 1);
if (!$tokens[$prevIndex]->equalsAny(array(',', array(T_END_HEREDOC)))) {
$tokens[$index - 1]->clear();
}
}
$nextToken = $tokens[$index + 1];
// Two cases for fix space after comma (exclude multiline comments)
// 1) multiple spaces after comma
// 2) no space after comma
if ($nextToken->isWhitespace()) {
if ($this->isCommentLastLineToken($tokens, $index + 2)) {
return;
}
$newContent = ltrim($nextToken->getContent(), " \t");
if ('' === $newContent) {
$newContent = ' ';
}
$nextToken->setContent($newContent);
return;
}
if (!$this->isCommentLastLineToken($tokens, $index + 1)) {
$tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
}
}
示例8: 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;
}
}
}
示例9: fixConcatenationToNoSpace
/**
* @param Tokens $tokens
* @param int $index index of concatenation '.' token
*/
private function fixConcatenationToNoSpace(Tokens $tokens, $index)
{
if (!$tokens[$tokens->getPrevNonWhitespace($index)]->isGivenKind(T_LNUMBER)) {
$tokens->removeLeadingWhitespace($index, " \t");
}
if (!$tokens[$tokens->getNextNonWhitespace($index)]->isGivenKind(array(T_LNUMBER, T_COMMENT, T_DOC_COMMENT))) {
$tokens->removeTrailingWhitespace($index, " \t");
}
}
示例10: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_LIST)) {
continue;
}
$openIndex = $tokens->getNextMeaningfulToken($index);
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);
$markIndex = null;
$prevIndex = $tokens->getPrevNonWhitespace($closeIndex);
while ($tokens[$prevIndex]->equals(',')) {
$markIndex = $prevIndex;
$prevIndex = $tokens->getPrevNonWhitespace($prevIndex);
}
if (null !== $markIndex) {
$tokens->clearRange($tokens->getPrevNonWhitespace($markIndex) + 1, $closeIndex - 1);
}
}
}
示例11: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->equals('.')) {
continue;
}
if (!$tokens[$tokens->getPrevNonWhitespace($index)]->isGivenKind(T_LNUMBER)) {
$tokens->removeLeadingWhitespace($index, " \t");
}
if (!$tokens[$tokens->getNextNonWhitespace($index)]->isGivenKind(T_LNUMBER)) {
$tokens->removeTrailingWhitespace($index, " \t");
}
}
}
示例12: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
if (!$tokens->isMonolithicPhp()) {
return;
}
$closeTags = $tokens->findGivenKind(T_CLOSE_TAG);
if (empty($closeTags)) {
return;
}
list($index, $token) = each($closeTags);
$tokens->removeLeadingWhitespace($index);
$token->clear();
$prevIndex = $tokens->getPrevNonWhitespace($index);
$prevToken = $tokens[$prevIndex];
if (!$prevToken->equalsAny(array(';', '}'))) {
$tokens->insertAt($prevIndex + 1, new Token(';'));
}
}
示例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: fixArray
/**
* Method to trim leading/trailing whitespace within single line arrays.
*
* @param Tokens $tokens
* @param int $index
*/
private static function fixArray(Tokens $tokens, $index)
{
$startIndex = $index;
if ($tokens[$startIndex]->isGivenKind(T_ARRAY)) {
$startIndex = $tokens->getNextMeaningfulToken($startIndex);
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
} else {
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $startIndex);
}
$nextToken = $tokens[$startIndex + 1];
$nextNonWhitespaceIndex = $tokens->getNextNonWhitespace($startIndex);
$nextNonWhitespaceToken = $tokens[$nextNonWhitespaceIndex];
$tokenAfterNextNonWhitespaceToken = $tokens[$nextNonWhitespaceIndex + 1];
$prevToken = $tokens[$endIndex - 1];
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($endIndex);
$prevNonWhitespaceToken = $tokens[$prevNonWhitespaceIndex];
if ($nextToken->isWhitespace(" \t") && (!$nextNonWhitespaceToken->isComment() || $nextNonWhitespaceIndex === $prevNonWhitespaceIndex || $tokenAfterNextNonWhitespaceToken->isWhitespace(" \t") || '/*' === substr($nextNonWhitespaceToken->getContent(), 0, 2))) {
$nextToken->clear();
}
if ($prevToken->isWhitespace(" \t") && !$prevNonWhitespaceToken->equals(',')) {
$prevToken->clear();
}
}
示例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, ' ')));
}
}