Skip to content

Commit 18cd8dd

Browse files
committed
test: array tests
1 parent 0da2d66 commit 18cd8dd

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

lib/PhpParser/Node/Expr/Array_.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Array_ extends Expr {
99
// For use in "kind" attribute
1010
public const KIND_LONG = 1; // array() syntax
1111
public const KIND_SHORT = 2; // [] syntax
12+
public const KIND_MULTILINE = 4; // force multiline formatting
1213

1314
/** @var ArrayItem[] Items */
1415
public array $items;

lib/PhpParser/PrettyPrinter/Standard.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,16 @@ protected function pExpr_Variable(Expr\Variable $node): string {
600600
}
601601

602602
protected function pExpr_Array(Expr\Array_ $node): string {
603-
$syntax = $node->getAttribute('kind',
603+
$kind = $node->getAttribute('kind',
604604
$this->shortArraySyntax ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);
605+
606+
$forceMultiline = ($kind & Expr\Array_::KIND_MULTILINE) === Expr\Array_::KIND_MULTILINE;
607+
$syntax = $kind & ~Expr\Array_::KIND_MULTILINE;
608+
605609
if ($syntax === Expr\Array_::KIND_SHORT) {
606-
return '[' . $this->pMaybeMultiline($node->items, true) . ']';
610+
return '[' . $this->pMaybeMultiline($node->items, true, $forceMultiline) . ']';
607611
} else {
608-
return 'array(' . $this->pMaybeMultiline($node->items, true) . ')';
612+
return 'array(' . $this->pMaybeMultiline($node->items, true, $forceMultiline) . ')';
609613
}
610614
}
611615

@@ -1181,12 +1185,12 @@ protected function hasNodeWithComments(array $nodes): bool {
11811185
}
11821186

11831187
/** @param Node[] $nodes */
1184-
protected function pMaybeMultiline(array $nodes, bool $trailingComma = false): string {
1185-
if (!$this->hasNodeWithComments($nodes)) {
1186-
return $this->pCommaSeparated($nodes);
1187-
} else {
1188+
protected function pMaybeMultiline(array $nodes, bool $trailingComma = false, bool $forceMultiline = false): string {
1189+
if ($forceMultiline || $this->hasNodeWithComments($nodes)) {
11881190
return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl;
11891191
}
1192+
1193+
return $this->pCommaSeparated($nodes);
11901194
}
11911195

11921196
/** @param Node\Param[] $params
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Arrays
2+
-----
3+
<?php
4+
5+
$short = [1, 2, 3];
6+
$shortMultiline = [
7+
1,
8+
2,
9+
3
10+
];
11+
12+
$long = array(1, 2, 3);
13+
$longMultiline = array(
14+
1,
15+
2,
16+
3
17+
);
18+
-----
19+
$short = [1, 2, 3];
20+
$shortMultiline = [1, 2, 3];
21+
$long = array(1, 2, 3);
22+
$longMultiline = array(1, 2, 3);

0 commit comments

Comments
 (0)