Skip to content

Commit 9389dec

Browse files
committed
Reflection::getPropertyDeclaringClass() takes into account doc comments [Closes nette/di#169]
1 parent 3c23141 commit 9389dec

3 files changed

+109
-1
lines changed

src/Utils/Reflection.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ public static function getParameterDefaultValue(\ReflectionParameter $param)
100100
public static function getPropertyDeclaringClass(\ReflectionProperty $prop): \ReflectionClass
101101
{
102102
foreach ($prop->getDeclaringClass()->getTraits() as $trait) {
103-
if ($trait->hasProperty($prop->getName())) {
103+
if ($trait->hasProperty($prop->getName())
104+
&& $trait->getProperty($prop->getName())->getDocComment() === $prop->getDocComment()
105+
) {
104106
return self::getPropertyDeclaringClass($trait->getProperty($prop->getName()));
105107
}
106108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\Reflection::getPropertyDeclaringClass
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Utils\Reflection;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
trait A
17+
{
18+
protected $foo;
19+
}
20+
21+
trait B
22+
{
23+
use A;
24+
25+
protected $foo;
26+
}
27+
28+
class C
29+
{
30+
use B;
31+
32+
protected $foo;
33+
}
34+
35+
class D extends C
36+
{
37+
protected $foo;
38+
}
39+
40+
41+
// Property in class
42+
Assert::same('D', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('D', 'foo'))->getName());
43+
44+
// Property in class - wrong, but impossible to solve in PHP https://github.com/nette/di/issues/169
45+
Assert::same('A', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('C', 'foo'))->getName());
46+
47+
// Property in trait - wrong, but impossible to solve in PHP https://github.com/nette/di/issues/169
48+
Assert::same('A', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('B', 'foo'))->getName());
49+
50+
// Property in trait
51+
Assert::same('A', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('A', 'foo'))->getName());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\Reflection::getPropertyDeclaringClass + doccomment workaround
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Utils\Reflection;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
trait A
17+
{
18+
/** a */
19+
protected $foo;
20+
}
21+
22+
trait B
23+
{
24+
use A;
25+
26+
/** b */
27+
protected $foo;
28+
}
29+
30+
class C
31+
{
32+
use B;
33+
34+
/** c */
35+
protected $foo;
36+
}
37+
38+
class D extends C
39+
{
40+
/** d */
41+
protected $foo;
42+
}
43+
44+
45+
// Property in class
46+
Assert::same('D', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('D', 'foo'))->getName());
47+
48+
// Property in class
49+
Assert::same('C', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('C', 'foo'))->getName());
50+
51+
// Property in trait
52+
Assert::same('B', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('B', 'foo'))->getName());
53+
54+
// Property in trait
55+
Assert::same('A', Reflection::getPropertyDeclaringClass(new \ReflectionProperty('A', 'foo'))->getName());

0 commit comments

Comments
 (0)