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

Closed
holtkamp opened this Issue Oct 7, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@holtkamp
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

This comment has been minimized.

Show comment
Hide comment
@mnapoli

mnapoli Oct 7, 2015

Member

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 :/

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

This comment has been minimized.

Show comment
Hide comment
@holtkamp

holtkamp Oct 7, 2015

Contributor

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 ;)

Contributor

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 ;)

@thebigb thebigb referenced this issue in PHP-DI/PhpDocReader Nov 22, 2015

Merged

Fixes issue where aliases in traits aren't detected #7

@mnapoli mnapoli added this to the 5.2 milestone Nov 29, 2015

@mnapoli mnapoli closed this in f7378d8 Nov 29, 2015

@mnapoli

This comment has been minimized.

Show comment
Hide comment
@mnapoli

mnapoli Nov 29, 2015

Member

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

Member

mnapoli commented Nov 29, 2015

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

@holtkamp

This comment has been minimized.

Show comment
Hide comment
@holtkamp

holtkamp Nov 30, 2015

Contributor

confirmed, got it working now, thanks!

Contributor

holtkamp commented Nov 30, 2015

confirmed, got it working now, thanks!

@mnapoli

This comment has been minimized.

Show comment
Hide comment
@mnapoli

mnapoli Nov 30, 2015

Member

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

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