Skip to content

Commit 72b9513

Browse files
committed
bug symfony#57802 [PropertyInfo] Fix nullable value returned from extractFromMutator on CollectionType (benjilebon)
This PR was merged into the 5.4 branch. Discussion ---------- [PropertyInfo] Fix nullable value returned from extractFromMutator on CollectionType | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#57707 | License | MIT Follow-up on symfony#57708 to provide the fix for the legacy type system on the 5.4 branch Commits ------- 6d77170 [PropertyInfo] Fix nullable value returned from extractFromMutator on CollectionType
2 parents d83afb2 + 6d77170 commit 72b9513

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ private function extractFromMutator(string $class, string $property): ?array
458458
$type = $this->extractFromReflectionType($reflectionType, $reflectionMethod->getDeclaringClass());
459459

460460
if (1 === \count($type) && \in_array($prefix, $this->arrayMutatorPrefixes)) {
461-
$type = [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), $type[0])];
461+
$type = [new Type(Type::BUILTIN_TYPE_ARRAY, $this->isNullableProperty($class, $property), null, true, new Type(Type::BUILTIN_TYPE_INT), $type[0])];
462462
}
463463

464464
return $type;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ public function testTypedProperties()
523523
$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'));
524524
$this->assertEquals([new Type(Type::BUILTIN_TYPE_INT, true)], $this->extractor->getTypes(Php74Dummy::class, 'nullableWithDefault'));
525525
$this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)], $this->extractor->getTypes(Php74Dummy::class, 'collection'));
526+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class))], $this->extractor->getTypes(Php74Dummy::class, 'nullableTypedCollection'));
526527
}
527528

528529
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ class Php74Dummy
2323
private ?int $nullableWithDefault = 1;
2424
public array $collection = [];
2525

26+
/** @var Dummy[]|null */
27+
public ?array $nullableTypedCollection = null;
28+
2629
public function addStringCollection(string $string): void
2730
{
2831
}
2932

3033
public function removeStringCollection(string $string): void
3134
{
3235
}
36+
37+
public function addNullableTypedCollection(Dummy $dummy): void
38+
{
39+
}
3340
}

0 commit comments

Comments
 (0)