Skip to content

Commit 1e9c687

Browse files
committed
fix: preserve multiline
1 parent 18cd8dd commit 1e9c687

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

grammar/php.y

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,13 +1267,19 @@ class_constant:
12671267

12681268
array_short_syntax:
12691269
'[' array_pair_list ']'
1270-
{ $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_SHORT;
1270+
{ $attrs = attributes();
1271+
$attrs['kind'] = $this->isMultiline($attrs)
1272+
? Expr\Array_::KIND_SHORT | Expr\Array_::KIND_MULTILINE
1273+
: Expr\Array_::KIND_SHORT;
12711274
$$ = new Expr\Array_($2, $attrs); }
12721275
;
12731276

12741277
dereferenceable_scalar:
12751278
T_ARRAY '(' array_pair_list ')'
1276-
{ $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_LONG;
1279+
{ $attrs = attributes();
1280+
$attrs['kind'] = $this->isMultiline($attrs)
1281+
? Expr\Array_::KIND_LONG | Expr\Array_::KIND_MULTILINE
1282+
: Expr\Array_::KIND_LONG;
12771283
$$ = new Expr\Array_($3, $attrs);
12781284
$this->createdArrays->offsetSet($$); }
12791285
| array_short_syntax

lib/PhpParser/ParserAbstract.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,14 @@ private function isSimpleExit(array $args): bool {
12641264
return false;
12651265
}
12661266

1267+
protected function isMultiline(array $attributes): bool {
1268+
if (!isset($attributes['startLine']) || !isset($attributes['endLine'])) {
1269+
return false;
1270+
}
1271+
1272+
return $attributes['startLine'] !== $attributes['endLine'];
1273+
}
1274+
12671275
/**
12681276
* @param array<Node\Arg|Node\VariadicPlaceholder> $args
12691277
* @param array<string, mixed> $attrs

test/code/prettyPrinter/stmt/arrays.test

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ $longMultiline = array(
1717
);
1818
-----
1919
$short = [1, 2, 3];
20-
$shortMultiline = [1, 2, 3];
20+
$shortMultiline = [
21+
1,
22+
2,
23+
3,
24+
];
2125
$long = array(1, 2, 3);
22-
$longMultiline = array(1, 2, 3);
26+
$longMultiline = array(
27+
1,
28+
2,
29+
3,
30+
);

0 commit comments

Comments
 (0)