From 282ed2850c69a438618e65945e93818d07b74e45 Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Thu, 3 Sep 2020 10:26:19 +1000 Subject: [PATCH] [PropertyInfo] Fix typed collections in PHP 7.4 --- .../Extractor/ReflectionExtractor.php | 24 +++++++++---------- .../Extractor/ReflectionExtractorTest.php | 1 + .../Tests/Fixtures/Php74Dummy.php | 10 ++++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php b/src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php index 035a460e77..c5a690cc21 100644 --- a/src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php +++ b/src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php @@ -139,18 +139,6 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp */ public function getTypes($class, $property, array $context = []): ?array { - if (\PHP_VERSION_ID >= 70400) { - try { - $reflectionProperty = new \ReflectionProperty($class, $property); - $type = $reflectionProperty->getType(); - if (null !== $type) { - return $this->extractFromReflectionType($type, $reflectionProperty->getDeclaringClass()); - } - } catch (\ReflectionException $e) { - // noop - } - } - if ($fromMutator = $this->extractFromMutator($class, $property)) { return $fromMutator; } @@ -170,6 +158,18 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp return $fromDefaultValue; } + if (\PHP_VERSION_ID >= 70400) { + try { + $reflectionProperty = new \ReflectionProperty($class, $property); + $type = $reflectionProperty->getType(); + if (null !== $type) { + return $this->extractFromReflectionType($type, $reflectionProperty->getDeclaringClass()); + } + } catch (\ReflectionException $e) { + // noop + } + } + return null; } diff --git a/src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php b/src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php index e15574f619..08cb2bcc5f 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php @@ -398,5 +398,6 @@ class ReflectionExtractorTest extends TestCase { $this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], $this->extractor->getTypes(Php74Dummy::class, 'dummy')); $this->assertEquals([new Type(Type::BUILTIN_TYPE_BOOL, true)], $this->extractor->getTypes(Php74Dummy::class, 'nullableBoolProp')); + $this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))], $this->extractor->getTypes(Php74Dummy::class, 'stringCollection')); } } diff --git a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php74Dummy.php b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php74Dummy.php index 9d3146442d..ffc4f4cd37 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php74Dummy.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php74Dummy.php @@ -18,4 +18,14 @@ class Php74Dummy { public Dummy $dummy; private ?bool $nullableBoolProp; + /** @var string[] */ + private array $stringCollection; + + public function addStringCollection(string $string): void + { + } + + public function removeStringCollection(string $string): void + { + } }