Kdyby/Autowired

ReflectionProperty::getDeclaringClass doesn't work with traits

Closed this issue · 11 comments

I tried to write a trait with its own dependencies and for laziness I used the @autowire annotation in that trait. Turns out it only works with fully-qualified @var annotation but not with use statements.

The reason is that if the property was declared in a trait, ReflectionProperty::getDeclaringClass() returns the class that uses the trait, not the trait itself. Because of that the use statements are read from wrong file.

More information about the topic as well as possible solution is here: http://mouf-php.com/blog/php_reflection_api_traits

Should I send a PR using the solution from the article above or are you against such solution?

Maybe it would be better to have a helper function in Nette\Reflection for this. I'm not sure though because it seems that Nette\Reflection might soon be deprecated.

Sure enough, @inject annotations in Nette\DI are affected as well.

AFAIK the logic is in nette\reflection, kdyby\autowired is not expanding the uses by itself. So the bugreport should be filled there.

Actually that's not completely true because Kdyby\Autowired calls Nette\Reflection\AnnotationsParser::expandClassName which expects an annotation value and a class reflection. You pass the wrong class reflection.

$expandedType = Nette\Reflection\AnnotationsParser::expandClassName($annotationValue, $prop->getDeclaringClass());

So the problem is that the getDeclaringClass doesn't return reflection to trait? Well, that seems like a bug of php to me. But okey, do you know how to fix it? Can you please send a pullrq? :)

I'll try to add a static helper for this into Nette\Reflection first. ;-)

Good idea :)

@fprochazka The reflection PR is merged, I'll send a PR for this when the commit appears in stable nette.

Thank you :)

Fixed by #18 ?

Yes, fixed. Forgot to close this one.