8000 [PropertyInfo] Fix typed collections in PHP 7.4 · symfony/symfony@282ed28 · GitHub
[go: up one dir, main page]

Skip to content

Commit 282ed28

Browse files
committed
[PropertyInfo] Fix typed collections in PHP 7.4
1 parent 6c2a1c9 commit 282ed28

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,6 @@ public function getProperties($class, array $context = []): ?array
139139
*/
140140
public function getTypes($class, $property, array $context = []): ?array
141141
{
142-
if (\PHP_VERSION_ID >= 70400) {
143-
try {
144-
$reflectionProperty = new \ReflectionProperty($class, $property);
145-
$type = $reflectionProperty->getType();
146-
if (null !== $type) {
147-
return $this->extractFromReflectionType($type, $reflectionProperty->getDeclaringClass());
148-
}
149-
} catch (\ReflectionException $e) {
150-
// noop
151-
}
152-
}
153-
154142
if ($fromMutator = $this->extractFromMutator($class, $property)) {
155143
return $fromMutator;
156144
}
@@ -170,6 +158,18 @@ public function getTypes($class, $property, array $context = []): ?array
170158
return $fromDefaultValue;
171159
}
172160

161+
if (\PHP_VERSION_ID >= 70400) {
162+
try {
163+
$reflectionProperty = new \ReflectionProperty($class, $property);
164+
$type = $reflectionProperty->getType();
165+
if (null !== $type) {
166+
return $this->extractFromReflectionType($type, $reflectionProperty->getDeclaringClass());
167+
}
168+
} catch (\ReflectionException $e) {
169+
// noop
170+
}
171+
}
172+
173173
return null;
174174
}
175175

src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

Copy file name to clipboard
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,5 +398,6 @@ public function testTypedProperties(): void
398398
{
399399
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], $this->extractor->getTypes(Php74Dummy::class, 'dummy'));
400400
$this->assertEquals([new Type(Type::BUILTIN_TYPE_BOOL, true)], $this->extractor->getTypes(Php74Dummy::class, 'nullableBoolProp'));
401+
$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'));
401402
}
402403
}

src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php74Dummy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ class Php74Dummy
1818
{
1919
public Dummy $dummy;
2020
private ?bool $nullableBoolProp;
21+
/** @var string[] */
22+
private array $stringCollection;
23+
24+
public function addStringCollection(string $string): void
25+
{
26+
}
27+
28+
public function removeStringCollection(string $string): void
29+
{
30+
}
2131
}

0 commit comments

Comments
 (0)
0