Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Internal\Differ;
use PhpParser\Internal\PrintableNewAnonClassNode;
use PhpParser\Internal\TokenStream;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\AssignOp;
Expand Down Expand Up @@ -1211,10 +1212,22 @@ protected function pStatic(bool $static): string {
* @return bool Whether multiline formatting is used
*/
protected function isMultiline(array $nodes): bool {
if (\count($nodes) < 2) {
if (!$nodes) {
return false;
}

if (count($nodes) === 1) {
$node = current($nodes);
$startPos = $node->getStartTokenPos() - 1;
$endPos = $node->getEndTokenPos() + 1;
$text = $this->origTokens->getTokenCode($startPos, $endPos, 0);
if (false === strpos($text, "\n")) {
return false;
}

return true; // Since the only element has a new line, we consider it multiline
}

$pos = -1;
foreach ($nodes as $node) {
if (null === $node) {
Expand Down
8 changes: 5 additions & 3 deletions test/code/formatPreservation/array_spread.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ $array->items[] = new Expr\ArrayItem(new Expr\Variable('b'));
-----
<?php
$items = [
...$value, $b
...$value,
$b
];
-----
<?php
Expand All @@ -25,5 +26,6 @@ $array->items[] = new Expr\ArrayItem(new Expr\Variable('c'), null, false, [], tr
<?php
$items =
[
... $value, ...$c
];
... $value,
...$c
];
3 changes: 2 additions & 1 deletion test/code/formatPreservation/arrow_function.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ $stmts[0]->expr->params[] = new Node\Param(new Expr\Variable('b'));
-----
<?php
fn(
$a, $b
$a,
$b
) => $a;
-----
<?php
Expand Down