From bbadfb34cc50f60cace6d4390613ed8d01fc1c13 Mon Sep 17 00:00:00 2001 From: Sullivan SENECHAL Date: Mon, 19 Apr 2021 18:20:38 +0200 Subject: [PATCH] [PropertyInfo] fix attribute namespace with recursive traits --- .../Extractor/PhpDocExtractor.php | 28 ++++------ .../Tests/Extractor/PhpDocExtractorTest.php | 6 ++ .../Tests/Fixtures/DummyTraitExternal.php | 56 +++++++++++++++++++ .../Tests/Fixtures/TraitUsage/DummyTrait.php | 3 + 4 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 src/Symfony/Component/PropertyInfo/Tests/Fixtures/DummyTraitExternal.php diff --git a/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php b/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php index d85132692a..2f36a017c5 100644 --- a/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php +++ b/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php @@ -220,17 +220,15 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property return null; } - try { - $reflector = $reflectionProperty->getDeclaringClass(); + $reflector = $reflectionProperty->getDeclaringClass(); - foreach ($reflector->getTraits() as $trait) { - if ($trait->hasProperty($property)) { - $reflector = $trait; - - break; - } + foreach ($reflector->getTraits() as $trait) { + if ($trait->hasProperty($property)) { + return $this->getDocBlockFromProperty($trait->getName(), $property); } + } + try { return $this->docBlockFactory->create($reflectionProperty, $this->createFromReflector($reflector)); } catch (\InvalidArgumentException $e) { return null; @@ -268,17 +266,15 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property return null; } - try { - $reflector = $reflectionMethod->getDeclaringClass(); + $reflector = $reflectionMethod->getDeclaringClass(); - foreach ($reflector->getTraits() as $trait) { - if ($trait->hasMethod($methodName)) { - $reflector = $trait; - - break; - } + foreach ($reflector->getTraits() as $trait) { + if ($trait->hasMethod($methodName)) { + return $this->getDocBlockFromMethod($trait->getName(), $ucFirstProperty, $type); } + } + try { return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflector)), $prefix]; } catch (\InvalidArgumentException $e) { return null; diff --git a/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php b/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php index 9adfa6783a..4a141642d0 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php @@ -292,6 +292,9 @@ class PhpDocExtractorTest extends TestCase ['propertyInTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)], ['propertyInTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)], ['propertyInTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], + ['propertyInExternalTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)], + ['propertyInExternalTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], + ['propertyInExternalTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)], ]; } @@ -309,6 +312,9 @@ class PhpDocExtractorTest extends TestCase ['methodInTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)], ['methodInTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)], ['methodInTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], + ['methodInExternalTraitPrimitiveType', new Type(Type::BUILTIN_TYPE_STRING)], + ['methodInExternalTraitObjectSameNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], + ['methodInExternalTraitObjectDifferentNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyUsedInTrait::class)], ]; } diff --git a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/DummyTraitExternal.php b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/DummyTraitExternal.php new file mode 100644 index 0000000000..cdd2c720cd --- /dev/null +++ b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/DummyTraitExternal.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\PropertyInfo\Tests\Fixtures; + +use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsedInTrait; + +trait DummyTraitExternal +{ + /** + * @var string + */ + private $propertyInExternalTraitPrimitiveType; + + /** + * @var Dummy + */ + private $propertyInExternalTraitObjectSameNamespace; + + /** + * @var DummyUsedInTrait + */ + private $propertyInExternalTraitObjectDifferentNamespace; + + /** + * @return string + */ + public function getMethodInExternalTraitPrimitiveType() + { + return 'value'; + } + + /** + * @return Dummy + */ + public function getMethodInExternalTraitObjectSameNamespace() + { + return new Dummy(); + } + + /** + * @return DummyUsedInTrait + */ + public function getMethodInExternalTraitObjectDifferentNamespace() + { + return new DummyUsedInTrait(); + } +} diff --git a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php index 0599d979c2..ed0f1e5ae5 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php @@ -12,9 +12,12 @@ namespace Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage; use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy; +use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyTraitExternal; trait DummyTrait { + use DummyTraitExternal; + /** * @var string */