本文整理汇总了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;
}
示例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);
}
}
示例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;
}
示例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;
}
示例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, '/', '_'));
}
}
}
示例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) . "'")));
}
示例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;
}