本文整理匯總了PHP中Symfony\CS\Tokenizer\Tokens::getNextNonWhitespace方法的典型用法代碼示例。如果您正苦於以下問題:PHP Tokens::getNextNonWhitespace方法的具體用法?PHP Tokens::getNextNonWhitespace怎麽用?PHP Tokens::getNextNonWhitespace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\CS\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::getNextNonWhitespace方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$namespace = false;
$classyName = null;
$classyIndex = 0;
foreach ($tokens as $index => $token) {
if ($token->isGivenKind(T_NAMESPACE)) {
if (false !== $namespace) {
return;
}
$namespace = true;
} elseif ($token->isClassy()) {
if (null !== $classyName) {
return;
}
$classyIndex = $tokens->getNextNonWhitespace($index);
$classyName = $tokens[$classyIndex]->getContent();
}
}
if (null === $classyName) {
return;
}
if (false !== $namespace) {
$filename = basename(strtr($file->getRealPath(), '\\', '/'), '.php');
if ($classyName !== $filename) {
$tokens[$classyIndex]->setContent($filename);
}
} else {
$normClass = strtr($classyName, '_', '/');
$filename = substr(strtr($file->getRealPath(), '\\', '/'), -strlen($normClass) - 4, -4);
if ($normClass !== $filename && strtolower($normClass) === strtolower($filename)) {
$tokens[$classyIndex]->setContent(strtr($filename, '/', '_'));
}
}
}
示例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;
}
$nextIndex = $tokens->getNextNonWhitespace($index);
$nextToken = $tokens[$nextIndex];
// if next meaning token is not T_IF - continue searching, this is not the case for fixing
if (!$nextToken->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
$nextToken->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: 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->getNextNonWhitespace($includy['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->insertAt($includy['begin'] + 1, new Token(array(T_WHITESPACE, ' ')));
}
}
}
示例4: 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(')')));
}
}
示例5: findHeaderCommentInsertionIndex
private function findHeaderCommentInsertionIndex(Tokens $tokens)
{
$index = $tokens->getNextNonWhitespace(0);
if (null === $index) {
$index = $tokens->getSize();
}
return $index;
}
示例6: fixAssertSame
private function fixAssertSame(Tokens $tokens, $index)
{
static $map = array('false' => 'assertFalse', 'null' => 'assertNull', 'true' => 'assertTrue');
$sequence = $tokens->findSequence(array(array(T_VARIABLE, '$this'), array(T_OBJECT_OPERATOR, '->'), array(T_STRING, 'assertSame'), '('), $index);
if (null === $sequence) {
return;
}
$sequenceIndexes = array_keys($sequence);
$sequenceIndexes[4] = $tokens->getNextMeaningfulToken($sequenceIndexes[3]);
$firstParameterToken = $tokens[$sequenceIndexes[4]];
if (!$firstParameterToken->isNativeConstant()) {
return;
}
$sequenceIndexes[5] = $tokens->getNextNonWhitespace($sequenceIndexes[4]);
$tokens[$sequenceIndexes[2]]->setContent($map[$firstParameterToken->getContent()]);
$tokens->clearRange($sequenceIndexes[4], $tokens->getNextNonWhitespace($sequenceIndexes[5]) - 1);
return $sequenceIndexes[5];
}
示例7: fixLineBreaksPerImportGroup
/**
* Fix the line breaks per group.
*
* For each use token reach the nearest ; and ensure every
* token after has one \n before next non empty token (next line).
* It skips the first pass from the bottom.
*
* @param Tokens $tokens
* @param array $uses
*/
private function fixLineBreaksPerImportGroup(Tokens $tokens, array $uses)
{
foreach ($uses as $index) {
$endIndex = $tokens->getNextTokenOfKind($index, array(';'));
$afterSemicolonIndex = $tokens->getNextNonWhitespace($endIndex);
if (null !== $afterSemicolonIndex && !$tokens[$afterSemicolonIndex]->isGivenKind(T_USE)) {
continue;
}
$nextToken = $tokens[$endIndex + 1];
if ($nextToken->isWhitespace()) {
$nextToken->setContent(preg_replace('/\\n{2,}/', "\n", $nextToken->getContent()));
}
}
}
示例8: 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");
}
}
}
示例9: extract
/**
* @param Tokens $tokens
*
* @throws ExtractionException
*
* @return string
*/
public function extract(Tokens $tokens)
{
$classyName = null;
foreach ($tokens as $index => $token) {
if ($token->isClassy()) {
$classyIndex = $tokens->getNextNonWhitespace($index);
$classyName = $tokens[$classyIndex]->getContent();
}
}
if (null === $classyName) {
throw new ExtractionException('No way to parse class name of this class');
}
return $classyName;
}
示例10: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
static $forbiddenSuccessors = array(T_DOC_COMMENT, T_COMMENT, T_WHITESPACE, T_RETURN, T_THROW, T_GOTO, T_CONTINUE, T_BREAK);
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_DOC_COMMENT)) {
continue;
}
// get the next non-whitespace token inc comments, provided
// that there is whitespace between it and the current token
$next = $tokens->getNextNonWhitespace($index);
if ($index + 2 === $next && false === $tokens[$next]->isGivenKind($forbiddenSuccessors)) {
$this->fixWhitespace($tokens[$index + 1]);
}
}
}
示例11: extract
/**
* @param Tokens $tokens
*
* @throws ExtractionException
*
* @return string
*/
public function extract(Tokens $tokens)
{
$namespace = null;
foreach ($tokens as $index => $token) {
if ($token->isGivenKind(T_NAMESPACE)) {
$namespaceIndex = $tokens->getNextNonWhitespace($index);
$namespaceEndIndex = $tokens->getNextTokenOfKind($index, array(';'));
$namespace = trim($tokens->generatePartialCode($namespaceIndex, $namespaceEndIndex - 1));
}
}
if (null === $namespace) {
throw new ExtractionException('No way to parse the namespace of this class');
}
return $namespace;
}
示例12: 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, ' ');
}
}
示例13: fixAssert
private function fixAssert(array $map, Tokens $tokens, $index, $method)
{
$sequence = $tokens->findSequence(array(array(T_VARIABLE, '$this'), array(T_OBJECT_OPERATOR, '->'), array(T_STRING, $method), '('), $index);
if (null === $sequence) {
return;
}
$sequenceIndexes = array_keys($sequence);
$sequenceIndexes[4] = $tokens->getNextMeaningfulToken($sequenceIndexes[3]);
$firstParameterToken = $tokens[$sequenceIndexes[4]];
if (!$firstParameterToken->isNativeConstant()) {
return;
}
$sequenceIndexes[5] = $tokens->getNextMeaningfulToken($sequenceIndexes[4]);
if (!$tokens[$sequenceIndexes[5]]->equals(',')) {
return;
}
$tokens[$sequenceIndexes[2]]->setContent($map[$firstParameterToken->getContent()]);
$tokens->clearRange($sequenceIndexes[4], $tokens->getNextNonWhitespace($sequenceIndexes[5]) - 1);
return $sequenceIndexes[5];
}
示例14: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$foundNamespace = $tokens->findGivenKind(T_NAMESPACE);
if (empty($foundNamespace)) {
return;
}
$tokensAnalyzer = new TokensAnalyzer($tokens);
$firstNamespaceIdx = key($foundNamespace);
$usesIdxs = $tokensAnalyzer->getImportUseIndexes();
foreach ($usesIdxs as $idx) {
if ($idx < $firstNamespaceIdx) {
continue;
}
$nextTokenIdx = $tokens->getNextNonWhitespace($idx);
$nextToken = $tokens[$nextTokenIdx];
if ($nextToken->isGivenKind(T_NS_SEPARATOR)) {
$nextToken->clear();
}
}
}
示例15: findIncludies
private function findIncludies(Tokens $tokens)
{
static $includyTokenKinds = array(T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE);
$includies = array();
foreach ($tokens->findGivenKind($includyTokenKinds) as $includyTokens) {
foreach ($includyTokens as $index => $token) {
$includy = array('begin' => $index, 'braces' => null, 'end' => $tokens->getNextTokenOfKind($index, array(';')));
// Don't remove when the statement is wrapped. include is also legal as function parameter
// but requires being wrapped then
if (!$tokens[$tokens->getPrevNonWhitespace($index)]->equals('(')) {
$nextTokenIndex = $tokens->getNextNonWhitespace($index);
$nextToken = $tokens[$nextTokenIndex];
if ($nextToken->equals('(')) {
$includy['braces'] = array('open' => $nextTokenIndex, 'close' => $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextTokenIndex));
}
}
$includies[] = $includy;
}
}
return $includies;
}