Roave/BetterReflection

[Feature Request] Access to namespace aliases

MaximilianKresse opened this issue · 2 comments

We're currently using BetterReflection to parse our code and generate our manual with it. We currently have v5.11.1 in usage and we wanted to update to the current version but there is a problem with version 6. We previously used ReflectionClass::getDeclaringNamespaceAst() to get access to the namespace node and find all USE and USE-Aliases (use * as *;) for this class to be able to parse the doc block and annotations. (together with a small hack for classes without a namespace)

Code example:

<?php

namespace TestNamespace;

use TestNamespace\SomeotherNamespace\SuperClass;
use SomeNamespace\SuperClass as AnotherClass;

class Test
{
    /**
     * Do something
     * @param SuperClass|AnotherClass $a Some cool parameter
     */
    public function test($a)
    {
        // something
    }
}

To be able to make any sense out of the @param definition we require a way to get the Namespace-Uses of the class.

IMO this is not part of a ReflectionClass, but of a Namespace AST node, so adding it back to ReflectionClass is awkward at best.

For example, this is valid:

<?php

namespace B {
    class C {}
}

namespace A {
    use B\C;
    class D extends C {}
    class E extends C {}
}

namespace {
    var_dump(new A\D());
    var_dump(new A\E());
}

https://3v4l.org/pAsqM

I don't think that such an API should therefore exist on ReflectionClass.

Until roave/better-reflection:^4, we had phpdocumentor/reflection-docblock as a dependency.

I think the safest way is to use that directly, with

https://github.com/phpDocumentor/ReflectionDocBlock/blob/566af9fb94c556de91562fcfcbc392f66680111b/src/DocBlockFactoryInterface.php#L19-L22

AFAIK, phpDocumentor has facilities to get the Context from a file too.

Thank you for your fast response! I'll take a look into it - the part I'm looking for seems to be located in "phpDocumentor/TypeResolver".