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