From 888687d643d05b651b6db2b024dddbc7d2f462e7 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 30 Oct 2018 17:04:21 +0100 Subject: [PATCH] Reflection::getPropertyDeclaringClass() takes into account doc comments [Closes nette/utils#169] --- src/Utils/Reflection.php | 4 +- ...n.getPropertyDeclaringClass.overwrite.phpt | 51 +++++++++++++++++ ....getPropertyDeclaringClass.overwrite2.phpt | 55 +++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 tests/Utils/Reflection.getPropertyDeclaringClass.overwrite.phpt create mode 100644 tests/Utils/Reflection.getPropertyDeclaringClass.overwrite2.phpt diff --git a/src/Utils/Reflection.php b/src/Utils/Reflection.php index 5e43db2eb..14e58c7a5 100644 --- a/src/Utils/Reflection.php +++ b/src/Utils/Reflection.php @@ -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())); } } diff --git a/tests/Utils/Reflection.getPropertyDeclaringClass.overwrite.phpt b/tests/Utils/Reflection.getPropertyDeclaringClass.overwrite.phpt new file mode 100644 index 000000000..80bf78c2c --- /dev/null +++ b/tests/Utils/Reflection.getPropertyDeclaringClass.overwrite.phpt @@ -0,0 +1,51 @@ +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()); diff --git a/tests/Utils/Reflection.getPropertyDeclaringClass.overwrite2.phpt b/tests/Utils/Reflection.getPropertyDeclaringClass.overwrite2.phpt new file mode 100644 index 000000000..f4f30e9b6 --- /dev/null +++ b/tests/Utils/Reflection.getPropertyDeclaringClass.overwrite2.phpt @@ -0,0 +1,55 @@ +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());