diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php index 8303c427aa..2ad8df975c 100644 --- a/lib/PhpParser/PrettyPrinterAbstract.php +++ b/lib/PhpParser/PrettyPrinterAbstract.php @@ -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; @@ -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) { diff --git a/test/code/formatPreservation/array_spread.test b/test/code/formatPreservation/array_spread.test index ce83e1651a..443a079d02 100644 --- a/test/code/formatPreservation/array_spread.test +++ b/test/code/formatPreservation/array_spread.test @@ -10,7 +10,8 @@ $array->items[] = new Expr\ArrayItem(new Expr\Variable('b')); ----- items[] = new Expr\ArrayItem(new Expr\Variable('c'), null, false, [], tr expr->params[] = new Node\Param(new Expr\Variable('b')); ----- $a; -----