Skip to content

Commit 232169f

Browse files
committed
Fix modifier change on anonymous class
Make sure the `new` keyword does not get dropped.
1 parent f174b0a commit 232169f

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,9 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
166166
* @var array<string, array{int|string|null, string, string}>
167167
*/
168168
protected array $emptyListInsertionMap;
169-
/** @var array<string, array{string, int}> Map from "{$class}->{$subNode}" to [$printFn, $token]
170-
* where $printFn is the function to print the modifiers and $token is the token before which
169+
/** @var array<string, array{string, int, int}>
170+
* Map from "{$class}->{$subNode}" to [$printFn, $skipToken, $findToken] where $printFn is the function to
171+
* print the modifiers, $skipToken is the token to skip at the start and $findToken is the token before which
171172
* the modifiers should be reprinted. */
172173
protected array $modifierChangeMap;
173174

@@ -677,8 +678,8 @@ protected function p(
677678
return $this->pFallback($fallbackNode, $precedence, $lhsPrecedence);
678679
}
679680

680-
[$printFn, $findToken] = $this->modifierChangeMap[$key];
681-
$skipWSPos = $this->origTokens->skipRightWhitespace($pos);
681+
[$printFn, $skipToken, $findToken] = $this->modifierChangeMap[$key];
682+
$skipWSPos = $this->origTokens->skipRight($pos, $skipToken);
682683
$result .= $this->origTokens->getTokenCode($pos, $skipWSPos, $indentAdjustment);
683684
$result .= $this->$printFn($subNode);
684685
$pos = $this->origTokens->findRight($skipWSPos, $findToken);
@@ -1678,15 +1679,15 @@ protected function initializeModifierChangeMap(): void {
16781679
}
16791680

16801681
$this->modifierChangeMap = [
1681-
Stmt\ClassConst::class . '->flags' => ['pModifiers', \T_CONST],
1682-
Stmt\ClassMethod::class . '->flags' => ['pModifiers', \T_FUNCTION],
1683-
Stmt\Class_::class . '->flags' => ['pModifiers', \T_CLASS],
1684-
Stmt\Property::class . '->flags' => ['pModifiers', \T_VARIABLE],
1685-
PrintableNewAnonClassNode::class . '->flags' => ['pModifiers', \T_CLASS],
1686-
Param::class . '->flags' => ['pModifiers', \T_VARIABLE],
1687-
PropertyHook::class . '->flags' => ['pModifiers', \T_STRING],
1688-
Expr\Closure::class . '->static' => ['pStatic', \T_FUNCTION],
1689-
Expr\ArrowFunction::class . '->static' => ['pStatic', \T_FN],
1682+
Stmt\ClassConst::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_CONST],
1683+
Stmt\ClassMethod::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_FUNCTION],
1684+
Stmt\Class_::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_CLASS],
1685+
Stmt\Property::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_VARIABLE],
1686+
PrintableNewAnonClassNode::class . '->flags' => ['pModifiers', \T_NEW, \T_CLASS],
1687+
Param::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_VARIABLE],
1688+
PropertyHook::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_STRING],
1689+
Expr\Closure::class . '->static' => ['pStatic', \T_WHITESPACE, \T_FUNCTION],
1690+
Expr\ArrowFunction::class . '->static' => ['pStatic', \T_WHITESPACE, \T_FN],
16901691
//Stmt\TraitUseAdaptation\Alias::class . '->newModifier' => 0, // TODO
16911692
];
16921693

test/code/formatPreservation/modifierChange.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ $stmts[0]->expr->class->flags = Modifiers::READONLY;
6464
$stmts[1]->expr->class->flags = 0;
6565
-----
6666
<?php
67-
readonly class {};
68-
class {};
67+
new readonly class {};
68+
new class {};
6969
-----
7070
<?php
7171
class X {

0 commit comments

Comments
 (0)