update v 1.0.7.5
This commit is contained in:
@@ -75,10 +75,25 @@ abstract class PrettyPrinterAbstract
|
||||
);
|
||||
|
||||
protected $noIndentToken;
|
||||
protected $docStringEndToken;
|
||||
protected $canUseSemicolonNamespaces;
|
||||
protected $options;
|
||||
|
||||
public function __construct() {
|
||||
/**
|
||||
* Creates a pretty printer instance using the given options.
|
||||
*
|
||||
* Supported options:
|
||||
* * bool $shortArraySyntax = false: Whether to use [] instead of array() as the default array
|
||||
* syntax, if the node does not specify a format.
|
||||
*
|
||||
* @param array $options Dictionary of formatting options
|
||||
*/
|
||||
public function __construct(array $options = []) {
|
||||
$this->noIndentToken = '_NO_INDENT_' . mt_rand();
|
||||
$this->docStringEndToken = '_DOC_STRING_END_' . mt_rand();
|
||||
|
||||
$defaultOptions = ['shortArraySyntax' => false];
|
||||
$this->options = $options + $defaultOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,7 +106,7 @@ abstract class PrettyPrinterAbstract
|
||||
public function prettyPrint(array $stmts) {
|
||||
$this->preprocessNodes($stmts);
|
||||
|
||||
return ltrim(str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($stmts, false)));
|
||||
return ltrim($this->handleMagicTokens($this->pStmts($stmts, false)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,7 +117,7 @@ abstract class PrettyPrinterAbstract
|
||||
* @return string Pretty printed node
|
||||
*/
|
||||
public function prettyPrintExpr(Expr $node) {
|
||||
return str_replace("\n" . $this->noIndentToken, "\n", $this->p($node));
|
||||
return $this->handleMagicTokens($this->p($node));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,13 +128,17 @@ abstract class PrettyPrinterAbstract
|
||||
* @return string Pretty printed statements
|
||||
*/
|
||||
public function prettyPrintFile(array $stmts) {
|
||||
$p = rtrim($this->prettyPrint($stmts));
|
||||
if (!$stmts) {
|
||||
return "<?php\n\n";
|
||||
}
|
||||
|
||||
$p = preg_replace('/^\?>\n?/', '', $p, -1, $count);
|
||||
$p = preg_replace('/<\?php$/', '', $p);
|
||||
$p = "<?php\n\n" . $this->prettyPrint($stmts);
|
||||
|
||||
if (!$count) {
|
||||
$p = "<?php\n\n" . $p;
|
||||
if ($stmts[0] instanceof Stmt\InlineHTML) {
|
||||
$p = preg_replace('/^<\?php\s+\?>\n?/', '', $p);
|
||||
}
|
||||
if ($stmts[count($stmts) - 1] instanceof Stmt\InlineHTML) {
|
||||
$p = preg_replace('/<\?php$/', '', rtrim($p));
|
||||
}
|
||||
|
||||
return $p;
|
||||
@@ -140,6 +159,17 @@ abstract class PrettyPrinterAbstract
|
||||
}
|
||||
}
|
||||
|
||||
protected function handleMagicTokens($str) {
|
||||
// Drop no-indent tokens
|
||||
$str = str_replace($this->noIndentToken, '', $str);
|
||||
|
||||
// Replace doc-string-end tokens with nothing or a newline
|
||||
$str = str_replace($this->docStringEndToken . ";\n", ";\n", $str);
|
||||
$str = str_replace($this->docStringEndToken, "\n", $str);
|
||||
|
||||
return $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pretty prints an array of nodes (statements) and indents them optionally.
|
||||
*
|
||||
@@ -151,10 +181,15 @@ abstract class PrettyPrinterAbstract
|
||||
protected function pStmts(array $nodes, $indent = true) {
|
||||
$result = '';
|
||||
foreach ($nodes as $node) {
|
||||
$result .= "\n"
|
||||
. $this->pComments($node->getAttribute('comments', array()))
|
||||
. $this->p($node)
|
||||
. ($node instanceof Expr ? ';' : '');
|
||||
$comments = $node->getAttribute('comments', array());
|
||||
if ($comments) {
|
||||
$result .= "\n" . $this->pComments($comments);
|
||||
if ($node instanceof Stmt\Nop) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : '');
|
||||
}
|
||||
|
||||
if ($indent) {
|
||||
@@ -252,19 +287,26 @@ abstract class PrettyPrinterAbstract
|
||||
*
|
||||
* @param string $string Not to be indented string
|
||||
*
|
||||
* @return mixed String marked with $this->noIndentToken's.
|
||||
* @return string String marked with $this->noIndentToken's.
|
||||
*/
|
||||
protected function pNoIndent($string) {
|
||||
return str_replace("\n", "\n" . $this->noIndentToken, $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints reformatted text of the passed comments.
|
||||
*
|
||||
* @param Comment[] $comments List of comments
|
||||
*
|
||||
* @return string Reformatted text of comments
|
||||
*/
|
||||
protected function pComments(array $comments) {
|
||||
$result = '';
|
||||
$formattedComments = [];
|
||||
|
||||
foreach ($comments as $comment) {
|
||||
$result .= $comment->getReformattedText() . "\n";
|
||||
$formattedComments[] = $comment->getReformattedText();
|
||||
}
|
||||
|
||||
return $result;
|
||||
return implode("\n", $formattedComments);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user