Add handling for Enum(Case)s in NameResolver

This commit is contained in:
Bob Weinand 2021-07-03 02:00:54 +02:00 committed by Nikita Popov
parent c35cc4b2cb
commit 3fb73520c1
4 changed files with 29 additions and 2 deletions

View File

@ -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

View File

@ -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 . '}';
}

View File

@ -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() {

View File

@ -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';
}