當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Tokens::fromCode方法代碼示例

本文整理匯總了PHP中Symfony\CS\Tokenizer\Tokens::fromCode方法的典型用法代碼示例。如果您正苦於以下問題:PHP Tokens::fromCode方法的具體用法?PHP Tokens::fromCode怎麽用?PHP Tokens::fromCode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Symfony\CS\Tokenizer\Tokens的用法示例。


在下文中一共展示了Tokens::fromCode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     for ($index = 0, $count = $tokens->count(); $index < $count; ++$index) {
         // skip T_FOR parenthesis to ignore duplicated `;` like `for ($i = 1; ; ++$i) {...}`
         if ($tokens[$index]->isGivenKind(T_FOR)) {
             $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $tokens->getNextMeaningfulToken($index)) + 1;
             continue;
         }
         if (!$tokens[$index]->equals(';')) {
             continue;
         }
         $previousMeaningfulIndex = $tokens->getPrevMeaningfulToken($index);
         // A semicolon can always be removed if it follows a semicolon, '{' or opening tag.
         if ($tokens[$previousMeaningfulIndex]->equalsAny(array('{', ';', array(T_OPEN_TAG)))) {
             $tokens->clearTokenAndMergeSurroundingWhitespace($index);
             continue;
         }
         // A semicolon might be removed if it follows a '}' but only if the brace is part of certain structures.
         if ($tokens[$previousMeaningfulIndex]->equals('}')) {
             $this->fixSemicolonAfterCurlyBraceClose($tokens, $index, $previousMeaningfulIndex);
         }
     }
     return $tokens->generateCode();
 }
開發者ID:Doability,項目名稱:magento2dev,代碼行數:28,代碼來源:NoEmptyStatementFixer.php

示例2: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $namespacesImports = $tokens->getImportUseIndexes(true);
     if (0 === count($namespacesImports)) {
         return $content;
     }
     $usesOrder = array();
     foreach ($namespacesImports as $uses) {
         $usesOrder = array_replace($usesOrder, $this->getNewOrder(array_reverse($uses), $tokens));
     }
     // First clean the old content
     // This must be done first as the indexes can be scattered
     foreach ($usesOrder as $use) {
         $tokens->clearRange($use['startIndex'], $use['endIndex']);
     }
     $usesOrder = array_reverse($usesOrder, true);
     // Now insert the new tokens, starting from the end
     foreach ($usesOrder as $index => $use) {
         $declarationTokens = Tokens::fromCode('<?php use ' . $use['namespace'] . ';');
         $declarationTokens->clearRange(0, 2);
         // clear `<?php use `
         $declarationTokens[count($declarationTokens) - 1]->clear();
         // clear `;`
         $declarationTokens->clearEmptyTokens();
         $tokens->insertAt($index, $declarationTokens);
     }
     return $tokens->generateCode();
 }
開發者ID:Doability,項目名稱:magento2dev,代碼行數:32,代碼來源:OrderedUseFixer.php

示例3: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     // ignore non-monolithic files
     if (!$tokens->isMonolithicPhp()) {
         return $content;
     }
     // ignore files with short open tag
     if (!$tokens[0]->isGivenKind(T_OPEN_TAG)) {
         return $content;
     }
     $newlineFound = false;
     /** @var Token $token */
     foreach ($tokens as $token) {
         if ($token->isWhitespace(array('whitespaces' => "\n"))) {
             $newlineFound = true;
             break;
         }
     }
     // ignore one-line files
     if (!$newlineFound) {
         return $content;
     }
     $token = $tokens[0];
     if (false === strpos($token->getContent(), "\n")) {
         $token->setContent(rtrim($token->getContent()) . "\n");
     }
     if (!$tokens[1]->isWhitespace() && false === strpos($tokens[1]->getContent(), "\n")) {
         $tokens->insertAt(1, new Token(array(T_WHITESPACE, "\n")));
     }
     return $tokens->generateCode();
 }
開發者ID:Flesh192,項目名稱:magento,代碼行數:35,代碼來源:BlanklineAfterOpenTagFixer.php

示例4: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     // Checks if specific statements are set and uses them in this case.
     $loops = array_intersect_key(self::$loops, array_flip(self::$controlStatements));
     foreach ($tokens as $index => $token) {
         if (!$token->equals('(')) {
             continue;
         }
         $blockStartIndex = $index;
         $index = $tokens->getPrevMeaningfulToken($index);
         $token = $tokens[$index];
         foreach ($loops as $loop) {
             if (!$token->isGivenKind($loop['lookupTokens'])) {
                 continue;
             }
             $blockEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $blockStartIndex);
             $blockEndNextIndex = $tokens->getNextMeaningfulToken($blockEndIndex);
             if (!$tokens[$blockEndNextIndex]->equalsAny($loop['neededSuccessors'])) {
                 continue;
             }
             if ($tokens[$blockStartIndex - 1]->isWhitespace() || $tokens[$blockStartIndex - 1]->isComment()) {
                 $this->clearParenthesis($tokens, $blockStartIndex);
             } else {
                 // Adds a space to prevent broken code like `return2`.
                 $tokens->overrideAt($blockStartIndex, array(T_WHITESPACE, ' '));
             }
             $this->clearParenthesis($tokens, $blockEndIndex);
         }
     }
     return $tokens->generateCode();
 }
開發者ID:gmoigneu,項目名稱:magento2-platformsh,代碼行數:35,代碼來源:UnneededControlParenthesesFixer.php

示例5: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     $namespacesImports = $tokensAnalyzer->getImportUseIndexes(true);
     $usesOrder = array();
     if (!count($namespacesImports)) {
         return;
     }
     foreach ($namespacesImports as $uses) {
         $uses = array_reverse($uses);
         $usesOrder = array_replace($usesOrder, $this->getNewOrder($uses, $tokens));
     }
     $usesOrder = array_reverse($usesOrder, true);
     $mapStartToEnd = array();
     foreach ($usesOrder as $use) {
         $mapStartToEnd[$use[1]] = $use[2];
     }
     // Now insert the new tokens, starting from the end
     foreach ($usesOrder as $index => $use) {
         $declarationTokens = Tokens::fromCode('<?php use ' . $use[0] . ';');
         $declarationTokens->clearRange(0, 2);
         // clear `<?php use `
         $declarationTokens[count($declarationTokens) - 1]->clear();
         // clear `;`
         $declarationTokens->clearEmptyTokens();
         $tokens->overrideRange($index, $mapStartToEnd[$index], $declarationTokens);
     }
 }
開發者ID:nazimodi,項目名稱:PHP-CS-Fixer,代碼行數:31,代碼來源:OrderedUseFixer.php

示例6: injectAlignmentPlaceholders

 /**
  * Inject into the text placeholders of candidates of vertical alignment.
  *
  * Output structure:
  * * Tokens $tokens
  * * int    $deepestLevel
  *
  * @param string $content
  *
  * @return array
  */
 private function injectAlignmentPlaceholders($content)
 {
     $deepestLevel = 0;
     $parenCount = 0;
     $bracketCount = 0;
     $tokens = Tokens::fromCode($content);
     foreach ($tokens as $token) {
         $tokenContent = $token->getContent();
         if (0 === $parenCount && 0 === $bracketCount && $token->equals('=')) {
             $token->setContent(sprintf(self::ALIGNABLE_PLACEHOLDER, $deepestLevel) . $tokenContent);
             continue;
         }
         if ($token->isGivenKind(T_FUNCTION)) {
             ++$deepestLevel;
         } elseif ($token->equals('(')) {
             ++$parenCount;
         } elseif ($token->equals(')')) {
             --$parenCount;
         } elseif ($token->equals('[')) {
             ++$bracketCount;
         } elseif ($token->equals(']')) {
             --$bracketCount;
         }
     }
     return array('tokens' => $tokens, 'deepestLevel' => $deepestLevel);
 }
開發者ID:Doability,項目名稱:magento2dev,代碼行數:37,代碼來源:AlignEqualsFixer.php

示例7: fix

 /**
  * Replace all `else if` (T_ELSE T_IF) with `elseif` (T_ELSEIF).
  *
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     foreach ($tokens->findGivenKind(T_ELSE) as $index => $token) {
         $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
         $token->override(array(T_ELSEIF, 'elseif', $token->getLine()));
         // 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');
         }
     }
     return $tokens->generateCode();
 }
開發者ID:shabbirvividads,項目名稱:magento2,代碼行數:32,代碼來源:ElseifFixer.php

示例8: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     if (!$tokens->isMonolithicPhp()) {
         return $content;
     }
     $index = 1;
     $hasNs = false;
     for ($i = 1; $i < $tokens->count(); $i++) {
         if ($tokens[$i]->isGivenKind(T_NAMESPACE)) {
             $hasNs = true;
             while ($tokens[$i]->getContent() !== ';') {
                 $i++;
             }
             $i++;
             $index = $i + 1;
         } elseif (!$tokens[$i]->isWhitespace() && !$tokens[$i]->isGivenKind(T_COMMENT)) {
             break;
         }
         $tokens[$i]->clear();
     }
     if ('' !== self::$headerComment) {
         $tokens->insertAt($index, [new Token([T_WHITESPACE, "\n" . ($hasNs ? "\n" : '')]), new Token([T_COMMENT, self::$headerComment]), new Token([T_WHITESPACE, "\n\n"])]);
     }
     $tokens->clearEmptyTokens();
     return $tokens->generateCode();
 }
開發者ID:cyberhouse,項目名稱:phpstyle,代碼行數:30,代碼來源:LowerHeaderCommentFixer.php

示例9: fix

 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $functionyTokens = $this->getFunctionyTokenKinds();
     $languageConstructionTokens = $this->getLanguageConstructionTokenKinds();
     foreach ($tokens as $index => $token) {
         if (!$token->equals('(')) {
             continue;
         }
         $lastTokenIndex = $tokens->getPrevNonWhitespace($index);
         if (null === $lastTokenIndex) {
             continue;
         }
         $endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
         $nextNonWhiteSpace = $tokens->getNextMeaningfulToken($endParenthesisIndex);
         if (null !== $nextNonWhiteSpace && $tokens[$nextNonWhiteSpace]->equals('?') && $tokens[$lastTokenIndex]->isGivenKind($languageConstructionTokens)) {
             continue;
         }
         if ($tokens[$lastTokenIndex]->isGivenKind($functionyTokens)) {
             $this->fixFunctionCall($tokens, $index);
         } elseif ($tokens[$lastTokenIndex]->isGivenKind(T_STRING)) {
             $possibleDefinitionIndex = $tokens->getPrevMeaningfulToken($lastTokenIndex);
             if (!$tokens[$possibleDefinitionIndex]->isGivenKind(T_FUNCTION)) {
                 $this->fixFunctionCall($tokens, $index);
             }
         }
     }
     return $tokens->generateCode();
 }
開發者ID:Ryu0621,項目名稱:SaNaVi,代碼行數:29,代碼來源:FunctionCallSpaceFixer.php

示例10: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     for ($index = count($tokens) - 1; 0 <= $index; --$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(T_NAMESPACE)) {
             continue;
         }
         $beforeNamespace = $tokens[$index - 1];
         if (!$beforeNamespace->isWhitespace()) {
             if (!self::endsWithWhitespace($beforeNamespace->getContent())) {
                 $tokens->insertAt($index, new Token(array(T_WHITESPACE, "\n")));
             }
             continue;
         }
         $lastNewline = strrpos($beforeNamespace->getContent(), "\n");
         if (false === $lastNewline) {
             $beforeBeforeNamespace = $tokens[$index - 2];
             if (self::endsWithWhitespace($beforeBeforeNamespace->getContent())) {
                 $beforeNamespace->clear();
             } else {
                 $beforeNamespace->setContent(' ');
             }
         } else {
             $beforeNamespace->setContent(substr($beforeNamespace->getContent(), 0, $lastNewline + 1));
         }
     }
     return $tokens->generateCode();
 }
開發者ID:Flesh192,項目名稱:magento,代碼行數:32,代碼來源:NamespaceNoLeadingWhitespaceFixer.php

示例11: fix

public function fix(\SplFileInfo $file, $content)
{
$tokens = Tokens::fromCode($content);

if (!$tokens->isMonolithicPhp()) {
return $content;
}

$closeTags = $tokens->findGivenKind(T_CLOSE_TAG);

if (empty($closeTags)) {
return $content;
}

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(';'));
}

return $tokens->generateCode();
}
開發者ID:Ryu0621,項目名稱:SaNaVi,代碼行數:28,代碼來源:PhpClosingTagFixer.php

示例12: fix

 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $count = $tokens->count();
     if (0 === $count) {
         return '';
     }
     $token = $tokens[$count - 1];
     if ($token->isGivenKind(array(T_INLINE_HTML, T_CLOSE_TAG, T_OPEN_TAG))) {
         return $content;
     }
     $isSingleLineComment = function (Token $token) {
         return $token->isComment() && '/*' !== substr($token->getContent(), 0, 2);
     };
     $clearSingleLineComment = function (Token $token) {
         $content = $token->getContent();
         $content = rtrim($content, "\n") . "\n";
         $token->setContent($content);
     };
     if ($token->isWhitespace()) {
         if ($count > 1 && $isSingleLineComment($tokens[$count - 2])) {
             $clearSingleLineComment($tokens[$count - 2]);
             $token->clear();
         } else {
             $lineBreak = false === strrpos($token->getContent(), "\r") ? "\n" : "\r\n";
             $token->setContent($lineBreak);
         }
     } elseif ($isSingleLineComment($token)) {
         $clearSingleLineComment($token);
     } else {
         $tokens->insertAt($count, new Token(array(T_WHITESPACE, "\n")));
     }
     return $tokens->generateCode();
 }
開發者ID:Ryu0621,項目名稱:SaNaVi,代碼行數:34,代碼來源:EofEndingFixer.php

示例13: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(T_START_HEREDOC) || false !== strpos($token->getContent(), "'")) {
             continue;
         }
         if ($tokens[$index + 1]->isGivenKind(T_END_HEREDOC)) {
             $this->convertToNowdoc($token);
             continue;
         }
         if (!$tokens[$index + 1]->isGivenKind(T_ENCAPSED_AND_WHITESPACE) || !$tokens[$index + 2]->isGivenKind(T_END_HEREDOC)) {
             continue;
         }
         $content = $tokens[$index + 1]->getContent();
         // regex: odd number of backslashes, not followed by dollar
         if (preg_match('/(?<!\\\\)(?:\\\\{2})*\\\\(?![$\\\\])/', $content)) {
             continue;
         }
         $this->convertToNowdoc($token);
         $content = str_replace(array('\\\\', '\\$'), array('\\', '$'), $content);
         $tokens[$index + 1]->setContent($content);
     }
     return $tokens->generateCode();
 }
開發者ID:Doability,項目名稱:magento2dev,代碼行數:28,代碼來源:HeredocToNowdocFixer.php

示例14: fix

public function fix(\SplFileInfo $file, $content)
{
static $map = null;

if (null === $map) {
$trueToken = new Token(array(T_STRING, 'true'));

$map = array(
'in_array' => array(null, null, $trueToken),
'base64_decode' => array(null, $trueToken),
'array_search' => array(null, null, $trueToken),
'array_keys' => array(null, null, $trueToken),
'mb_detect_encoding' => array(null, array(new Token(array(T_STRING, 'mb_detect_order')), new Token('('), new Token(')')), $trueToken),
);
}

$tokens = Tokens::fromCode($content);

for ($index = $tokens->count() - 1; 0 <= $index; --$index) {
$token = $tokens[$index];

if ($token->isGivenKind(T_STRING) && isset($map[$token->getContent()])) {
$this->fixFunction($tokens, $index, $map[$token->getContent()]);
}
}

return $tokens->generateCode();
}
開發者ID:Ryu0621,項目名稱:SaNaVi,代碼行數:28,代碼來源:StrictParamFixer.php

示例15: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     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, ' ', $tokens[$iter]->getLine())));
             }
         }
     }
     return $tokens->generateCode();
 }
開發者ID:gmoigneu,項目名稱:magento2-platformsh,代碼行數:36,代碼來源:FunctionTypehintSpaceFixer.php


注:本文中的Symfony\CS\Tokenizer\Tokens::fromCode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。