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

Any way to get fully qualified class names in AST #195

Closed
moufmouf opened this issue Apr 5, 2016 · 4 comments
Closed

Any way to get fully qualified class names in AST #195

moufmouf opened this issue Apr 5, 2016 · 4 comments
Assignees
Labels
Milestone

Comments

@moufmouf
Copy link
Contributor

moufmouf commented Apr 5, 2016

Hi guys,

First of all, congratulations for the great package. I've started working with it today, and it's really a pleasure to use.

I'm having one quick question:

When I parse the AST (for instance using $method->getBodyAst()), the class names I get (from class PhpParser\Node\Name) are not fully qualified (they are not taking into account the use statements declared at the beginning of the file).

In nikic/PHPParser, there is a node traverser that can resolve the FQCN:
https://github.com/nikic/PHP-Parser/blob/master/doc/2_Usage_of_basic_components.markdown#the-nameresolver-node-visitor

I'm wondering if there is any chance I could use this node traverser within BetterReflection. Is this possible? Did I miss something?

(if you are wondering why, I'm working on a code analyzer that translates basic PHP code (a factory method) into an object representing container definition)

@asgrim
Copy link
Member

asgrim commented Apr 5, 2016

@moufmouf yes, it may be possible to use FindTypeFromAst which takes into account the use statements etc. by providing the LocatedSource. I imagine it looking something like:

$ast = $method->getBodyAst();

// do whatever you're doing to find the `PhpParser\Node\Name` node

$finder = new \BetterReflection\TypesFinder\FindTypeFromAst();
$type = $finder($theNameNode, $method->getLocatedSource(), $method->inNamespace ? $method->getNamespaceName() : '');

(I haven't tested this code, it's just off my head)

@asgrim asgrim added the question label Apr 5, 2016
@asgrim asgrim self-assigned this Apr 5, 2016
@asgrim
Copy link
Member

asgrim commented Apr 5, 2016

Alternatively, you could simply feed [$ast] into a node traverser to do the same, though I've not tried that!

@asgrim asgrim added this to the 1.1.0 milestone Apr 5, 2016
@moufmouf
Copy link
Contributor Author

moufmouf commented Apr 6, 2016

Hey James,

Thanks a lot for the input. Using the FindTypeByAst class, I was able to solve my issue in a breeze.

The code I used:

$finder = new FindTypeFromAst();
$type = $finder($className, $method->getLocatedSource(), $method->getDeclaringClass()->inNamespace() ? $method->getDeclaringClass()->getNamespaceName() : '');

Thanks a lot and congrats again for this great lib! Back to work now!

@moufmouf moufmouf closed this as completed Apr 6, 2016
@asgrim
Copy link
Member

asgrim commented Apr 6, 2016

You're welcome @moufmouf :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants