Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,39 @@ public function provideCollectionTypes()
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,
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function testGetProperties()
'iteratorCollection',
'iteratorCollectionWithKey',
'nestedIterators',
'arrayWithKeys',
'arrayWithKeysAndComplexValue',
'foo',
'foo2',
'foo3',
Expand Down Expand Up @@ -108,6 +110,8 @@ public function testGetPropertiesWithCustomPrefixes()
'iteratorCollection',
'iteratorCollectionWithKey',
'nestedIterators',
'arrayWithKeys',
'arrayWithKeysAndComplexValue',
'foo',
'foo2',
'foo3',
Expand Down Expand Up @@ -146,6 +150,8 @@ public function testGetPropertiesWithNoPrefixes()
'iteratorCollection',
'iteratorCollectionWithKey',
'nestedIterators',
'arrayWithKeys',
'arrayWithKeysAndComplexValue',
'foo',
'foo2',
'foo3',
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
Expand Down
22 changes: 17 additions & 5 deletions src/Symfony/Component/PropertyInfo/Util/PhpDocTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -109,12 +110,23 @@ private function createType(DocType $type, bool $nullable, string $docType = nul
}

if ('[]' === substr($docType, -2)) {
if ('mixed[]' === $docType) {
$collectionKeyType = null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code was never executed. in case of a mixed[] type on the variable, $docType was array, so I removed this code.

$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 {
$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);
Expand Down Expand Up @@ -160,6 +172,6 @@ private function getPhpTypeAndClass(string $docType): array
return [$docType, null];
}

return ['object', substr($docType, 1)];
return ['object', substr($docType, 1)]; // substr to strip the namespace's `\`-prefix
}
}