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

Constants with a namespace are not added to Namespace_ #156

Closed
dirx opened this issue Mar 2, 2020 · 4 comments
Closed

Constants with a namespace are not added to Namespace_ #156

dirx opened this issue Mar 2, 2020 · 4 comments

Comments

@dirx
Copy link
Contributor

dirx commented Mar 2, 2020

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

if ($namespace->getFqsen() . '::' . $constant->getName() !== (string) $constant->getFqsen()) {

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);
dirx added a commit to klitsche/dog that referenced this issue Mar 2, 2020
* open issue with constants in namespace in lib phpDocumentor/reflection (phpDocumentor/Reflection#156)
@mvriel
Copy link
Member

mvriel commented Mar 2, 2020

That's odd; I thought and tested this.. at least the former issue

@dirx
Copy link
Contributor Author

dirx commented Mar 2, 2020

@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.

@dirx
Copy link
Contributor Author

dirx commented Mar 3, 2020

Maybe special expressions with __NAMESPACE__ could be resolved with a special fallback evaluator for:

https://github.com/nikic/PHP-Parser/blob/35b8caf75e791ba1b2d24fec1552168d72692b12/lib/PhpParser/ConstExprEvaluator.php#L17

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.

@jaapio
Copy link
Member

jaapio commented Jun 19, 2020

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.

@jaapio jaapio closed this as completed Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants