Skip to content

Commit

Permalink
Support PropertyHooks in NameResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Sep 17, 2024
1 parent 23c79fb commit 8d09ba8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/PhpParser/NodeVisitor/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public function enterNode(Node $node) {
$node->type = $this->resolveType($node->type);
}
$this->resolveAttrGroups($node);
} elseif ($node instanceof Node\PropertyHook) {
foreach ($node->params as $param) {
$param->type = $this->resolveType($param->type);
$this->resolveAttrGroups($param);
}
$this->resolveAttrGroups($node);
} elseif ($node instanceof Stmt\Const_) {
foreach ($node->consts as $const) {
$this->addNamespacedName($const);
Expand Down
26 changes: 25 additions & 1 deletion test/PhpParser/NodeVisitor/NameResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ class A extends B implements C, D {
public const X A = X::Bar;
public const X\Foo B = X\Foo::Bar;
public const \X\Foo C = \X\Foo::Bar;
public Foo $foo {
#[X]
set(#[X] Bar $v) {}
}
public function __construct(
public Foo $bar {
#[X]
set(#[X] Bar $v) {}
}
) {}
}
#[X]
Expand Down Expand Up @@ -269,6 +281,18 @@ class A extends \NS\B implements \NS\C, \NS\D
public const \NS\X A = \NS\X::Bar;
public const \NS\X\Foo B = \NS\X\Foo::Bar;
public const \X\Foo C = \X\Foo::Bar;
public \NS\Foo $foo {
#[\NS\X]
set(#[\NS\X] \NS\Bar $v) {
}
}
public function __construct(public \NS\Foo $bar {
#[\NS\X]
set(#[\NS\X] \NS\Bar $v) {
}
})
{
}
}
#[\NS\X]
interface A extends \NS\C, \NS\D
Expand Down Expand Up @@ -543,7 +567,7 @@ public function testAttributeOnlyMode(): void {
}

private function parseAndResolve(string $code): array {
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative());
$parser = new PhpParser\Parser\Php8(new PhpParser\Lexer\Emulative());
$traverser = new PhpParser\NodeTraverser();
$traverser->addVisitor(new NameResolver());

Expand Down

0 comments on commit 8d09ba8

Please sign in to comment.