Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions grammar/php.y
Original file line number Diff line number Diff line change
Expand Up @@ -1267,13 +1267,19 @@ class_constant:

array_short_syntax:
'[' array_pair_list ']'
{ $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_SHORT;
{ $attrs = attributes();
$attrs['kind'] = $this->isMultiline($attrs)
? Expr\Array_::KIND_SHORT | Expr\Array_::KIND_MULTILINE
: Expr\Array_::KIND_SHORT;
$$ = new Expr\Array_($2, $attrs); }
;

dereferenceable_scalar:
T_ARRAY '(' array_pair_list ')'
{ $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_LONG;
{ $attrs = attributes();
$attrs['kind'] = $this->isMultiline($attrs)
? Expr\Array_::KIND_LONG | Expr\Array_::KIND_MULTILINE
: Expr\Array_::KIND_LONG;
$$ = new Expr\Array_($3, $attrs);
$this->createdArrays->offsetSet($$); }
| array_short_syntax
Expand Down
1 change: 1 addition & 0 deletions lib/PhpParser/Node/Expr/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Array_ extends Expr {
// For use in "kind" attribute
public const KIND_LONG = 1; // array() syntax
public const KIND_SHORT = 2; // [] syntax
public const KIND_MULTILINE = 4; // force multiline formatting

/** @var ArrayItem[] Items */
public array $items;
Expand Down
10 changes: 8 additions & 2 deletions lib/PhpParser/Parser/Php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2686,11 +2686,17 @@ protected function initReduceCallbacks(): void {
$self->semValue = new Expr\ClassConstFetch($self->semStack[$stackPos-(3-1)], new Expr\Error($self->getAttributes($self->tokenStartStack[$stackPos-(3-3)], $self->tokenEndStack[$stackPos-(3-3)])), $self->getAttributes($self->tokenStartStack[$stackPos-(3-1)], $self->tokenEndStack[$stackPos])); $self->errorState = 2;
},
560 => static function ($self, $stackPos) {
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(3-1)], $self->tokenEndStack[$stackPos]); $attrs['kind'] = Expr\Array_::KIND_SHORT;
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(3-1)], $self->tokenEndStack[$stackPos]);
$attrs['kind'] = $self->isMultiline($attrs)
? Expr\Array_::KIND_SHORT | Expr\Array_::KIND_MULTILINE
: Expr\Array_::KIND_SHORT;
$self->semValue = new Expr\Array_($self->semStack[$stackPos-(3-2)], $attrs);
},
561 => static function ($self, $stackPos) {
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(4-1)], $self->tokenEndStack[$stackPos]); $attrs['kind'] = Expr\Array_::KIND_LONG;
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(4-1)], $self->tokenEndStack[$stackPos]);
$attrs['kind'] = $self->isMultiline($attrs)
? Expr\Array_::KIND_LONG | Expr\Array_::KIND_MULTILINE
: Expr\Array_::KIND_LONG;
$self->semValue = new Expr\Array_($self->semStack[$stackPos-(4-3)], $attrs);
$self->createdArrays->offsetSet($self->semValue);
},
Expand Down
10 changes: 8 additions & 2 deletions lib/PhpParser/Parser/Php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -2687,11 +2687,17 @@ protected function initReduceCallbacks(): void {
$self->semValue = new Expr\ClassConstFetch($self->semStack[$stackPos-(3-1)], new Expr\Error($self->getAttributes($self->tokenStartStack[$stackPos-(3-3)], $self->tokenEndStack[$stackPos-(3-3)])), $self->getAttributes($self->tokenStartStack[$stackPos-(3-1)], $self->tokenEndStack[$stackPos])); $self->errorState = 2;
},
563 => static function ($self, $stackPos) {
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(3-1)], $self->tokenEndStack[$stackPos]); $attrs['kind'] = Expr\Array_::KIND_SHORT;
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(3-1)], $self->tokenEndStack[$stackPos]);
$attrs['kind'] = $self->isMultiline($attrs)
? Expr\Array_::KIND_SHORT | Expr\Array_::KIND_MULTILINE
: Expr\Array_::KIND_SHORT;
$self->semValue = new Expr\Array_($self->semStack[$stackPos-(3-2)], $attrs);
},
564 => static function ($self, $stackPos) {
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(4-1)], $self->tokenEndStack[$stackPos]); $attrs['kind'] = Expr\Array_::KIND_LONG;
$attrs = $self->getAttributes($self->tokenStartStack[$stackPos-(4-1)], $self->tokenEndStack[$stackPos]);
$attrs['kind'] = $self->isMultiline($attrs)
? Expr\Array_::KIND_LONG | Expr\Array_::KIND_MULTILINE
: Expr\Array_::KIND_LONG;
$self->semValue = new Expr\Array_($self->semStack[$stackPos-(4-3)], $attrs);
$self->createdArrays->offsetSet($self->semValue);
},
Expand Down
11 changes: 11 additions & 0 deletions lib/PhpParser/ParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,17 @@ private function isSimpleExit(array $args): bool {
return false;
}

/**
* @param array{startLine?:int, endLine?: int} $attrs
*/
protected function isMultiline(array $attrs): bool {
if (!isset($attrs['startLine']) || !isset($attrs['endLine'])) {
return false;
}

return $attrs['startLine'] !== $attrs['endLine'];
}

/**
* @param array<Node\Arg|Node\VariadicPlaceholder> $args
* @param array<string, mixed> $attrs
Expand Down
18 changes: 11 additions & 7 deletions lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,16 @@ protected function pExpr_Variable(Expr\Variable $node): string {
}

protected function pExpr_Array(Expr\Array_ $node): string {
$syntax = $node->getAttribute('kind',
$kind = $node->getAttribute('kind',
$this->shortArraySyntax ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);

$forceMultiline = ($kind & Expr\Array_::KIND_MULTILINE) === Expr\Array_::KIND_MULTILINE;
$syntax = $kind & ~Expr\Array_::KIND_MULTILINE;

if ($syntax === Expr\Array_::KIND_SHORT) {
return '[' . $this->pMaybeMultiline($node->items, true) . ']';
return '[' . $this->pMaybeMultiline($node->items, true, $forceMultiline) . ']';
} else {
return 'array(' . $this->pMaybeMultiline($node->items, true) . ')';
return 'array(' . $this->pMaybeMultiline($node->items, true, $forceMultiline) . ')';
}
}

Expand Down Expand Up @@ -1181,12 +1185,12 @@ protected function hasNodeWithComments(array $nodes): bool {
}

/** @param Node[] $nodes */
protected function pMaybeMultiline(array $nodes, bool $trailingComma = false): string {
if (!$this->hasNodeWithComments($nodes)) {
return $this->pCommaSeparated($nodes);
} else {
protected function pMaybeMultiline(array $nodes, bool $trailingComma = false, bool $forceMultiline = false): string {
if ($forceMultiline || $this->hasNodeWithComments($nodes)) {
return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl;
}

return $this->pCommaSeparated($nodes);
}

/** @param Node\Param[] $params
Expand Down
83 changes: 83 additions & 0 deletions test/code/prettyPrinter/stmt/arrays.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Arrays
-----
<?php

$short = [1, 2, 3];
$shortMultiline = [
1,
2,
3
];

$long = array(1, 2, 3);
$longMultiline = array(
1,
2,
3
);

$mixedShort = array(
array(1, 2, 3),
array(1,
2, 3),
[4, 5, 6],
[1,
2, 3],
2,
3,
);
$mixedLong = [
array(1, 2, 3),
array(1,
2, 3),
[4, 5, 6],
[1,
2, 3],
2,
3
];
-----
$short = [1, 2, 3];
$shortMultiline = [
1,
2,
3,
];
$long = array(1, 2, 3);
$longMultiline = array(
1,
2,
3,
);
$mixedShort = array(
array(1, 2, 3),
array(
1,
2,
3,
),
[4, 5, 6],
[
1,
2,
3,
],
2,
3,
);
$mixedLong = [
array(1, 2, 3),
array(
1,
2,
3,
),
[4, 5, 6],
[
1,
2,
3,
],
2,
3,
];