[PropertyInfo] fix array types with keys (array<string, string>)
This commit is contained in:
parent
ef19a03b2b
commit
64f7bd7832
@ -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,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -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',
|
||||
|
@ -130,6 +130,16 @@ class Dummy extends ParentDummy
|
||||
*/
|
||||
public $nestedIterators;
|
||||
|
||||
/**
|
||||
* @var array<string,string>
|
||||
*/
|
||||
public $arrayWithKeys;
|
||||
|
||||
/**
|
||||
* @var array<string,array<integer,null|string>|null>
|
||||
*/
|
||||
public $arrayWithKeysAndComplexValue;
|
||||
|
||||
public static function getStatic()
|
||||
{
|
||||
}
|
||||
|
@ -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;
|
||||
$collectionValueType = null;
|
||||
} else {
|
||||
$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<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;
|
||||
} else {
|
||||
$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
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user