Skip to content

Commit 31b68fa

Browse files
benjilebonfabpot
authored andcommitted
[PropertyInfo] Check if property is nullable when using ReflectionExtractor
1 parent 89bd1b4 commit 31b68fa

File tree

5 files changed

+21
-1
lines changed

5 files changed

+21
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function getType(string $class, string $property, array $context = []): ?
202202
$type = $this->typeResolver->resolve($mutatorReflection->getParameters()[0]);
203203

204204
if (!$type instanceof CollectionType && \in_array($prefix, $this->arrayMutatorPrefixes, true)) {
205-
$type = Type::list($type);
205+
$type = $this->isNullableProperty($class, $property) ? Type::nullable(Type::list($type)) : Type::list($type);
206206
}
207207

208208
return $type;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public static function provideLegacyTypes()
164164
['listOfStrings', [new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, false, null, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), new LegacyType(LegacyType::BUILTIN_TYPE_STRING))], null, null],
165165
['self', [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, Dummy::class)], null, null],
166166
['collectionAsObject', [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, DummyCollection::class, true, [new LegacyType(LegacyType::BUILTIN_TYPE_INT)], [new LegacyType(LegacyType::BUILTIN_TYPE_STRING)])], null, null],
167+
['nullableTypedCollection', [new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, true, null, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, Dummy::class))], null, null],
167168
];
168169
}
169170

@@ -548,6 +549,7 @@ public static function typeProvider(): iterable
548549
yield ['collection', Type::list(Type::object(\DateTimeImmutable::class)), null, null];
549550
yield ['nestedCollection', Type::list(Type::list(Type::string())), null, null];
550551
yield ['mixedCollection', Type::array(), null, null];
552+
yield ['nullableTypedCollection', Type::nullable(Type::list(Type::object(Dummy::class))), null, null];
551553
yield ['a', Type::int(), 'A.', null];
552554
yield ['b', Type::nullable(Type::object(ParentDummy::class)), 'B.', null];
553555
yield ['c', Type::nullable(Type::bool()), null, null];

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function testGetProperties()
7676
'listOfStrings',
7777
'parentAnnotation',
7878
'genericInterface',
79+
'nullableTypedCollection',
7980
'foo',
8081
'foo2',
8182
'foo3',
@@ -143,6 +144,7 @@ public function testGetPropertiesWithCustomPrefixes()
143144
'listOfStrings',
144145
'parentAnnotation',
145146
'genericInterface',
147+
'nullableTypedCollection',
146148
'foo',
147149
'foo2',
148150
'foo3',
@@ -199,6 +201,7 @@ public function testGetPropertiesWithNoPrefixes()
199201
'listOfStrings',
200202
'parentAnnotation',
201203
'genericInterface',
204+
'nullableTypedCollection',
202205
'foo',
203206
'foo2',
204207
'foo3',
@@ -865,6 +868,7 @@ public function testTypedProperties()
865868
$this->assertEquals(Type::list(Type::string()), $this->extractor->getType(Php74Dummy::class, 'stringCollection'));
866869
$this->assertEquals(Type::nullable(Type::int()), $this->extractor->getType(Php74Dummy::class, 'nullableWithDefault'));
867870
$this->assertEquals(Type::array(), $this->extractor->getType(Php74Dummy::class, 'collection'));
871+
$this->assertEquals(Type::nullable(Type::list(Type::object(Dummy::class))), $this->extractor->getType(Php74Dummy::class, 'nullableTypedCollection'));
868872
}
869873

870874
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ class Dummy extends ParentDummy
177177
*/
178178
public $genericInterface;
179179

180+
/** @var Dummy[]|null */
181+
public $nullableTypedCollection = null;
182+
180183
public static function getStatic()
181184
{
182185
}
@@ -269,4 +272,8 @@ public function addDate(\DateTimeImmutable $date)
269272
public function hasElement(string $element): bool
270273
{
271274
}
275+
276+
public function addNullableTypedCollection(Dummy $dummy): void
277+
{
278+
}
272279
}

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)