diff --git a/src/DI/Helpers.php b/src/DI/Helpers.php index 974726831..92b7710a3 100644 --- a/src/DI/Helpers.php +++ b/src/DI/Helpers.php @@ -159,7 +159,7 @@ public static function getInjectProperties(Nette\Reflection\ClassType $class, $c throw new Nette\InvalidStateException("Property $property has no @var annotation."); } - $type = Nette\Reflection\AnnotationsParser::expandClassName($type, $property->getDeclaringClass()); + $type = Nette\Reflection\AnnotationsParser::expandClassName($type, self::getDeclaringClass($property)); if (!class_exists($type) && !interface_exists($type)) { throw new Nette\InvalidStateException("Class or interface '$type' used in @var annotation at $property not found. Check annotation and 'use' statements."); } elseif ($container && !$container->getByType($type, FALSE)) { @@ -171,4 +171,20 @@ public static function getInjectProperties(Nette\Reflection\ClassType $class, $c } + /** + * Returns declaring class or trait. + * @return \ReflectionClass + */ + private static function getDeclaringClass(\ReflectionProperty $prop) + { + if (PHP_VERSION_ID >= 50400) { + foreach ($prop->getDeclaringClass()->getTraits() as $trait) { + if ($trait->hasProperty($prop->getName())) { + return self::getDeclaringClass($trait->getProperty($prop->getName())); + } + } + } + return $prop->getDeclaringClass(); + } + } diff --git a/tests/DI/Helpers.getInjectProperties().traits.phpt b/tests/DI/Helpers.getInjectProperties().traits.phpt new file mode 100644 index 000000000..4d519937e --- /dev/null +++ b/tests/DI/Helpers.getInjectProperties().traits.phpt @@ -0,0 +1,49 @@ + 'A\AInjected', + ), Helpers::getInjectProperties($refC) ); +}