Constants with a namespace are not added to Namespace_
dirx opened this issue · 4 comments
Only Class based Constants are currently recognized and added to the Namespace_
.
Constants in a namespace are ignored:
namespace Example;
const NS_CONST = 1; // accessible via \Example\NS_CONST
Also Constants defined e.g. via define('GLOBAL_CONST', 1)
in a namespace \Example
are wrongly attributed to this namespace. FQSEN should be \GLOBAL_CONST
.
Those Constants have to be explicitly defined with the current Namespace - e.g.:
namespace Example;
define('GLOBAL_CONST', 1); // only accessible via \GLOBAL_CONST or GLOBAL_CONST
define(__NAMESPACE__ . '\\NS_CONST', 1);
That's odd; I thought and tested this.. at least the former issue
@mvriel Adding && $namespace->getFqsen() . '\\' . $constant->getName() !== ...
might fix the first one.
On a second thought - the defined
case might be a separate issue - handling extra logic like __NAMESPACE__ . '\\NS_CONST'
might not be a trivial thing to do.
Maybe special expressions with __NAMESPACE__
could be resolved with a special fallback evaluator for:
Something like:
private function determineFqsen(?Context $context, Arg $name) : Fqsen
{
$namespace = $context ? $context->getNamespace() : '';
$evaluator = new ConstExprEvaluator(function(Expr $expr) use ($namespace) {
if ($expr instanceof Namespace_) {
return $namespace;
}
return ''; // or throw Exception
});
$nameString = $evaluator->evaluateSilently($name->value);
if (strpos($nameString, '\\') === false) {
return new Fqsen(sprintf('\\%s', $nameString));
}
return new Fqsen(sprintf('%s', $nameString));
}
Not sure how common those special expressions are and if you want to support them.
About the expressions, let's be a bit pragmatic with that. If somebody reports this as an issue we might want to support it. although we are never able to support everything that people can put in there. Because PHP allows dynamically defined constants and this library is only doing static analysis on the code.
Thank you for reaching out and thanks again for creating a patch. We do appreciate that a lot.