Skip to content

Commit

Permalink
Add handling for Enum(Case)s in NameResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoebi authored and nikic committed Jul 3, 2021
1 parent c35cc4b commit 3fb7352
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/PhpParser/NodeVisitor/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '}';
}
Expand Down
15 changes: 15 additions & 0 deletions test/PhpParser/NodeVisitor/NameResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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
{
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions test/code/prettyPrinter/stmt/enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

0 comments on commit 3fb7352

Please sign in to comment.