From 64f7bd78323dd1a3d544a2b93674642d25593ede Mon Sep 17 00:00:00 2001 From: Markus Fasselt Date: Sat, 11 Jul 2020 15:51:13 +0200 Subject: [PATCH] [PropertyInfo] fix array types with keys (array) --- .../Tests/Extractor/PhpDocExtractorTest.php | 33 +++++++++++++++++++ .../Extractor/ReflectionExtractorTest.php | 6 ++++ .../PropertyInfo/Tests/Fixtures/Dummy.php | 10 ++++++ .../PropertyInfo/Util/PhpDocTypeHelper.php | 22 ++++++++++--- 4 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php b/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php index d352fa12b6..9c2d3a0abf 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php @@ -150,6 +150,39 @@ class PhpDocExtractorTest extends TestCase null, null, ], + [ + 'arrayWithKeys', + [new Type( + Type::BUILTIN_TYPE_ARRAY, + false, + null, + true, + new Type(Type::BUILTIN_TYPE_STRING), + new Type(Type::BUILTIN_TYPE_STRING) + )], + null, + null, + ], + [ + 'arrayWithKeysAndComplexValue', + [new Type( + Type::BUILTIN_TYPE_ARRAY, + false, + null, + true, + new Type(Type::BUILTIN_TYPE_STRING), + new Type( + Type::BUILTIN_TYPE_ARRAY, + true, + null, + true, + new Type(Type::BUILTIN_TYPE_INT), + new Type(Type::BUILTIN_TYPE_STRING, true) + ) + )], + null, + null, + ], ]; } diff --git a/src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php b/src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php index 353a41b829..a3c3d95f7b 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php @@ -60,6 +60,8 @@ class ReflectionExtractorTest extends TestCase 'iteratorCollection', 'iteratorCollectionWithKey', 'nestedIterators', + 'arrayWithKeys', + 'arrayWithKeysAndComplexValue', 'foo', 'foo2', 'foo3', @@ -108,6 +110,8 @@ class ReflectionExtractorTest extends TestCase 'iteratorCollection', 'iteratorCollectionWithKey', 'nestedIterators', + 'arrayWithKeys', + 'arrayWithKeysAndComplexValue', 'foo', 'foo2', 'foo3', @@ -146,6 +150,8 @@ class ReflectionExtractorTest extends TestCase 'iteratorCollection', 'iteratorCollectionWithKey', 'nestedIterators', + 'arrayWithKeys', + 'arrayWithKeysAndComplexValue', 'foo', 'foo2', 'foo3', diff --git a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php index bcec074438..90c6b2d795 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php @@ -130,6 +130,16 @@ class Dummy extends ParentDummy */ public $nestedIterators; + /** + * @var array + */ + public $arrayWithKeys; + + /** + * @var array|null> + */ + public $arrayWithKeysAndComplexValue; + public static function getStatic() { } diff --git a/src/Symfony/Component/PropertyInfo/Util/PhpDocTypeHelper.php b/src/Symfony/Component/PropertyInfo/Util/PhpDocTypeHelper.php index e7b06f1e32..01d023d055 100644 --- a/src/Symfony/Component/PropertyInfo/Util/PhpDocTypeHelper.php +++ b/src/Symfony/Component/PropertyInfo/Util/PhpDocTypeHelper.php @@ -12,6 +12,7 @@ namespace Symfony\Component\PropertyInfo\Util; use phpDocumentor\Reflection\Type as DocType; +use phpDocumentor\Reflection\Types\Array_; use phpDocumentor\Reflection\Types\Collection; use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Null_; @@ -109,12 +110,23 @@ final class PhpDocTypeHelper } if ('[]' === substr($docType, -2)) { - if ('mixed[]' === $docType) { - $collectionKeyType = null; + $collectionKeyType = new Type(Type::BUILTIN_TYPE_INT); + $collectionValueType = $this->createType($type, false, substr($docType, 0, -2)); + + return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyType, $collectionValueType); + } + + if (0 === strpos($docType, 'array<') && $type instanceof Array_) { + // array is converted to x[] which is handled above + // so it's only necessary to handle array here + $collectionKeyType = $this->getTypes($type->getKeyType())[0]; + + $collectionValueTypes = $this->getTypes($type->getValueType()); + if (\count($collectionValueTypes) > 1) { + // the Type class does not support union types yet, so assume that no type was defined $collectionValueType = null; } else { - $collectionKeyType = new Type(Type::BUILTIN_TYPE_INT); - $collectionValueType = $this->createType($type, false, substr($docType, 0, -2)); + $collectionValueType = $collectionValueTypes[0]; } return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyType, $collectionValueType); @@ -160,6 +172,6 @@ final class PhpDocTypeHelper return [$docType, null]; } - return ['object', substr($docType, 1)]; + return ['object', substr($docType, 1)]; // substr to strip the namespace's `\`-prefix } }