From 3fb73520c1d3529f2e4ebffebc84b32db43f16ea Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Sat, 3 Jul 2021 02:00:54 +0200 Subject: [PATCH] Add handling for Enum(Case)s in NameResolver --- lib/PhpParser/NodeVisitor/NameResolver.php | 11 +++++++++++ lib/PhpParser/PrettyPrinter/Standard.php | 1 + test/PhpParser/NodeVisitor/NameResolverTest.php | 15 +++++++++++++++ test/code/prettyPrinter/stmt/enum.test | 4 ++-- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/PhpParser/NodeVisitor/NameResolver.php b/lib/PhpParser/NodeVisitor/NameResolver.php index c55532a..79bbc45 100644 --- a/lib/PhpParser/NodeVisitor/NameResolver.php +++ b/lib/PhpParser/NodeVisitor/NameResolver.php @@ -86,6 +86,15 @@ class NameResolver extends NodeVisitorAbstract $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 @@ class NameResolver extends NodeVisitorAbstract } } 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 14496ce..62d1f34 100644 --- a/lib/PhpParser/PrettyPrinter/Standard.php +++ b/lib/PhpParser/PrettyPrinter/Standard.php @@ -730,6 +730,7 @@ class Standard extends PrettyPrinterAbstract 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 a98b57c..5c85c29 100644 --- a/test/PhpParser/NodeVisitor/NameResolverTest.php +++ b/test/PhpParser/NodeVisitor/NameResolverTest.php @@ -206,6 +206,12 @@ interface A extends C, D { 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 @@ interface A extends \NS\C, \NS\D 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 @@ EOC; ]), new Stmt\Trait_('E'), new Expr\New_(new Stmt\Class_(null)), + new Stmt\Enum_('F'), ]; $traverser = new PhpParser\NodeTraverser; @@ -339,6 +352,7 @@ EOC; $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 @@ EOC; $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 260e273..407c371 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