diff --git a/lib/PhpParser/NodeVisitor/NameResolver.php b/lib/PhpParser/NodeVisitor/NameResolver.php index c55532a5ea..79bbc4577a 100644 --- a/lib/PhpParser/NodeVisitor/NameResolver.php +++ b/lib/PhpParser/NodeVisitor/NameResolver.php @@ -86,6 +86,15 @@ public function enterNode(Node $node) { $this->resolveAttrGroups($node); $this->addNamespacedName($node); + } elseif ($node instanceof Stmt\Enum_) { + foreach ($node->implements as &$interface) { + $interface = $this->resolveClassName($interface); + } + + $this->resolveAttrGroups($node); + if (null !== $node->name) { + $this->addNamespacedName($node); + } } elseif ($node instanceof Stmt\Trait_) { $this->resolveAttrGroups($node); $this->addNamespacedName($node); @@ -110,6 +119,8 @@ public function enterNode(Node $node) { } } else if ($node instanceof Stmt\ClassConst) { $this->resolveAttrGroups($node); + } else if ($node instanceof Stmt\EnumCase) { + $this->resolveAttrGroups($node); } elseif ($node instanceof Expr\StaticCall || $node instanceof Expr\StaticPropertyFetch || $node instanceof Expr\ClassConstFetch diff --git a/lib/PhpParser/PrettyPrinter/Standard.php b/lib/PhpParser/PrettyPrinter/Standard.php index 14496ceb3c..62d1f34c10 100644 --- a/lib/PhpParser/PrettyPrinter/Standard.php +++ b/lib/PhpParser/PrettyPrinter/Standard.php @@ -730,6 +730,7 @@ protected function pStmt_Interface(Stmt\Interface_ $node) { protected function pStmt_Enum(Stmt\Enum_ $node) { return $this->pAttrGroups($node->attrGroups) . 'enum ' . $node->name + . ($node->scalarType ? " : $node->scalarType" : '') . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '') . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; } diff --git a/test/PhpParser/NodeVisitor/NameResolverTest.php b/test/PhpParser/NodeVisitor/NameResolverTest.php index a98b57cd00..5c85c292bd 100644 --- a/test/PhpParser/NodeVisitor/NameResolverTest.php +++ b/test/PhpParser/NodeVisitor/NameResolverTest.php @@ -206,6 +206,12 @@ public function a(A $a) : A; public function b(A|B|int $a): A|B|int; } +#[X] +enum E: int { + #[X] + case A = 1; +} + #[X] trait A {} @@ -264,6 +270,12 @@ public function a(\NS\A $a) : \NS\A; public function b(\NS\A|\NS\B|int $a) : \NS\A|\NS\B|int; } #[\NS\X] +enum E : int +{ + #[\NS\X] + case A = 1; +} +#[\NS\X] trait A { } @@ -327,6 +339,7 @@ public function testAddDeclarationNamespacedName() { ]), new Stmt\Trait_('E'), new Expr\New_(new Stmt\Class_(null)), + new Stmt\Enum_('F'), ]; $traverser = new PhpParser\NodeTraverser; @@ -339,6 +352,7 @@ public function testAddDeclarationNamespacedName() { $this->assertSame('NS\\D', (string) $stmts[0]->stmts[3]->consts[0]->namespacedName); $this->assertSame('NS\\E', (string) $stmts[0]->stmts[4]->namespacedName); $this->assertObjectNotHasAttribute('namespacedName', $stmts[0]->stmts[5]->class); + $this->assertSame('NS\\F', (string) $stmts[0]->stmts[6]->namespacedName); $stmts = $traverser->traverse([new Stmt\Namespace_(null, $nsStmts)]); $this->assertSame('A', (string) $stmts[0]->stmts[0]->namespacedName); @@ -347,6 +361,7 @@ public function testAddDeclarationNamespacedName() { $this->assertSame('D', (string) $stmts[0]->stmts[3]->consts[0]->namespacedName); $this->assertSame('E', (string) $stmts[0]->stmts[4]->namespacedName); $this->assertObjectNotHasAttribute('namespacedName', $stmts[0]->stmts[5]->class); + $this->assertSame('F', (string) $stmts[0]->stmts[6]->namespacedName); } public function testAddRuntimeResolvedNamespacedName() { diff --git a/test/code/prettyPrinter/stmt/enum.test b/test/code/prettyPrinter/stmt/enum.test index 260e27320e..407c371e4b 100644 --- a/test/code/prettyPrinter/stmt/enum.test +++ b/test/code/prettyPrinter/stmt/enum.test @@ -28,12 +28,12 @@ enum A implements B { } } -enum B +enum B : int { case X = 1; case Y = 2; } -enum C implements D +enum C : string implements D { case Z = 'A'; } \ No newline at end of file