当前位置: 首页>>代码示例>>PHP>>正文


PHP Tokens::fromCode方法代码示例

本文整理汇总了PHP中PhpCsFixer\Tokenizer\Tokens::fromCode方法的典型用法代码示例。如果您正苦于以下问题:PHP Tokens::fromCode方法的具体用法?PHP Tokens::fromCode怎么用?PHP Tokens::fromCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PhpCsFixer\Tokenizer\Tokens的用法示例。


在下文中一共展示了Tokens::fromCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: 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);
     }
 }
开发者ID:thekabal,项目名称:tki,代码行数:28,代码来源:SingleImportPerStatementFixer.php

示例2: 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:thekabal,项目名称:tki,代码行数:31,代码来源:OrderedImportsFixer.php

示例3: testProcess

 /**
  * @dataProvider provideProcessCases
  */
 public function testProcess($source, array $expectedTokens)
 {
     $tokens = Tokens::fromCode($source);
     foreach ($expectedTokens as $index => $expectedToken) {
         $token = $tokens[$index];
         $this->assertSame($expectedToken[1], $token->getContent());
         $this->assertSame($expectedToken[0], $token->getId());
     }
 }
开发者ID:infinitely-young,项目名称:PHP-CS-Fixer,代码行数:12,代码来源:WhitespacyCommentTransformerTest.php

示例4: lintSource

 /**
  * {@inheritdoc}
  */
 public function lintSource($source)
 {
     try {
         // it will throw ParseError on syntax error
         // if not, it will cache the tokenized version of code, which is great for Runner
         Tokens::fromCode($source);
         return new TokenizerLintingResult();
     } catch (\ParseError $e) {
         return new TokenizerLintingResult($e);
     }
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:14,代码来源:TokenizerLinter.php

示例5: doTest

 protected function doTest($source, array $expectedTokens = array(), array $observedKinds = array())
 {
     $tokens = Tokens::fromCode($source);
     $this->assertSame(count($expectedTokens), array_sum(array_map(function ($item) {
         return count($item);
     }, $tokens->findGivenKind(array_unique(array_merge($observedKinds, array_values($expectedTokens)))))));
     foreach ($expectedTokens as $index => $tokenId) {
         $this->assertSame(CT::has($tokenId) ? CT::getName($tokenId) : token_name($tokenId), $tokens[$index]->getName(), sprintf('Token kind should be the same at index %d.', $index));
         $this->assertSame($tokenId, $tokens[$index]->getId());
     }
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:11,代码来源:AbstractTransformerTestCase.php

示例6: testCommentBlockStartDetection

 /**
  * @param int    $expected
  * @param string $code
  * @param int    $index
  *
  * @dataProvider provideCommentBlockStartDetectionCases
  */
 public function testCommentBlockStartDetection($expected, $code, $index)
 {
     Tokens::clearCache();
     $tokens = Tokens::fromCode($code);
     $fixer = $this->getFixer();
     $method = new \ReflectionMethod($fixer, 'findCommentBlockStart');
     $method->setAccessible(true);
     if ($expected !== ($result = $method->invoke($fixer, $tokens, $index))) {
         $this->fail(sprintf('Expected index %d (%s) got index %d (%s).', $expected, $tokens[$expected]->toJson(), $result, $tokens[$result]->toJson()));
     }
 }
开发者ID:thekabal,项目名称:tki,代码行数:18,代码来源:MethodSeparationFixerTest.php

示例7: doTest

 protected function doTest($source, array $expectedTokens = array())
 {
     $tokens = Tokens::fromCode($source);
     $this->assertSame(count($expectedTokens), array_sum(array_map(function ($item) {
         return count($item);
     }, $tokens->findGivenKind(array_map(function ($name) {
         return constant($name);
     }, $expectedTokens)))));
     foreach ($expectedTokens as $index => $name) {
         $this->assertSame($name, $tokens[$index]->getName());
         $this->assertSame(constant($name), $tokens[$index]->getId());
     }
 }
开发者ID:cryode,项目名称:PHP-CS-Fixer,代码行数:13,代码来源:AbstractTransformerTestCase.php

示例8: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokensOrg)
 {
     $content = $tokensOrg->generateCode();
     // replace all <? with <?php to replace all short open tags even without short_open_tag option enabled
     $newContent = preg_replace('/<\\?(\\s|$)/', '<?php$1', $content, -1, $count);
     if (!$count) {
         return;
     }
     /* the following code is magic to revert previous replacements which should NOT be replaced, for example incorrectly replacing
      * > echo '<? ';
      * with
      * > echo '<?php ';
      */
     $tokens = Tokens::fromCode($newContent);
     $tokensOldContent = '';
     $tokensOldContentLength = 0;
     foreach ($tokens as $token) {
         if ($token->isGivenKind(T_OPEN_TAG)) {
             $tokenContent = $token->getContent();
             if ('<?php' !== substr($content, $tokensOldContentLength, 5)) {
                 $tokenContent = '<? ';
             }
             $tokensOldContent .= $tokenContent;
             $tokensOldContentLength += strlen($tokenContent);
             continue;
         }
         if ($token->isGivenKind(array(T_COMMENT, T_DOC_COMMENT, T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_STRING))) {
             $tokenContent = '';
             $tokenContentLength = 0;
             $parts = explode('<?php', $token->getContent());
             $iLast = count($parts) - 1;
             foreach ($parts as $i => $part) {
                 $tokenContent .= $part;
                 $tokenContentLength += strlen($part);
                 if ($i !== $iLast) {
                     if ('<?php' === substr($content, $tokensOldContentLength + $tokenContentLength, 5)) {
                         $tokenContent .= '<?php';
                         $tokenContentLength += 5;
                     } else {
                         $tokenContent .= '<?';
                         $tokenContentLength += 2;
                     }
                 }
             }
             $token->setContent($tokenContent);
         }
         $tokensOldContent .= $token->getContent();
         $tokensOldContentLength += strlen($token->getContent());
     }
     $tokensOrg->overrideRange(0, $tokensOrg->count() - 1, $tokens);
 }
开发者ID:infinitely-young,项目名称:PHP-CS-Fixer,代码行数:54,代码来源:FullOpeningTagFixer.php

示例9: 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(';', array(T_CLOSE_TAG)));
         $previous = $tokens->getPrevMeaningfulToken($endIndex);
         if ($tokens[$previous]->equals('}')) {
             $start = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $previous, false);
             $declarationContent = $tokens->generatePartialCode($start + 1, $previous - 1);
             $prefix = '';
             for ($i = $index + 1; $i < $start; ++$i) {
                 $prefix .= $tokens[$i]->getContent();
             }
             $prefix = ' ' . ltrim($prefix);
         } else {
             $declarationContent = $tokens->generatePartialCode($index + 1, $endIndex - 1);
             $prefix = ' ';
         }
         $declarationParts = explode(',', $declarationContent);
         if (1 === count($declarationParts)) {
             continue;
         }
         $declarationContent = array();
         foreach ($declarationParts as $declarationPart) {
             $declarationContent[] = 'use' . $prefix . trim($declarationPart) . ';';
         }
         $declarationContent = implode("\n" . $this->detectIndent($tokens, $index), $declarationContent);
         for ($i = $index; $i < $endIndex; ++$i) {
             $tokens[$i]->clear();
         }
         if ($tokens[$endIndex]->equals(';')) {
             $tokens[$endIndex]->clear();
         }
         $declarationTokens = Tokens::fromCode('<?php ' . $declarationContent);
         $declarationTokens[0]->clear();
         $declarationTokens->clearEmptyTokens();
         $tokens->insertAt($index, $declarationTokens);
     }
 }
开发者ID:phansys,项目名称:PHP-CS-Fixer,代码行数:43,代码来源:SingleImportPerStatementFixer.php

示例10: supports

 /**
  * {@inheritdoc}
  */
 public function supports(\SplFileInfo $file)
 {
     if ($file instanceof StdinFileInfo) {
         return false;
     }
     $filenameParts = explode('.', $file->getBasename(), 2);
     if (!isset($filenameParts[1]) || 'php' !== $filenameParts[1] || 0 === preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $filenameParts[0]) || '__halt_compiler' === $filenameParts[0]) {
         return false;
     }
     try {
         $tokens = Tokens::fromCode(sprintf('<?php class %s {}', $filenameParts[0]));
         if ($tokens[3]->isKeyword() || $tokens[3]->isMagicConstant()) {
             // name can not be a class name - detected by PHP 5.x
             return false;
         }
     } catch (\ParseError $e) {
         // name can not be a class name - detected by PHP 7.x
         return false;
     }
     // ignore stubs/fixtures, since they are typically containing invalid files for various reasons
     return !preg_match('{[/\\\\](stub|fixture)s?[/\\\\]}i', $file->getRealPath());
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:25,代码来源:AbstractPsrAutoloadingFixer.php

示例11: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     $namespacesImports = $tokensAnalyzer->getImportUseIndexes(true);
     if (0 === count($namespacesImports)) {
         return;
     }
     $usesOrder = array();
     foreach ($namespacesImports as $uses) {
         $usesOrder = array_replace($usesOrder, $this->getNewOrder(array_reverse($uses), $tokens));
     }
     $usesOrder = array_reverse($usesOrder, true);
     $mapStartToEnd = array();
     foreach ($usesOrder as $use) {
         $mapStartToEnd[$use['startIndex']] = $use['endIndex'];
     }
     // 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->overrideRange($index, $mapStartToEnd[$index], $declarationTokens);
         if ($use['group']) {
             // a group import must start with `use` and cannot be part of comma separated import list
             $prev = $tokens->getPrevMeaningfulToken($index);
             if ($tokens[$prev]->equals(',')) {
                 $tokens[$prev]->setContent(';');
                 $tokens->insertAt($prev + 1, new Token(array(T_USE, 'use')));
                 if (!$tokens[$prev + 2]->isWhitespace()) {
                     $tokens->insertAt($prev + 2, new Token(array(T_WHITESPACE, ' ')));
                 }
             }
         }
     }
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:41,代码来源:OrderedImportsFixer.php

示例12: testBlockDetection

 /**
  * @dataProvider provideBlockDetectionCases
  */
 public function testBlockDetection(array $expected, $source, $index)
 {
     Tokens::clearCache();
     $tokens = Tokens::fromCode($source);
     $fixer = $this->getFixer();
     $method = new \ReflectionMethod($fixer, 'getPreviousBlock');
     $method->setAccessible(true);
     $result = $method->invoke($fixer, $tokens, $index);
     $this->assertSame($expected, $result);
 }
开发者ID:GrahamForks,项目名称:PHP-CS-Fixer,代码行数:13,代码来源:NoUselessElseFixerTest.php

示例13: testFindHeaderCommentInsertionIndex

 /**
  * @dataProvider provideFindHeaderCommentInsertionIndexCases
  */
 public function testFindHeaderCommentInsertionIndex($expected, $code, array $config)
 {
     Tokens::clearCache();
     $tokens = Tokens::fromCode($code);
     $fixer = $this->getFixer();
     $fixer->configure($config);
     $method = new \ReflectionMethod($fixer, 'findHeaderCommentInsertionIndex');
     $method->setAccessible(true);
     $this->assertSame($expected, $method->invoke($fixer, $tokens));
 }
开发者ID:GrahamForks,项目名称:PHP-CS-Fixer,代码行数:13,代码来源:HeaderCommentFixerTest.php

示例14: testCountArguments

 /**
  * @dataProvider provideCases
  */
 public function testCountArguments($code, $openIndex, $closeIndex, $argumentsCount)
 {
     $mock = new AccessibleObject($this->getMockForAbstractClass('\\PhpCsFixer\\AbstractFunctionReferenceFixer'));
     $this->assertSame($argumentsCount, $mock->countArguments(Tokens::fromCode($code), $openIndex, $closeIndex));
 }
开发者ID:thekabal,项目名称:tki,代码行数:8,代码来源:AbstractFunctionReferenceFixerTest.php

示例15: doTestClearTokens

 /**
  * @param string  $source
  * @param int[]   $indexes
  * @param Token[] $expected
  */
 private function doTestClearTokens($source, array $indexes, array $expected)
 {
     Tokens::clearCache();
     $tokens = Tokens::fromCode($source);
     foreach ($indexes as $index) {
         $tokens->clearTokenAndMergeSurroundingWhitespace($index);
     }
     $this->assertSame(count($expected), $tokens->count());
     foreach ($expected as $index => $expectedToken) {
         $token = $tokens[$index];
         $expectedPrototype = $expectedToken->getPrototype();
         if (is_array($expectedPrototype)) {
             unset($expectedPrototype[2]);
             // don't compare token lines as our token mutations don't deal with line numbers
         }
         $this->assertTrue($token->equals($expectedPrototype), sprintf('The token at index %d should be %s, got %s', $index, json_encode($expectedPrototype), $token->toJson()));
     }
 }
开发者ID:GrahamForks,项目名称:PHP-CS-Fixer,代码行数:23,代码来源:TokensTest.php


注:本文中的PhpCsFixer\Tokenizer\Tokens::fromCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。