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 d0b3512 commit c0b1f07
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 @@ -115,6 +115,9 @@ public function enterNode(Node $node) {
$this->addNamespacedName($const);
}
} elseif ($node instanceof Stmt\ClassConst) {
if (null !== $node->type) {
$node->type = $this->resolveType($node->type);
}
$this->resolveAttrGroups($node);
} elseif ($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 @@ -188,7 +188,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 @@ -535,6 +535,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());
$traverser = new PhpParser\NodeTraverser();
Expand Down

0 comments on commit c0b1f07

Please sign in to comment.