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


PHP Tokens::generatePartialCode方法代码示例

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


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

示例1: 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;
 }
开发者ID:bgotink,项目名称:PHP-CS-Fixer,代码行数:28,代码来源:UnusedUseFixer.php

示例2: 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:rafwlaz,项目名称:PHP-CS-Fixer,代码行数:28,代码来源:MultipleUseFixer.php

示例3: 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;
 }
开发者ID:akeneo,项目名称:php-coupling-detector,代码行数:22,代码来源:NamespaceExtractor.php

示例4: extract

 /**
  * Copy/paste from a private method of Symfony\CS\Fixer\Symfony\UnusedUseFixer.
  *
  * @param Tokens $tokens
  *
  * @return array
  */
 public function extract(Tokens $tokens)
 {
     $uses = array();
     $useIndexes = $tokens->getImportUseIndexes();
     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;
         } else {
             $fullName = $declarationParts[0];
         }
         $uses[] = trim($fullName);
     }
     return $uses;
 }
开发者ID:akeneo,项目名称:php-coupling-detector,代码行数:29,代码来源:UseDeclarationsExtractor.php

示例5: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $namespace = false;
     $namespaceIndex = 0;
     $namespaceEndIndex = 0;
     $classyName = null;
     $classyIndex = 0;
     foreach ($tokens as $index => $token) {
         if ($token->isGivenKind(T_NAMESPACE)) {
             if (false !== $namespace) {
                 return;
             }
             $namespaceIndex = $tokens->getNextNonWhitespace($index);
             $namespaceEndIndex = $tokens->getNextTokenOfKind($index, array(';'));
             $namespace = trim($tokens->generatePartialCode($namespaceIndex, $namespaceEndIndex - 1));
         } elseif ($token->isClassy()) {
             if (null !== $classyName) {
                 return;
             }
             $classyIndex = $tokens->getNextNonWhitespace($index);
             $classyName = $tokens[$classyIndex]->getContent();
         }
     }
     if (null === $classyName) {
         return;
     }
     if (false !== $namespace) {
         $normNamespace = strtr($namespace, '\\', '/');
         $path = strtr($file->getRealPath(), '\\', '/');
         $dir = dirname($path);
         if ($this->config) {
             $dir = substr($dir, strlen(realpath($this->config->getDir())) + 1);
             if (strlen($normNamespace) > strlen($dir)) {
                 if ('' !== $dir) {
                     $normNamespace = substr($normNamespace, -strlen($dir));
                 } else {
                     $normNamespace = '';
                 }
             }
         }
         $dir = substr($dir, -strlen($normNamespace));
         if (false === $dir) {
             $dir = '';
         }
         $filename = basename($path, '.php');
         if ($classyName !== $filename) {
             $tokens[$classyIndex]->setContent($filename);
         }
         if ($normNamespace !== $dir && strtolower($normNamespace) === strtolower($dir)) {
             for ($i = $namespaceIndex; $i <= $namespaceEndIndex; ++$i) {
                 $tokens[$i]->clear();
             }
             $namespace = substr($namespace, 0, -strlen($dir)) . strtr($dir, '/', '\\');
             $newNamespace = Tokens::fromCode('<?php namespace ' . $namespace . ';');
             $newNamespace[0]->clear();
             $newNamespace[1]->clear();
             $newNamespace[2]->clear();
             $newNamespace->clearEmptyTokens();
             $tokens->insertAt($namespaceIndex, $newNamespace);
         }
     } else {
         $normClass = strtr($classyName, '_', '/');
         $path = strtr($file->getRealPath(), '\\', '/');
         $filename = substr($path, -strlen($normClass) - 4, -4);
         if ($normClass !== $filename && strtolower($normClass) === strtolower($filename)) {
             $tokens[$classyIndex]->setContent(strtr($filename, '/', '_'));
         }
     }
 }
开发者ID:rybakit,项目名称:PHP-CS-Fixer,代码行数:72,代码来源:Psr0Fixer.php

示例6: replaceClassKeyword

 /**
  * @param Tokens $tokens
  * @param int    $classIndex
  */
 private function replaceClassKeyword(Tokens $tokens, $classIndex)
 {
     $classEndIndex = $classIndex - 2;
     $classBeginIndex = $classEndIndex;
     while ($tokens[--$classBeginIndex]->isGivenKind(array(T_NS_SEPARATOR, T_STRING))) {
     }
     ++$classBeginIndex;
     $classString = $tokens->generatePartialCode($classBeginIndex, $classEndIndex);
     $classImport = false;
     foreach ($this->imports as $alias => $import) {
         if ($classString === $alias) {
             $classImport = $import;
             break;
         }
         $classStringArray = explode('\\', $classString);
         $namespaceToTest = $classStringArray[0];
         if (0 === strcmp($namespaceToTest, substr($import, -strlen($namespaceToTest)))) {
             $classImport = $import;
             break;
         }
     }
     $tokens->clearRange($classBeginIndex, $classIndex);
     $tokens->insertAt($classBeginIndex, new Token(array(T_CONSTANT_ENCAPSED_STRING, "'" . $this->makeClassFQN($classImport, $classString) . "'")));
 }
开发者ID:Doability,项目名称:magento2dev,代码行数:28,代码来源:ClassKeywordRemoveFixer.php

示例7: 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);


 
 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(
'aliased' => $aliased,
'end' => $declarationEndIndex,
'fullName' => trim($fullName),
'shortName' => $shortName,
'start' => $index,
);
}

return $uses;
}
开发者ID:Ryu0621,项目名称:SaNaVi,代码行数:41,代码来源:UnusedUseFixer.php


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