本文整理匯總了PHP中Symfony\CS\Tokenizer\Tokens::getNextTokenOfKind方法的典型用法代碼示例。如果您正苦於以下問題:PHP Tokens::getNextTokenOfKind方法的具體用法?PHP Tokens::getNextTokenOfKind怎麽用?PHP Tokens::getNextTokenOfKind使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\CS\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::getNextTokenOfKind方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: fixContent
/**
* @param Tokens|Token[] $tokens
*
* @return void
*/
protected function fixContent(Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if ($token->isGivenKind([T_FUNCTION])) {
$openingBraceIndex = $tokens->getNextTokenOfKind($index, ['{']);
if ($openingBraceIndex === null) {
$openingBracketIndex = $tokens->getNextTokenOfKind($index, ['(']);
$closingBracketIndex = $tokens->getNextTokenOfKind($index, [')']);
$nextIndex = $tokens->getNextMeaningfulToken($closingBracketIndex);
if (!$this->isEmptyLineAfterIndex($tokens, $nextIndex)) {
$tokens[$nextIndex + 1]->setContent("\n" . $tokens[$nextIndex + 1]->getContent());
}
continue;
}
$closingBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $openingBraceIndex);
// Ignore closures
$nextIndex = $tokens->getNextMeaningfulToken($closingBraceIndex);
if ($tokens[$nextIndex]->equals(';') || $tokens[$nextIndex]->equals(',') || $tokens[$nextIndex]->equals(')')) {
continue;
}
if (!$this->isEmptyLineAfterIndex($tokens, $closingBraceIndex)) {
$tokens[$closingBraceIndex + 1]->setContent("\n" . $tokens[$closingBraceIndex + 1]->getContent());
}
}
}
}
示例2: fixFunction
private function fixFunction(Tokens $tokens, $functionIndex, array $functionParams)
{
$startBraceIndex = $tokens->getNextTokenOfKind($functionIndex, array('('));
$endBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startBraceIndex);
$commaCounter = 0;
$sawParameter = false;
for ($index = $startBraceIndex + 1; $index < $endBraceIndex; ++$index) {
$token = $tokens[$index];
if (!$token->isWhitespace() && !$token->isComment()) {
$sawParameter = true;
}
if ($token->equals('(')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
continue;
}
if ($token->equals('[')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_SQUARE_BRACE, $index);
continue;
}
if ($token->equals(',')) {
++$commaCounter;
continue;
}
}
$functionParamsQuantity = count($functionParams);
$paramsQuantity = ($sawParameter ? 1 : 0) + $commaCounter;
if ($paramsQuantity === $functionParamsQuantity) {
return;
}
$tokensToInsert = array();
for ($i = $paramsQuantity; $i < $functionParamsQuantity; ++$i) {
if (!$functionParams[$i]) {
return;
}
$tokensToInsert[] = new Token(',');
$tokensToInsert[] = new Token(array(T_WHITESPACE, ' '));
if (!is_array($functionParams[$i])) {
$tokensToInsert[] = clone $functionParams[$i];
continue;
}
foreach ($functionParams[$i] as $param) {
$tokensToInsert[] = clone $param;
}
}
$beforeEndBraceIndex = $tokens->getPrevNonWhitespace($endBraceIndex, array());
$tokens->insertAt($beforeEndBraceIndex + 1, $tokensToInsert);
}
示例3: replaceNameOccurrences
/**
* Replace occurrences of the name of the classy element by "self" (if possible).
*
* @param Tokens $tokens
* @param string $name
* @param int $startIndex
* @param int $endIndex
*/
private function replaceNameOccurrences(Tokens $tokens, $name, $startIndex, $endIndex)
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
for ($i = $startIndex; $i < $endIndex; ++$i) {
$token = $tokens[$i];
// skip lambda functions (PHP < 5.4 compatibility)
if ($token->isGivenKind(T_FUNCTION) && $tokensAnalyzer->isLambda($i)) {
$i = $tokens->getNextTokenOfKind($i, array('{'));
$i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $i);
continue;
}
if (!$token->equals(array(T_STRING, $name), false)) {
continue;
}
$prevToken = $tokens[$tokens->getPrevMeaningfulToken($i)];
$nextToken = $tokens[$tokens->getNextMeaningfulToken($i)];
// skip tokens that are part of a fully qualified name
if ($prevToken->isGivenKind(T_NS_SEPARATOR) || $nextToken->isGivenKind(T_NS_SEPARATOR)) {
continue;
}
if ($prevToken->isGivenKind(array(T_INSTANCEOF, T_NEW)) || $nextToken->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM)) {
$token->setContent('self');
}
}
}
示例4: 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)));
}
}
}
示例5: fixArray
private function fixArray(Tokens $tokens, $index)
{
if (!$tokens->isArrayMultiLine($index)) {
return;
}
$startIndex = $index;
if ($tokens[$startIndex]->isGivenKind(T_ARRAY)) {
$startIndex = $tokens->getNextTokenOfKind($startIndex, array('('));
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
} else {
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_SQUARE_BRACE, $startIndex);
}
$beforeEndIndex = $tokens->getPrevMeaningfulToken($endIndex);
$beforeEndToken = $tokens[$beforeEndIndex];
if ($startIndex !== $beforeEndIndex && !$beforeEndToken->equalsAny(array(',', array(T_END_HEREDOC)))) {
$tokens->insertAt($beforeEndIndex + 1, new Token(','));
$endToken = $tokens[$endIndex];
if (!$endToken->isComment() && !$endToken->isWhitespace()) {
$tokens->ensureWhitespaceAtIndex($endIndex, 1, ' ');
}
}
}
示例6: process
/**
* {@inheritdoc}
*/
public function process(Tokens $tokens, Token $token, $index)
{
$prevTokenIndex = $tokens->getPrevMeaningfulToken($index);
$prevToken = $prevTokenIndex === null ? null : $tokens[$prevTokenIndex];
// Skip whole class braces content.
// That way we can skip whole tokens in class declaration, therefore skip `T_USE` for traits.
if ($token->isClassy() && !$prevToken->isGivenKind(T_DOUBLE_COLON)) {
$index = $tokens->getNextTokenOfKind($index, array('{'));
$innerLimit = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
while ($index < $innerLimit) {
$token = $tokens[++$index];
if (!$token->isGivenKind(T_USE)) {
continue;
}
if ($this->isUseForLambda($tokens, $index)) {
$token->override(array(CT_USE_LAMBDA, $token->getContent()));
} else {
$token->override(array(CT_USE_TRAIT, $token->getContent()));
}
}
return;
}
if ($token->isGivenKind(T_USE) && $this->isUseForLambda($tokens, $index)) {
$token->override(array(CT_USE_LAMBDA, $token->getContent()));
}
}
示例7: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_ECHO)) {
continue;
}
$nextTokenIndex = $tokens->getNextMeaningfulToken($index);
$endTokenIndex = $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)));
$canBeConverted = true;
for ($i = $nextTokenIndex; $i < $endTokenIndex; ++$i) {
if ($tokens[$i]->equalsAny(array('(', array(CT_ARRAY_SQUARE_BRACE_OPEN)))) {
$blockType = $tokens->detectBlockType($tokens[$i]);
$i = $tokens->findBlockEnd($blockType['type'], $i);
}
if ($tokens[$i]->equals(',')) {
$canBeConverted = false;
break;
}
}
if (false === $canBeConverted) {
continue;
}
$tokens->overrideAt($index, array(T_PRINT, 'print'));
}
}
示例8: process
/**
* {@inheritdoc}
*/
public function process(Tokens $tokens)
{
foreach ($tokens->findGivenKind(T_DOLLAR_OPEN_CURLY_BRACES) as $index => $token) {
$nextIndex = $tokens->getNextTokenOfKind($index, array('}'));
$tokens[$nextIndex]->override(array(CT_DOLLAR_CLOSE_CURLY_BRACES, '}', $tokens[$nextIndex]->getLine()));
}
}
示例9: getNewOrder
private function getNewOrder(array $uses, Tokens $tokens)
{
$uses = array_reverse($uses);
$indexes = array();
$originalIndexes = array();
foreach ($uses as $index) {
$endIndex = $tokens->getNextTokenOfKind($index, array(';'));
$startIndex = $tokens->getTokenNotOfKindSibling($index + 1, 1, array(array(T_WHITESPACE)));
$namespace = '';
$index = $startIndex;
while ($index <= $endIndex) {
$token = $tokens[$index];
/** @var Token $token */
if ($token->equals(',') || $index === $endIndex) {
$indexes[$startIndex] = array($namespace, $startIndex, $index - 1);
$originalIndexes[] = $startIndex;
$namespace = '';
$nextPartIndex = $tokens->getTokenNotOfKindSibling($index, 1, array(array(','), array(T_WHITESPACE)));
$startIndex = $nextPartIndex;
$index = $nextPartIndex;
continue;
}
$namespace .= $token->getContent();
++$index;
}
}
uasort($indexes, 'self::sortingCallBack');
$i = -1;
$usesOrder = array();
// Loop trough the index but use original index order
foreach ($indexes as $v) {
$usesOrder[$originalIndexes[++$i]] = $v;
}
return $usesOrder;
}
示例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_NEW)) {
continue;
}
$nextIndex = $tokens->getNextTokenOfKind($index, array(':', ';', ',', '(', ')', '[', ']', 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->isGivenKind(T_WHITESPACE)) {
$nextIndex = $tokens->getNextNonWhitespace($nextIndex);
$nextToken = $tokens[$nextIndex];
}
// new statement with () - nothing to do
if ($nextToken->equals('(')) {
continue;
}
$meaningBeforeNextIndex = $tokens->getPrevNonWhitespace($nextIndex);
$tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')')));
}
}
示例11: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
$uses = array_reverse($tokensAnalyzer->getImportUseIndexes());
foreach ($uses as $index) {
$endIndex = $tokens->getNextTokenOfKind($index, array(';'));
$declarationContent = $tokens->generatePartialCode($index + 1, $endIndex - 1);
$declarationParts = explode(',', $declarationContent);
if (1 === count($declarationParts)) {
continue;
}
$declarationContent = array();
foreach ($declarationParts as $declarationPart) {
$declarationContent[] = 'use ' . trim($declarationPart) . ';';
}
$declarationContent = implode("\n" . $this->detectIndent($tokens, $index), $declarationContent);
for ($i = $index; $i <= $endIndex; ++$i) {
$tokens[$i]->clear();
}
$declarationTokens = Tokens::fromCode('<?php ' . $declarationContent);
$declarationTokens[0]->clear();
$declarationTokens->clearEmptyTokens();
$tokens->insertAt($index, $declarationTokens);
}
}
示例12: 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, ' ')));
}
}
}
}
示例13: transformIntoDollarCloseBrace
private function transformIntoDollarCloseBrace(Tokens $tokens, Token $token, $index)
{
if ($token->isGivenKind(T_DOLLAR_OPEN_CURLY_BRACES)) {
$nextIndex = $tokens->getNextTokenOfKind($index, array('}'));
$tokens[$nextIndex]->override(array(CT_DOLLAR_CLOSE_CURLY_BRACES, '}'));
}
}
示例14: getNamespaceUseDeclarations
private function getNamespaceUseDeclarations(Tokens $tokens, array $useIndexes)
{
$uses = array();
foreach ($useIndexes as $index) {
$declarationEndIndex = $tokens->getNextTokenOfKind($index, array(';'));
$declarationContent = $tokens->generatePartialCode($index + 1, $declarationEndIndex - 1);
// ignore multiple use statements like: `use BarB, BarC as C, BarD;`
// that should be split into few separate statements
if (false !== strpos($declarationContent, ',')) {
continue;
}
$declarationParts = preg_split('/\\s+as\\s+/i', $declarationContent);
if (1 === count($declarationParts)) {
$fullName = $declarationContent;
$declarationParts = explode('\\', $fullName);
$shortName = end($declarationParts);
$aliased = false;
} else {
$fullName = $declarationParts[0];
$shortName = $declarationParts[1];
$declarationParts = explode('\\', $fullName);
$aliased = $shortName !== end($declarationParts);
}
$shortName = trim($shortName);
$uses[$shortName] = array('shortName' => $shortName, 'fullName' => trim($fullName), 'aliased' => $aliased, 'declarationStart' => $index, 'declarationEnd' => $declarationEndIndex);
}
return $uses;
}
示例15: 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 (!$token->isGivenKind(T_FUNCTION)) {
continue;
}
$startParenthesisIndex = $tokens->getNextTokenOfKind($index, array('('));
$endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startParenthesisIndex);
$startBraceIndex = $tokens->getNextTokenOfKind($endParenthesisIndex, array(';', '{'));
$startBraceToken = $tokens[$startBraceIndex];
if ($startBraceToken->equals('{')) {
// fix single-line whitespace before {
// eg: `function foo(){}` => `function foo() {}`
// eg: `function foo() {}` => `function foo() {}`
if (!$tokens[$startBraceIndex - 1]->isWhitespace() || $tokens[$startBraceIndex - 1]->isWhitespace($this->singleLineWhitespaceOptions)) {
$tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, ' ');
}
}
$afterParenthesisIndex = $tokens->getNextNonWhitespace($endParenthesisIndex);
$afterParenthesisToken = $tokens[$afterParenthesisIndex];
if ($afterParenthesisToken->isGivenKind(CT_USE_LAMBDA)) {
$useStartParenthesisIndex = $tokens->getNextTokenOfKind($afterParenthesisIndex, array('('));
$useEndParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $useStartParenthesisIndex);
// fix whitespace after CT_USE_LAMBDA
$tokens->ensureWhitespaceAtIndex($afterParenthesisIndex + 1, 0, ' ');
// remove single-line edge whitespaces inside use parentheses
$this->fixParenthesisInnerEdge($tokens, $useStartParenthesisIndex, $useEndParenthesisIndex);
// fix whitespace before CT_USE_LAMBDA
$tokens->ensureWhitespaceAtIndex($afterParenthesisIndex - 1, 1, ' ');
}
// remove single-line edge whitespaces inside parameters list parentheses
$this->fixParenthesisInnerEdge($tokens, $startParenthesisIndex, $endParenthesisIndex);
if (!$tokensAnalyzer->isLambda($index)) {
// remove whitespace before (
// eg: `function foo () {}` => `function foo() {}`
if ($tokens[$startParenthesisIndex - 1]->isWhitespace()) {
$tokens[$startParenthesisIndex - 1]->clear();
}
}
// fix whitespace after T_FUNCTION
// eg: `function foo() {}` => `function foo() {}`
$tokens->ensureWhitespaceAtIndex($index + 1, 0, ' ');
}
}