Fix formatting preservation for match

This commit is contained in:
Nikita Popov 2022-09-03 21:02:34 +02:00
parent c585a2d766
commit a772853307
2 changed files with 9 additions and 5 deletions

View File

@ -826,8 +826,10 @@ abstract class PrettyPrinterAbstract {
// We go multiline if the original code was multiline,
// or if it's an array item with a comment above it.
// Match always uses multiline formatting.
if ($insertStr === ', ' &&
($this->isMultiline($origNodes) || $arrItem->getComments())
($this->isMultiline($origNodes) || $arrItem->getComments() ||
$parentNodeClass === Expr\Match_::class)
) {
$insertStr = ',';
$insertNewline = true;
@ -1378,7 +1380,7 @@ abstract class PrettyPrinterAbstract {
Stmt\Global_::class . '->vars' => ', ',
Stmt\GroupUse::class . '->uses' => ', ',
Stmt\Interface_::class . '->extends' => ', ',
//Expr\Match_::class . '->arms' => ', ',
Expr\Match_::class . '->arms' => ', ',
Stmt\Property::class . '->props' => ', ',
Stmt\StaticVar::class . '->vars' => ', ',
Stmt\TraitUse::class . '->traits' => ', ',

View File

@ -1,7 +1,8 @@
Matches
-----
<?php
$value = match (1) {
$value = match (1)
{
1
=>
'one'
@ -10,11 +11,12 @@ $value = match (1) {
$stmts[0]->expr->expr->arms[] = new Node\MatchArm(null, new Scalar\String_('two'));
-----
<?php
$value = match (1) {
$value = match (1)
{
1
=>
'one',
default => 'two',
default => 'two'
};
-----
<?php