mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-26 20:04:48 +01:00
Merge branch '3.x'
This commit is contained in:
commit
bc75ac2990
@ -446,12 +446,12 @@ class Standard extends PrettyPrinterAbstract
|
||||
|
||||
protected function pExpr_FuncCall(Expr\FuncCall $node) {
|
||||
return $this->pCallLhs($node->name)
|
||||
. '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
||||
}
|
||||
|
||||
protected function pExpr_MethodCall(Expr\MethodCall $node) {
|
||||
return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name)
|
||||
. '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
||||
}
|
||||
|
||||
protected function pExpr_StaticCall(Expr\StaticCall $node) {
|
||||
@ -461,7 +461,7 @@ class Standard extends PrettyPrinterAbstract
|
||||
? $this->p($node->name)
|
||||
: '{' . $this->p($node->name) . '}')
|
||||
: $node->name)
|
||||
. '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
||||
}
|
||||
|
||||
protected function pExpr_Empty(Expr\Empty_ $node) {
|
||||
@ -509,9 +509,9 @@ class Standard extends PrettyPrinterAbstract
|
||||
$syntax = $node->getAttribute('kind',
|
||||
$this->options['shortArraySyntax'] ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);
|
||||
if ($syntax === Expr\Array_::KIND_SHORT) {
|
||||
return '[' . $this->pCommaSeparated($node->items) . ']';
|
||||
return '[' . $this->pMaybeMultiline($node->items, true) . ']';
|
||||
} else {
|
||||
return 'array(' . $this->pCommaSeparated($node->items) . ')';
|
||||
return 'array(' . $this->pMaybeMultiline($node->items, true) . ')';
|
||||
}
|
||||
}
|
||||
|
||||
@ -561,10 +561,10 @@ class Standard extends PrettyPrinterAbstract
|
||||
|
||||
protected function pExpr_New(Expr\New_ $node) {
|
||||
if ($node->class instanceof Stmt\Class_) {
|
||||
$args = $node->args ? '(' . $this->pCommaSeparated($node->args) . ')' : '';
|
||||
$args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : '';
|
||||
return 'new ' . $this->pClassCommon($node->class, $args);
|
||||
}
|
||||
return 'new ' . $this->p($node->class) . '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
return 'new ' . $this->p($node->class) . '(' . $this->pMaybeMultiline($node->args) . ')';
|
||||
}
|
||||
|
||||
protected function pExpr_Clone(Expr\Clone_ $node) {
|
||||
@ -938,4 +938,21 @@ class Standard extends PrettyPrinterAbstract
|
||||
return '(' . $this->p($node) . ')';
|
||||
}
|
||||
}
|
||||
|
||||
private function hasNodeWithComments(array $nodes) {
|
||||
foreach ($nodes as $node) {
|
||||
if ($node && $node->getAttribute('comments')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function pMaybeMultiline(array $nodes, $trailingComma = false) {
|
||||
if (!$this->hasNodeWithComments($nodes)) {
|
||||
return $this->pCommaSeparated($nodes);
|
||||
} else {
|
||||
return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -342,6 +342,38 @@ abstract class PrettyPrinterAbstract
|
||||
return $this->pImplode($nodes, ', ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Pretty prints a comma-separated list of nodes in multiline style, including comments.
|
||||
*
|
||||
* The result includes a leading newline and one level of indentation (same as pStmts).
|
||||
*
|
||||
* @param Node[] $nodes Array of Nodes to be printed
|
||||
* @param bool $trailingComma Whether to use a trailing comma
|
||||
*
|
||||
* @return string Comma separated pretty printed nodes in multiline style
|
||||
*/
|
||||
protected function pCommaSeparatedMultiline(array $nodes, $trailingComma) {
|
||||
$result = '';
|
||||
$lastIdx = count($nodes) - 1;
|
||||
foreach ($nodes as $idx => $node) {
|
||||
if ($node !== null) {
|
||||
$comments = $node->getAttribute('comments', array());
|
||||
if ($comments) {
|
||||
$result .= "\n" . $this->pComments($comments);
|
||||
}
|
||||
|
||||
$result .= "\n" . $this->p($node);
|
||||
} else {
|
||||
$result .= "\n";
|
||||
}
|
||||
if ($trailingComma || $idx !== $lastIdx) {
|
||||
$result .= ',';
|
||||
}
|
||||
}
|
||||
|
||||
return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Signals the pretty printer that a string shall not be indented.
|
||||
*
|
||||
|
53
test/code/prettyPrinter/commentsInCommaList.test
Normal file
53
test/code/prettyPrinter/commentsInCommaList.test
Normal file
@ -0,0 +1,53 @@
|
||||
Comments in arrays and function calls
|
||||
-----
|
||||
<?php
|
||||
|
||||
$arr = [
|
||||
// Foo
|
||||
$foo,
|
||||
// Bar
|
||||
$bar,
|
||||
// Discarded
|
||||
];
|
||||
[
|
||||
// Foo
|
||||
$foo,
|
||||
,
|
||||
// Bar
|
||||
$bar,
|
||||
] = $arr;
|
||||
foo(
|
||||
// Foo
|
||||
$foo,
|
||||
// Bar
|
||||
$bar
|
||||
);
|
||||
new Foo(
|
||||
// Foo
|
||||
$foo
|
||||
);
|
||||
-----
|
||||
!!php7
|
||||
$arr = [
|
||||
// Foo
|
||||
$foo,
|
||||
// Bar
|
||||
$bar,
|
||||
];
|
||||
[
|
||||
// Foo
|
||||
$foo,
|
||||
,
|
||||
// Bar
|
||||
$bar,
|
||||
] = $arr;
|
||||
foo(
|
||||
// Foo
|
||||
$foo,
|
||||
// Bar
|
||||
$bar
|
||||
);
|
||||
new Foo(
|
||||
// Foo
|
||||
$foo
|
||||
);
|
Loading…
Reference in New Issue
Block a user