diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php index 4e4ba9a844..89ff44dfb2 100644 --- a/lib/PhpParser/PrettyPrinterAbstract.php +++ b/lib/PhpParser/PrettyPrinterAbstract.php @@ -679,6 +679,10 @@ protected function p( } [$printFn, $skipToken, $findToken] = $this->modifierChangeMap[$key]; + if ($skipToken === \T_NEW && $pos !== $startPos) { + // Anonymous class attributes were printed, so we are already past "new". + $skipToken = \T_WHITESPACE; + } $skipWSPos = $this->origTokens->skipRight($pos, $skipToken); $result .= $this->origTokens->getTokenCode($pos, $skipWSPos, $indentAdjustment); $result .= $this->$printFn($subNode); diff --git a/test/code/formatPreservation/modifierChange.test b/test/code/formatPreservation/modifierChange.test index ec6cdf37df..4591e78ee7 100644 --- a/test/code/formatPreservation/modifierChange.test +++ b/test/code/formatPreservation/modifierChange.test @@ -84,4 +84,21 @@ class X { protected function m() {} /** A */ protected function m2() {} -} \ No newline at end of file +} +----- +expr->class->flags = Modifiers::READONLY; +$stmts[1]->expr->class->flags = 0; +$stmts[2]->expr->class->flags = Modifiers::READONLY; +$stmts[2]->expr->class->attrGroups[] = new Node\AttributeGroup([ + new Node\Attribute(new Node\Name('B'), []), +]); +----- +