[PropertyInfo] fix array types with keys (array<string, string>)

This commit is contained in:
Markus Fasselt 2020-07-11 15:51:13 +02:00
parent ef19a03b2b
commit 64f7bd7832
4 changed files with 66 additions and 5 deletions

View File

@ -150,6 +150,39 @@ class PhpDocExtractorTest extends TestCase
null, null,
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,
],
]; ];
} }

View File

@ -60,6 +60,8 @@ class ReflectionExtractorTest extends TestCase
'iteratorCollection', 'iteratorCollection',
'iteratorCollectionWithKey', 'iteratorCollectionWithKey',
'nestedIterators', 'nestedIterators',
'arrayWithKeys',
'arrayWithKeysAndComplexValue',
'foo', 'foo',
'foo2', 'foo2',
'foo3', 'foo3',
@ -108,6 +110,8 @@ class ReflectionExtractorTest extends TestCase
'iteratorCollection', 'iteratorCollection',
'iteratorCollectionWithKey', 'iteratorCollectionWithKey',
'nestedIterators', 'nestedIterators',
'arrayWithKeys',
'arrayWithKeysAndComplexValue',
'foo', 'foo',
'foo2', 'foo2',
'foo3', 'foo3',
@ -146,6 +150,8 @@ class ReflectionExtractorTest extends TestCase
'iteratorCollection', 'iteratorCollection',
'iteratorCollectionWithKey', 'iteratorCollectionWithKey',
'nestedIterators', 'nestedIterators',
'arrayWithKeys',
'arrayWithKeysAndComplexValue',
'foo', 'foo',
'foo2', 'foo2',
'foo3', 'foo3',

View File

@ -130,6 +130,16 @@ class Dummy extends ParentDummy
*/ */
public $nestedIterators; public $nestedIterators;
/**
* @var array<string,string>
*/
public $arrayWithKeys;
/**
* @var array<string,array<integer,null|string>|null>
*/
public $arrayWithKeysAndComplexValue;
public static function getStatic() public static function getStatic()
{ {
} }

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\PropertyInfo\Util; namespace Symfony\Component\PropertyInfo\Util;
use phpDocumentor\Reflection\Type as DocType; use phpDocumentor\Reflection\Type as DocType;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Collection; use phpDocumentor\Reflection\Types\Collection;
use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Compound;
use phpDocumentor\Reflection\Types\Null_; use phpDocumentor\Reflection\Types\Null_;
@ -109,12 +110,23 @@ final class PhpDocTypeHelper
} }
if ('[]' === substr($docType, -2)) { if ('[]' === substr($docType, -2)) {
if ('mixed[]' === $docType) { $collectionKeyType = new Type(Type::BUILTIN_TYPE_INT);
$collectionKeyType = null; $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<value> is converted to x[] which is handled above
// so it's only necessary to handle array<key, value> 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; $collectionValueType = null;
} else { } else {
$collectionKeyType = new Type(Type::BUILTIN_TYPE_INT); $collectionValueType = $collectionValueTypes[0];
$collectionValueType = $this->createType($type, false, substr($docType, 0, -2));
} }
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyType, $collectionValueType); return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyType, $collectionValueType);
@ -160,6 +172,6 @@ final class PhpDocTypeHelper
return [$docType, null]; return [$docType, null];
} }
return ['object', substr($docType, 1)]; return ['object', substr($docType, 1)]; // substr to strip the namespace's `\`-prefix
} }
} }