mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-26 20:04:48 +01:00
Improve pretty printing of empty statement lists
The pStmts() method now also includes the leading \n, however only if the statement list is non-empty.
This commit is contained in:
parent
b3332184cf
commit
91f6880734
@ -445,7 +445,7 @@ class Standard extends PrettyPrinterAbstract
|
||||
. 'function ' . ($node->byRef ? '&' : '')
|
||||
. '(' . $this->pCommaSeparated($node->params) . ')'
|
||||
. (!empty($node->uses) ? ' use(' . $this->pCommaSeparated($node->uses) . ')': '')
|
||||
. ' {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. ' {' . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pExpr_ClosureUse(Expr\ClosureUse $node) {
|
||||
@ -488,10 +488,10 @@ class Standard extends PrettyPrinterAbstract
|
||||
|
||||
public function pStmt_Namespace(Stmt\Namespace_ $node) {
|
||||
if ($this->canUseSemicolonNamespaces) {
|
||||
return 'namespace ' . $this->p($node->name) . ';' . "\n\n" . $this->pStmts($node->stmts, false);
|
||||
return 'namespace ' . $this->p($node->name) . ';' . "\n" . $this->pStmts($node->stmts, false);
|
||||
} else {
|
||||
return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
|
||||
. ' {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. ' {' . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
}
|
||||
|
||||
@ -510,7 +510,7 @@ class Standard extends PrettyPrinterAbstract
|
||||
public function pStmt_Interface(Stmt\Interface_ $node) {
|
||||
return 'interface ' . $node->name
|
||||
. (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '')
|
||||
. "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_Class(Stmt\Class_ $node) {
|
||||
@ -518,19 +518,19 @@ class Standard extends PrettyPrinterAbstract
|
||||
. 'class ' . $node->name
|
||||
. (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '')
|
||||
. (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
|
||||
. "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_Trait(Stmt\Trait_ $node) {
|
||||
return 'trait ' . $node->name
|
||||
. "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_TraitUse(Stmt\TraitUse $node) {
|
||||
return 'use ' . $this->pCommaSeparated($node->traits)
|
||||
. (empty($node->adaptations)
|
||||
? ';'
|
||||
: ' {' . "\n" . $this->pStmts($node->adaptations) . "\n" . '}');
|
||||
: ' {' . $this->pStmts($node->adaptations) . "\n" . '}');
|
||||
}
|
||||
|
||||
public function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node) {
|
||||
@ -560,7 +560,7 @@ class Standard extends PrettyPrinterAbstract
|
||||
. 'function ' . ($node->byRef ? '&' : '') . $node->name
|
||||
. '(' . $this->pCommaSeparated($node->params) . ')'
|
||||
. (null !== $node->stmts
|
||||
? "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}'
|
||||
? "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}'
|
||||
: ';');
|
||||
}
|
||||
|
||||
@ -571,7 +571,7 @@ class Standard extends PrettyPrinterAbstract
|
||||
public function pStmt_Function(Stmt\Function_ $node) {
|
||||
return 'function ' . ($node->byRef ? '&' : '') . $node->name
|
||||
. '(' . $this->pCommaSeparated($node->params) . ')'
|
||||
. "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_Const(Stmt\Const_ $node) {
|
||||
@ -580,7 +580,7 @@ class Standard extends PrettyPrinterAbstract
|
||||
|
||||
public function pStmt_Declare(Stmt\Declare_ $node) {
|
||||
return 'declare (' . $this->pCommaSeparated($node->declares) . ') {'
|
||||
. "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_DeclareDeclare(Stmt\DeclareDeclare $node) {
|
||||
@ -591,18 +591,18 @@ class Standard extends PrettyPrinterAbstract
|
||||
|
||||
public function pStmt_If(Stmt\If_ $node) {
|
||||
return 'if (' . $this->p($node->cond) . ') {'
|
||||
. "\n" . $this->pStmts($node->stmts) . "\n" . '}'
|
||||
. $this->pStmts($node->stmts) . "\n" . '}'
|
||||
. $this->pImplode($node->elseifs)
|
||||
. (null !== $node->else ? $this->p($node->else) : '');
|
||||
}
|
||||
|
||||
public function pStmt_ElseIf(Stmt\ElseIf_ $node) {
|
||||
return ' elseif (' . $this->p($node->cond) . ') {'
|
||||
. "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_Else(Stmt\Else_ $node) {
|
||||
return ' else {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
return ' else {' . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_For(Stmt\For_ $node) {
|
||||
@ -610,47 +610,47 @@ class Standard extends PrettyPrinterAbstract
|
||||
. $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '')
|
||||
. $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '')
|
||||
. $this->pCommaSeparated($node->loop)
|
||||
. ') {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. ') {' . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_Foreach(Stmt\Foreach_ $node) {
|
||||
return 'foreach (' . $this->p($node->expr) . ' as '
|
||||
. (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '')
|
||||
. ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {'
|
||||
. "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_While(Stmt\While_ $node) {
|
||||
return 'while (' . $this->p($node->cond) . ') {'
|
||||
. "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_Do(Stmt\Do_ $node) {
|
||||
return 'do {' . "\n" . $this->pStmts($node->stmts) . "\n"
|
||||
return 'do {' . $this->pStmts($node->stmts) . "\n"
|
||||
. '} while (' . $this->p($node->cond) . ');';
|
||||
}
|
||||
|
||||
public function pStmt_Switch(Stmt\Switch_ $node) {
|
||||
return 'switch (' . $this->p($node->cond) . ') {'
|
||||
. "\n" . $this->pStmts($node->cases) . "\n" . '}';
|
||||
. $this->pStmts($node->cases) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_Case(Stmt\Case_ $node) {
|
||||
return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
|
||||
. ($node->stmts ? "\n" . $this->pStmts($node->stmts) : '');
|
||||
. $this->pStmts($node->stmts);
|
||||
}
|
||||
|
||||
public function pStmt_TryCatch(Stmt\TryCatch $node) {
|
||||
return 'try {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}'
|
||||
return 'try {' . $this->pStmts($node->stmts) . "\n" . '}'
|
||||
. $this->pImplode($node->catches)
|
||||
. ($node->finallyStmts !== null
|
||||
? ' finally {' . "\n" . $this->pStmts($node->finallyStmts) . "\n" . '}'
|
||||
? ' finally {' . $this->pStmts($node->finallyStmts) . "\n" . '}'
|
||||
: '');
|
||||
}
|
||||
|
||||
public function pStmt_Catch(Stmt\Catch_ $node) {
|
||||
return ' catch (' . $this->p($node->type) . ' $' . $node->var . ') {'
|
||||
. "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
. $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_Break(Stmt\Break_ $node) {
|
||||
|
@ -87,7 +87,7 @@ abstract class PrettyPrinterAbstract
|
||||
public function prettyPrint(array $stmts) {
|
||||
$this->preprocessNodes($stmts);
|
||||
|
||||
return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($stmts, false));
|
||||
return trim(str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($stmts, false)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,7 +109,7 @@ abstract class PrettyPrinterAbstract
|
||||
* @return string Pretty printed statements
|
||||
*/
|
||||
public function prettyPrintFile(array $stmts) {
|
||||
$p = trim($this->prettyPrint($stmts));
|
||||
$p = $this->prettyPrint($stmts);
|
||||
|
||||
$p = preg_replace('/^\?>\n?/', '', $p, -1, $count);
|
||||
$p = preg_replace('/<\?php$/', '', $p);
|
||||
@ -145,21 +145,18 @@ abstract class PrettyPrinterAbstract
|
||||
* @return string Pretty printed statements
|
||||
*/
|
||||
protected function pStmts(array $nodes, $indent = true) {
|
||||
$pNodes = array();
|
||||
$result = '';
|
||||
foreach ($nodes as $node) {
|
||||
$pNodes[] = $this->pComments($node->getAttribute('comments', array()))
|
||||
. $this->p($node)
|
||||
. ($node instanceof Expr ? ';' : '');
|
||||
$result .= "\n"
|
||||
. $this->pComments($node->getAttribute('comments', array()))
|
||||
. $this->p($node)
|
||||
. ($node instanceof Expr ? ';' : '');
|
||||
}
|
||||
|
||||
if ($indent) {
|
||||
return ' ' . preg_replace(
|
||||
'~\n(?!$|' . $this->noIndentToken . ')~',
|
||||
"\n" . ' ',
|
||||
implode("\n", $pNodes)
|
||||
);
|
||||
return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
|
||||
} else {
|
||||
return implode("\n", $pNodes);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,27 +6,23 @@ namespace Foo;
|
||||
|
||||
function foo()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
namespace Bar;
|
||||
|
||||
function bar()
|
||||
{
|
||||
|
||||
}
|
||||
-----
|
||||
namespace Foo;
|
||||
|
||||
function foo()
|
||||
{
|
||||
|
||||
}
|
||||
namespace Bar;
|
||||
|
||||
function bar()
|
||||
{
|
||||
|
||||
}
|
||||
-----
|
||||
<?php
|
||||
@ -34,25 +30,21 @@ function bar()
|
||||
namespace Foo {
|
||||
function foo()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
function glob() {
|
||||
|
||||
}
|
||||
}
|
||||
-----
|
||||
namespace Foo {
|
||||
function foo()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
namespace {
|
||||
function glob()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -9,17 +9,13 @@ function test(&... $foo) {}
|
||||
-----
|
||||
function test($a, &$b, Type $c, Type &$c, Type &... $d)
|
||||
{
|
||||
|
||||
}
|
||||
function test(... $foo)
|
||||
{
|
||||
|
||||
}
|
||||
function test(Type ... $foo)
|
||||
{
|
||||
|
||||
}
|
||||
function test(&... $foo)
|
||||
{
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user