Skip to content

Commit

Permalink
Reflection::getPropertyDeclaringClass() takes into account doc commen…
Browse files Browse the repository at this point in the history
…ts [Closes nette/di#169]
  • Loading branch information
dg committed Oct 30, 2018
1 parent 3c23141 commit 9389dec
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Utils/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ public static function getParameterDefaultValue(\ReflectionParameter $param)
public static function getPropertyDeclaringClass(\ReflectionProperty $prop): \ReflectionClass
{
foreach ($prop->getDeclaringClass()->getTraits() as $trait) {
if ($trait->hasProperty($prop->getName())) {
if ($trait->hasProperty($prop->getName())
&& $trait->getProperty($prop->getName())->getDocComment() === $prop->getDocComment()
) {
return self::getPropertyDeclaringClass($trait->getProperty($prop->getName()));
}
}
Expand Down
51 changes: 51 additions & 0 deletions tests/Utils/Reflection.getPropertyDeclaringClass.overwrite.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* Test: Nette\Utils\Reflection::getPropertyDeclaringClass
*/

declare(strict_types=1);

use Nette\Utils\Reflection;
use Tester\Assert;


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


trait A
{
protected $foo;
}

trait B
{
use A;

protected $foo;
}

class C
{
use B;

protected $foo;
}

class D extends C
{
protected $foo;
}


// Property in class
Assert::same('D', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('D', 'foo'))->getName());

// Property in class - wrong, but impossible to solve in PHP https://github.com/nette/di/issues/169
Assert::same('A', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('C', 'foo'))->getName());

// Property in trait - wrong, but impossible to solve in PHP https://github.com/nette/di/issues/169
Assert::same('A', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('B', 'foo'))->getName());

// Property in trait
Assert::same('A', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('A', 'foo'))->getName());
55 changes: 55 additions & 0 deletions tests/Utils/Reflection.getPropertyDeclaringClass.overwrite2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Test: Nette\Utils\Reflection::getPropertyDeclaringClass + doccomment workaround
*/

declare(strict_types=1);

use Nette\Utils\Reflection;
use Tester\Assert;


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


trait A
{
/** a */
protected $foo;
}

trait B
{
use A;

/** b */
protected $foo;
}

class C
{
use B;

/** c */
protected $foo;
}

class D extends C
{
/** d */
protected $foo;
}


// Property in class
Assert::same('D', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('D', 'foo'))->getName());

// Property in class
Assert::same('C', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('C', 'foo'))->getName());

// Property in trait
Assert::same('B', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('B', 'foo'))->getName());

// Property in trait
Assert::same('A', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('A', 'foo'))->getName());

0 comments on commit 9389dec

Please sign in to comment.