Skip to content

fix: preserve array multiline mode #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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 @@ -1226,13 +1226,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->attach($$); }
| array_short_syntax { $$ = $1; $this->createdArrays->attach($$); }
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 @@ -2564,11 +2564,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;
},
547 => 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);
},
548 => 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->attach($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 @@ -2568,11 +2568,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;
},
549 => 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);
},
550 => 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->attach($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 @@ -1229,6 +1229,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 @@ -592,12 +592,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 @@ -1171,12 +1175,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\AttributeGroup[] $nodes */
Expand Down
30 changes: 30 additions & 0 deletions test/code/prettyPrinter/stmt/arrays.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Arrays
-----
<?php

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

$long = array(1, 2, 3);
$longMultiline = array(
1,
2,
3
);
-----
$short = [1, 2, 3];
$shortMultiline = [
1,
2,
3,
];
$long = array(1, 2, 3);
$longMultiline = array(
1,
2,
3,
);