Skip to content

Commit

Permalink
Fix NameResolver for class constant native type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 28, 2023
1 parent e453389 commit 9a36275
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/PhpParser/NodeVisitor/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public function enterNode(Node $node) {
$this->addNamespacedName($const);
}
} else if ($node instanceof Stmt\ClassConst) {
if (null !== $node->type) {
$node->type = $this->resolveType($node->type);
}
$this->resolveAttrGroups($node);
} else if ($node instanceof Stmt\EnumCase) {
$this->resolveAttrGroups($node);
Expand Down
23 changes: 22 additions & 1 deletion test/PhpParser/NodeVisitor/NameResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class A extends B implements C, D {
E::h as i;
E::j insteadof F, G;
}
#[X]
public float $php = 7.4;
public ?Foo $person;
Expand Down Expand Up @@ -537,6 +537,27 @@ public function testAttributeOnlyMode() {
new Name\FullyQualified('Foo\bar'), $n2->getAttribute('namespacedName'));
}

public function testClassConstantNativeType() {
$source = <<<'EOC'
<?php
namespace Foo;
class Bar
{
public const X A = X::Bar;
public const X\Foo B = X\Foo::Bar;
public const \X\Foo C = \X\Foo::Bar;
}
EOC;

$stmts = $this->parseAndResolve($source);
$classStmt = $stmts[0]->stmts[0];

$this->assertSame('Foo\\X', (string) $classStmt->stmts[0]->type);
$this->assertSame('Foo\\X\\Foo', (string) $classStmt->stmts[1]->type);
$this->assertSame('X\\Foo', (string) $classStmt->stmts[2]->type);
}

private function parseAndResolve(string $code): array
{
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative);
Expand Down

0 comments on commit 9a36275

Please sign in to comment.