本文整理汇总了PHP中PhpCsFixer\Tokenizer\Tokens::generatePartialCode方法的典型用法代码示例。如果您正苦于以下问题:PHP Tokens::generatePartialCode方法的具体用法?PHP Tokens::generatePartialCode怎么用?PHP Tokens::generatePartialCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpCsFixer\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::generatePartialCode方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
}
示例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(';', 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);
}
}
示例3: 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->getNextMeaningfulToken($index);
$namespaceEndIndex = $tokens->getNextTokenOfKind($index, array(';'));
$namespace = trim($tokens->generatePartialCode($namespaceIndex, $namespaceEndIndex - 1));
} elseif ($token->isClassy()) {
if (null !== $classyName) {
return;
}
$classyIndex = $tokens->getNextMeaningfulToken($index);
$classyName = $tokens[$classyIndex]->getContent();
}
}
if (null === $classyName) {
return;
}
if (false !== $namespace) {
$normNamespace = str_replace('\\', '/', $namespace);
$path = str_replace('\\', '/', $file->getRealPath());
$dir = dirname($path);
if (isset($this->configuration['dir'])) {
$dir = substr($dir, strlen(realpath($this->configuration['dir'])) + 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)) . str_replace('/', '\\', $dir);
$newNamespace = Tokens::fromCode('<?php namespace ' . $namespace . ';');
$newNamespace[0]->clear();
$newNamespace[1]->clear();
$newNamespace[2]->clear();
$newNamespace->clearEmptyTokens();
$tokens->insertAt($namespaceIndex, $newNamespace);
}
} else {
$normClass = str_replace('_', '/', $classyName);
$path = str_replace('\\', '/', $file->getRealPath());
$filename = substr($path, -strlen($normClass) - 4, -4);
if ($normClass !== $filename && strtolower($normClass) === strtolower($filename)) {
$tokens[$classyIndex]->setContent(str_replace('/', '_', $filename));
}
}
}
示例4: 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) . "'")));
}
示例5: getNamespaceUseDeclarations
private function getNamespaceUseDeclarations(Tokens $tokens, array $useIndexes)
{
$uses = array();
foreach ($useIndexes as $index) {
$declarationEndIndex = $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)));
$declarationContent = $tokens->generatePartialCode($index + 1, $declarationEndIndex - 1);
if (false !== strpos($declarationContent, ',') || 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 {
list($fullName, $shortName) = $declarationParts;
$declarationParts = explode('\\', $fullName);
$aliased = $shortName !== end($declarationParts);
}
$shortName = trim($shortName);
$uses[$shortName] = array('fullName' => trim($fullName), 'shortName' => $shortName, 'aliased' => $aliased, 'start' => $index, 'end' => $declarationEndIndex);
}
return $uses;
}
示例6: 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('aliased' => $aliased, 'end' => $declarationEndIndex, 'fullName' => trim($fullName), 'shortName' => $shortName, 'start' => $index);
}
return $uses;
}