diff --git a/Extractor/PhpStanExtractor.php b/Extractor/PhpStanExtractor.php index 5762e58..44888a6 100644 --- a/Extractor/PhpStanExtractor.php +++ b/Extractor/PhpStanExtractor.php @@ -232,6 +232,14 @@ private function getDocBlockFromProperty(string $class, string $property): ?arra return null; } + $reflector = $reflectionProperty->getDeclaringClass(); + + foreach ($reflector->getTraits() as $trait) { + if ($trait->hasProperty($property)) { + return $this->getDocBlockFromProperty($trait->getName(), $property); + } + } + // Type can be inside property docblock as `@var` $rawDocNode = $reflectionProperty->getDocComment(); $phpDocNode = $rawDocNode ? $this->getPhpDocNode($rawDocNode) : null; diff --git a/Tests/Extractor/PhpStanExtractorTest.php b/Tests/Extractor/PhpStanExtractorTest.php index 3c32c84..e2da679 100644 --- a/Tests/Extractor/PhpStanExtractorTest.php +++ b/Tests/Extractor/PhpStanExtractorTest.php @@ -21,6 +21,7 @@ use Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy; use Symfony\Component\PropertyInfo\Tests\Fixtures\Php80PromotedDummy; use Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem; +use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\AnotherNamespace\DummyInAnotherNamespace; use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsedInTrait; use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsingTrait; use Symfony\Component\PropertyInfo\Type; @@ -306,6 +307,7 @@ public static function propertiesDefinedByTraitsProvider(): array ['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)], + ['dummyInAnotherNamespace', new Type(Type::BUILTIN_TYPE_OBJECT, false, DummyInAnotherNamespace::class)], ]; } diff --git a/Tests/Fixtures/TraitUsage/AnotherNamespace/DummyInAnotherNamespace.php b/Tests/Fixtures/TraitUsage/AnotherNamespace/DummyInAnotherNamespace.php new file mode 100644 index 0000000..5ae6b60 --- /dev/null +++ b/Tests/Fixtures/TraitUsage/AnotherNamespace/DummyInAnotherNamespace.php @@ -0,0 +1,7 @@ +