8000 bug #38041 [PropertyInfo] Fix typed collections in PHP 7.4 (ndench) · symfony/symfony@4482fcf · GitHub
[go: up one dir, main page]

Skip to content

Commit 4482fcf

Browse files
committed
bug #38041 [PropertyInfo] Fix typed collections in PHP 7.4 (ndench)
This PR was merged into the 4.4 branch. Discussion ---------- [PropertyInfo] Fix typed collections in PHP 7.4 | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | No ticket | License | MIT | Doc PR | N/A #37971 introduced support for typed properties in PHP 7.4, however by short circuiting the `getTypes()` method, typed collections are returned as `Type::BUILTIN_TYPE_ARRAY` without a proper collection type. If running on PHP < 7.4, the `getMutator()` method would be called which would extract the collection type from the getter/setter or adder/remover. I updated the typedPropertiesTest to cover this case. Commits ------- 282ed28 [PropertyInfo] Fix typed collections in PHP 7.4
2 parents 067dc2d + 282ed28 commit 4482fcf

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

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