Skip to content

Commit

Permalink
Fix issue where anonymous classes in lists are not formatted correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lkrms committed Jan 2, 2025
1 parent 14cc586 commit 247242c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ public function format(

case \T_OPEN_PARENTHESIS:
$prev = $parent->PrevCode;
if ($prev && (
if (!$prev || !(
$idx->BeforeListParenthesis[$prev->id] || (
$prev->id === \T_CLOSE_BRACE
&& !($prev->Flags & TokenFlag::STRUCTURAL_BRACE)
Expand All @@ -999,22 +999,26 @@ public function format(
&& $idx->FunctionOrFn[$prev->PrevCode->id]
)
)) {
break;
continue 2;
}
continue 2;
break;

case \T_OPEN_BRACKET:
if ($parent->isArrayOpenBracket()) {
break;
if (!$parent->isArrayOpenBracket()) {
continue 2;
}
continue 2;
break;
}

$items ??= $parent->children()
->filter(
fn(Token $t, ?Token $next, ?Token $prev) =>
$t->id !== $delimiter && (
!$prev || ($t->PrevCode && $t->PrevCode->id === $delimiter)
$t->id !== $delimiter
&& $t->Statement === $t
&& (
!$prev
|| !$t->PrevCode
|| $t->PrevCode->id === $delimiter
)
);

Expand Down
2 changes: 2 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ private function parseStatements(array $scopes, ?array &$statements): void
// lists, arrays, `for` expressions
// - between non-structural braces, e.g. in `match`
// expressions
// - not after `implements` in anonymous class declarations

if (
!$token->NextSibling
Expand Down Expand Up @@ -687,6 +688,7 @@ private function parseStatements(array $scopes, ?array &$statements): void
$parent->id === \T_OPEN_BRACE
&& !($parent->Flags & TokenFlag::STRUCTURAL_BRACE)
))
&& $token->skipPrevCodeFrom($idx->DeclarationList)->id !== \T_IMPLEMENTS
)
) {
$end = $token->CloseBracket ?? $token;
Expand Down
9 changes: 9 additions & 0 deletions src/TokenIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,15 @@ class TokenIndex implements HasTokenIndex, Immutable
]
+ self::TOKEN_INDEX;

/**
* T_COMMA, T_NAME_FULLY_QUALIFIED, T_NAME_QUALIFIED, T_NAME_RELATIVE,
* T_STATIC, T_STRING
*
* @var array<int,bool>
*/
public array $DeclarationList = self::DECLARATION_LIST
+ self::TOKEN_INDEX;

/**
* T_ABSTRACT, T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG,
* T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG, T_AND, T_ATTRIBUTE_COMMENT,
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,36 @@ function a()
goto bar;
}
bar: qux();
PHP,
$formatter,
],
'anonymous class commas in array scope' => [
<<<'PHP'
<?php
$foo = new Bar(
[
'baz' => [
new class implements Foo, Bar {
public const FOO = 1;
},
'qux',
],
],
);

PHP,
<<<'PHP'
<?php
$foo = new Bar(
[
'baz' => [
new class implements Foo, Bar {
public const FOO = 1;
},
'qux',
],
],
);
PHP,
$formatter,
],
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,29 @@ public static function statementsProvider(): array
case $bar ? fn(): ?int => null : $foo:
break;
}
PHP,
],
'anonymous class commas in array scope' => [
[
"T1:L2:'\$foo' - T32:L11:';'",
"T6:L3:'[' - T30:L10:','",
"T7:L4:'\'baz\'' - T28:L9:','",
"T10:L5:'new' - T24:L7:','",
"T17:L6:'public' - T22:L6:';'",
"T25:L8:'\'qux\'' - T26:L8:','",
],
<<<'PHP'
<?php
$foo = new Bar(
[
'baz' => [
new class implements Foo, Bar {
public const FOO = 1;
},
'qux',
],
],
);
PHP,
],
];
Expand Down

0 comments on commit 247242c

Please sign in to comment.