本文整理汇总了PHP中PhpCsFixer\Tokenizer\Tokens::generateCode方法的典型用法代码示例。如果您正苦于以下问题:PHP Tokens::generateCode方法的具体用法?PHP Tokens::generateCode怎么用?PHP Tokens::generateCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpCsFixer\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::generateCode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
示例2: replacePlaceholder
/**
* Look for group of placeholders, and provide vertical alignment.
*
* @param Tokens $tokens
*
* @return string
*/
protected function replacePlaceholder(Tokens $tokens)
{
$tmpCode = $tokens->generateCode();
for ($j = 0; $j <= $this->deepestLevel; ++$j) {
$placeholder = sprintf(self::ALIGNABLE_PLACEHOLDER, $j);
if (false === strpos($tmpCode, $placeholder)) {
continue;
}
$lines = explode("\n", $tmpCode);
$linesWithPlaceholder = array();
$blockSize = 0;
$linesWithPlaceholder[$blockSize] = array();
foreach ($lines as $index => $line) {
if (substr_count($line, $placeholder) > 0) {
$linesWithPlaceholder[$blockSize][] = $index;
} else {
++$blockSize;
$linesWithPlaceholder[$blockSize] = array();
}
}
foreach ($linesWithPlaceholder as $group) {
if (count($group) < 1) {
continue;
}
$rightmostSymbol = 0;
foreach ($group as $index) {
$rightmostSymbol = max($rightmostSymbol, strpos(utf8_decode($lines[$index]), $placeholder));
}
foreach ($group as $index) {
$line = $lines[$index];
$currentSymbol = strpos(utf8_decode($line), $placeholder);
$delta = abs($rightmostSymbol - $currentSymbol);
if ($delta > 0) {
$line = str_replace($placeholder, str_repeat(' ', $delta) . $placeholder, $line);
$lines[$index] = $line;
}
}
}
$tmpCode = str_replace($placeholder, '', implode("\n", $lines));
}
return $tmpCode;
}
示例3: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
// [Structure] Use the UNIX line ending character (0x0A) to end lines
$tokens->setCode(str_replace("\r\n", "\n", $tokens->generateCode()));
}