Skip to content

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

@holtkamp

Description

@holtkamp

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions