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
NameResolver should be able to resolve unqualified ConstFetch in namespace #236
Comments
Generally we do not know whether an unqualified constant fetch in a namespace refers to the constant in the namespace or to a global constant. As such they are not resolved by the NameResolver. If you want to do additional resolution based on your specific knowledge of which constants exist or not, you should resolve them manually. |
Hmm, it's interesting https://3v4l.org/BBeFX
However, values are still from the global namespace, so you are right, an additional logic should check this fact to resolve this correctly (if it's possible on parse level). |
I would really like to have an option to enable this. Let me explain my use case: |
The resolved name could be added as an attribute -- would avoid the need for an option. For now I'd suggest manually keeping track of the current namespace and using |
Currently using this piece of code as a workaround (I have decorated all nodes with parent references): if ($parent instanceof Node\Expr\FuncCall || $parent instanceof Node\Expr\ConstFetch) {
// Find and try with namespace
$n = $parent;
while (isset($n)) {
$n = $n->getAttribute('parentNode');
if ($n instanceof Node\Stmt\Namespace_) {
return (string)$n->name . '\\' . $name;
}
}
}
return $name; It is not pretty though because I do this every time a request is made, while I would rather like to save this on the node once with a NodeVisitor. |
90834bf Does this suit you? Note that the attribute is on the name itself, not the FuncCall/ConstFetch node. |
Btw, don't forget that some symbols in PHP are case-insensitive. In particular class and function names always are, so you should lowercase these when inserting them into the definition table. Constants are more complicated, because they exist in both case-sensitive and case-insensitive variants. |
Sure, that would work fine! For consistency though, would be nice to have
And thanks for the tip! |
Unless the mechanism is changed entirely (#312) I won't use dynamic object properties (the namespacedName property predates the introduction of attributes). |
@nikic are you planning another beta release? |
@felixfbecker Yeah, I expect another beta sometime soon. |
"Sometime soon" turned out to be two weeks, but here we go: https://github.com/nikic/PHP-Parser/releases/tag/v3.0.0beta2 |
Thanks. Had to release PHPLS 3.0 though, which simply depends on c0f0edf directly 😉 |
Looks like constants in namespaces aren't resolved:
Here,
NameResolver
will leave the name unqualified for the constant fetch. However, it should be FQN:Foo\A
The text was updated successfully, but these errors were encountered: