From bf257e78b0471e67757af038344919f498af804d Mon Sep 17 00:00:00 2001 From: Mathias Arlaud Date: Fri, 19 Apr 2024 17:22:54 +0200 Subject: [PATCH] [PropertyInfo] Fix PHPStan properties type in trait --- Extractor/PhpStanExtractor.php | 8 ++++++++ Tests/Extractor/PhpStanExtractorTest.php | 2 ++ .../AnotherNamespace/DummyInAnotherNamespace.php | 7 +++++++ .../AnotherNamespace/DummyTraitInAnotherNamespace.php | 11 +++++++++++ Tests/Fixtures/TraitUsage/DummyUsingTrait.php | 3 +++ 5 files changed, 31 insertions(+) create mode 100644 Tests/Fixtures/TraitUsage/AnotherNamespace/DummyInAnotherNamespace.php create mode 100644 Tests/Fixtures/TraitUsage/AnotherNamespace/DummyTraitInAnotherNamespace.php diff --git a/Extractor/PhpStanExtractor.php b/Extractor/PhpStanExtractor.php index 2f16969..0596eb2 100644 --- a/Extractor/PhpStanExtractor.php +++ b/Extractor/PhpStanExtractor.php @@ -233,6 +233,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); + } + } + if (null === $rawDocNode = $reflectionProperty->getDocComment() ?: null) { return null; } diff --git a/Tests/Extractor/PhpStanExtractorTest.php b/Tests/Extractor/PhpStanExtractorTest.php index d8fa9b9..fd11fcb 100644 --- a/Tests/Extractor/PhpStanExtractorTest.php +++ b/Tests/Extractor/PhpStanExtractorTest.php @@ -19,6 +19,7 @@ use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy; use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy; 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; @@ -311,6 +312,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 @@ +