Skip to content

Commit 3f2d283

Browse files
committed
test: array tests
1 parent c9d0b6c commit 3f2d283

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
@@ -592,12 +592,16 @@ protected function pExpr_Variable(Expr\Variable $node): string {
592592
}
593593

594594
protected function pExpr_Array(Expr\Array_ $node): string {
595-
$syntax = $node->getAttribute('kind',
595+
$kind = $node->getAttribute('kind',
596596
$this->shortArraySyntax ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);
597+
598+
$forceMultiline = ($kind & Expr\Array_::KIND_MULTILINE) === Expr\Array_::KIND_MULTILINE;
599+
$syntax = $kind & ~Expr\Array_::KIND_MULTILINE;
600+
597601
if ($syntax === Expr\Array_::KIND_SHORT) {
598-
return '[' . $this->pMaybeMultiline($node->items, true) . ']';
602+
return '[' . $this->pMaybeMultiline($node->items, true, $forceMultiline) . ']';
599603
} else {
600-
return 'array(' . $this->pMaybeMultiline($node->items, true) . ')';
604+
return 'array(' . $this->pMaybeMultiline($node->items, true, $forceMultiline) . ')';
601605
}
602606
}
603607

@@ -1171,12 +1175,12 @@ protected function hasNodeWithComments(array $nodes): bool {
11711175
}
11721176

11731177
/** @param Node[] $nodes */
1174-
protected function pMaybeMultiline(array $nodes, bool $trailingComma = false): string {
1175-
if (!$this->hasNodeWithComments($nodes)) {
1176-
return $this->pCommaSeparated($nodes);
1177-
} else {
1178+
protected function pMaybeMultiline(array $nodes, bool $trailingComma = false, bool $forceMultiline = false): string {
1179+
if ($forceMultiline || $this->hasNodeWithComments($nodes)) {
11781180
return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl;
11791181
}
1182+
1183+
return $this->pCommaSeparated($nodes);
11801184
}
11811185

11821186
/** @param Node\AttributeGroup[] $nodes */
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)