Skip to content

Commit 470ac26

Browse files
committed
bug #28055 [PropertyInfo] Allow nested collections (jderusse)
This PR was merged into the 2.8 branch. Discussion ---------- [PropertyInfo] Allow nested collections | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Duplicate of #28012 for the 2.8 branche (as both code and test have been refactored between 2.8 and 3.x Commits ------- 6331687 Allow multidimensional collection in property info
2 parents 5d8bf16 + 6331687 commit 470ac26

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -319,30 +319,28 @@ private function createType($docType, $nullable)
319319
{
320320
// Cannot guess
321321
if (!$docType || 'mixed' === $docType) {
322-
return;
323-
}
324-
325-
if ($collection = '[]' === substr($docType, -2)) {
326-
$docType = substr($docType, 0, -2);
322+
return null;
327323
}
328324

329-
$docType = $this->normalizeType($docType);
330-
list($phpType, $class) = $this->getPhpTypeAndClass($docType);
331-
332-
$array = 'array' === $docType;
333-
334-
if ($collection || $array) {
335-
if ($array || 'mixed' === $docType) {
325+
if ('[]' === substr($docType, -2)) {
326+
if ('mixed[]' === $docType) {
336327
$collectionKeyType = null;
337328
$collectionValueType = null;
338329
} else {
339330
$collectionKeyType = new Type(Type::BUILTIN_TYPE_INT);
340-
$collectionValueType = new Type($phpType, $nullable, $class);
331+
$collectionValueType = $this->createType(substr($docType, 0, -2), $nullable);
341332
}
342333

343334
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyType, $collectionValueType);
344335
}
345336

337+
$docType = $this->normalizeType($docType);
338+
list($phpType, $class) = $this->getPhpTypeAndClass($docType);
339+
340+
if ('array' === $docType) {
341+
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, null, null);
342+
}
343+
346344
return new Type($phpType, $nullable, $class);
347345
}
348346

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public function typesProvider()
6262
array('bal', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime')), null, null),
6363
array('parent', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy')), null, null),
6464
array('collection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime'))), null, null),
65+
array('mixedCollection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, null, null)), null, null),
66+
array('nestedCollection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING, false)))), null, null),
6567
array('a', array(new Type(Type::BUILTIN_TYPE_INT)), 'A.', null),
6668
array('b', array(new Type(Type::BUILTIN_TYPE_OBJECT, true, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy')), 'B.', null),
6769
array('c', array(new Type(Type::BUILTIN_TYPE_BOOL, true)), null, null),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public function testGetProperties()
3737
'bal',
3838
'parent',
3939
'collection',
40+
'nestedCollection',
41+
'mixedCollection',
4042
'B',
4143
'Guid',
4244
'g',

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ class Dummy extends ParentDummy
4646
*/
4747
public $collection;
4848

49+
/**
50+
* @var string[][]
51+
*/
52+
public $nestedCollection;
53+
54+
/**
55+
* @var mixed[]
56+
*/
57+
public $mixedCollection;
58+
4959
/**
5060
* @var ParentDummy
5161
*/

0 commit comments

Comments
 (0)