diff --git a/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php b/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php index 25446deb1e..d85132692a 100644 --- a/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php +++ b/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php @@ -269,7 +269,17 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property } try { - return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflectionMethod->getDeclaringClass())), $prefix]; + $reflector = $reflectionMethod->getDeclaringClass(); + + foreach ($reflector->getTraits() as $trait) { + if ($trait->hasMethod($methodName)) { + $reflector = $trait; + + break; + } + } + + return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflector)), $prefix]; } catch (\InvalidArgumentException $e) { return null; } catch (\RuntimeException $e) { diff --git a/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php b/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php index 4e3ff39257..9adfa6783a 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php @@ -295,6 +295,23 @@ class PhpDocExtractorTest extends TestCase ]; } + /** + * @dataProvider methodsDefinedByTraitsProvider + */ + public function testMethodsDefinedByTraits(string $property, Type $type) + { + $this->assertEquals([$type], $this->extractor->getTypes(DummyUsingTrait::class, $property)); + } + + public function methodsDefinedByTraitsProvider(): array + { + return [ + ['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)], + ]; + } + /** * @dataProvider propertiesStaticTypeProvider */ diff --git a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php index 6284ebf105..0599d979c2 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/TraitUsage/DummyTrait.php @@ -29,4 +29,28 @@ trait DummyTrait * @var Dummy */ private $propertyInTraitObjectDifferentNamespace; + + /** + * @return string + */ + public function getMethodInTraitPrimitiveType() + { + return 'value'; + } + + /** + * @return DummyUsedInTrait + */ + public function getMethodInTraitObjectSameNamespace() + { + return new DummyUsedInTrait(); + } + + /** + * @return Dummy + */ + public function getMethodInTraitObjectDifferentNamespace() + { + return new Dummy(); + } }