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

Class imports in traits are not considered when parsing annotations #335

Closed
holtkamp opened this issue Oct 7, 2015 · 5 comments
Closed
Labels
Milestone

Comments

@holtkamp
Copy link
Contributor

holtkamp commented Oct 7, 2015

When using annotation based dependency injection on a property of a Trait (accessibility does not matter: private, protected, public), it seems no aliases can be used:

With the FQCN, it works

namespace Project\Traits;

trait MyTrait
{
  /**
   * @Inject
   * @var \Fully\Quallified\Class\Name
   */
  private $propertyName;
}

When using an alias, an error message is generated:

namespace Project\Traits;

use Fully\Quallified\Class\Name as AliasName

trait MyTrait
{
  /**
   * @Inject
   * @var AliasName
   */
  private $propertyName;
}

Error-message: The @var annotation on Class::propertyName contains a non existent class "AliasName". Did you maybe forget to add a "use" statement for this annotation?", which seem to origin here

I "guess" the parsing of use statements of the declaring class does not consider stuff that are defined in a trait?

https://github.com/PHP-DI/PhpDocReader/blob/21dce5e29f640d655e7b4583ecfb7d166127a5da/src/PhpDocReader/PhpDocReader.php#L102

I think not only the declaring class of the property should be considered, but also the traits that are used by that class with ReflectionClass.getTraits()

@mnapoli
Copy link
Member

mnapoli commented Oct 7, 2015

Ah right, that's probably not a case handled by PhpDocReader… It might be worth looking into using https://github.com/phpDocumentor/TypeResolver or else that's something to fix in PhpDocReader :/

@mnapoli mnapoli added the bug label Oct 7, 2015
@holtkamp
Copy link
Contributor Author

holtkamp commented Oct 7, 2015

Mm, interesting package indeed. Might be useful. But I would say to first try to stay with native PHP, recursively iterate over all used Traits (potential performance thingie?). Note that ReflectionClass.getTraitNames() can be used to determine the used Traits, but that it is not recursive by default, see 1st comment.

Anyhow, not a big issue, but good to report ;)

@mnapoli
Copy link
Member

mnapoli commented Nov 29, 2015

It has been fixed in PHP-DI/PhpDocReader#7

@holtkamp
Copy link
Contributor Author

confirmed, got it working now, thanks!

@mnapoli
Copy link
Member

mnapoli commented Nov 30, 2015

Thank you for testing, and thanks @thebigb for fixing this :)

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