Skip to content

Commit

Permalink
Fixed usage of inject annotations in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
enumag authored and dg committed Jan 6, 2015
1 parent 1b1c49f commit ff56672
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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();
}

}
49 changes: 49 additions & 0 deletions tests/DI/Helpers.getInjectProperties().traits.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* Test: Nette\DI\Helpers::getInjectProperties() with traits
* @phpversion 5.4
*/

namespace A
{
class AInjected
{

}
}

namespace B
{
use A\AInjected;

trait BTrait
{
/** @var AInjected @inject */
public $varA;
}
}

namespace C
{
class CClass
{
use \B\BTrait;
}
}

namespace
{
use Nette\DI\Helpers;
use Nette\Reflection\ClassType;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


$refC = ClassType::from('C\CClass');

Assert::same( array(
'varA' => 'A\AInjected',
), Helpers::getInjectProperties($refC) );
}

0 comments on commit ff56672

Please sign in to comment.