Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix name context on no namespace #1067

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/PhpParser/NameContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public function getResolvedName(Name $name, int $type): ?Name {

if ($type !== Stmt\Use_::TYPE_NORMAL && $name->isUnqualified()) {
if (null === $this->namespace) {
$relativeName = $this->getNamespaceRelativeName($name->toString(), strtolower($name->toString()), $type, $name->getAttributes());

if ($relativeName instanceof Name) {
return $relativeName;
}

// outside of a namespace unaliased unqualified is same as fully qualified
return new FullyQualified($name, $name->getAttributes());
}
Expand Down Expand Up @@ -249,22 +255,25 @@ private function resolveAlias(Name $name, int $type): ?FullyQualified {
return null;
}

private function getNamespaceRelativeName(string $name, string $lcName, int $type): ?Name {
/**
* @param array<string, mixed> $attributes
*/
private function getNamespaceRelativeName(string $name, string $lcName, int $type, array $attributes = []): ?Name {
if (null === $this->namespace) {
return new Name($name);
return new Name($name, $attributes);
}

if ($type === Stmt\Use_::TYPE_CONSTANT) {
// The constants true/false/null always resolve to the global symbols, even inside a
// namespace, so they may be used without qualification
if ($lcName === "true" || $lcName === "false" || $lcName === "null") {
return new Name($name);
return new Name($name, $attributes);
}
}

$namespacePrefix = strtolower($this->namespace . '\\');
if (0 === strpos($lcName, $namespacePrefix)) {
return new Name(substr($name, strlen($namespacePrefix)));
return new Name(substr($name, strlen($namespacePrefix)), $attributes);
}

return null;
Expand Down
6 changes: 3 additions & 3 deletions test/PhpParser/NodeVisitor/NameResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public function testResolveNames(): void {
new \Hallo\Bar();
new \Bar();
new \Bar();
\bar();
\hi();
bar();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I mean. This should resolve to a fully qualified name. The fact that it is equivalent to the unqualified name doesn't matter, we should always resolve fully if it is possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if i understand correctly, the test is removing namespace, which no longer has namespace, means it doesn't have \\ prefix, next line to it, it have \bar which not changed as already fully qualified on the first place, which expexted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see line 124 which kept to have \\ prefix as it previously already fully qualified.

hi();
\Hallo\bar();
\foo\bar();
\bar();
Expand Down Expand Up @@ -197,7 +197,7 @@ class A extends B implements C, D {

#[X]
const C = 1;

public const X A = X::Bar;
public const X\Foo B = X\Foo::Bar;
public const \X\Foo C = \X\Foo::Bar;
Expand Down
Loading