8000 [PropertyInfo] Fix phpDocExtractor nullable array value type by MudrakIvan · Pull Request #49557 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyInfo] Fix phpDocExtractor nullable array value type #49557

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
May 5, 2023
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 @@ -169,7 +169,7 @@ public function testExtractCollection($property, array $type = null, $shortDescr
public static function provideCollectionTypes()
{
return [
['iteratorCollection', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Iterator', true, null, new Type(Type::BUILTIN_TYPE_STRING))], null, null],
['iteratorCollection', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Iterator', true, [new Type(Type::BUILTIN_TYPE_STRING), new Type(Type::BUILTIN_TYPE_INT)], new Type(Type::BUILTIN_TYPE_STRING))], null, null],
['iteratorCollectionWithKey', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Iterator', true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))], null, null],
[
'nestedIterators',
Expand Down Expand Up @@ -265,6 +265,8 @@ public static function typesWithCustomPrefixesProvider()
['i', [new Type(Type::BUILTIN_TYPE_STRING, true), new Type(Type::BUILTIN_TYPE_INT, true)], null, null],
['j', [new Type(Type::BUILTIN_TYPE_OBJECT, true, 'DateTime')], null, null],
['nullableCollectionOfNonNullableElements', [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_INT, false))], null, null],
['nonNullableCollectionOfNullableElements', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_INT, true))], null, null],
['nullableCollectionOfMultipleNonNullableElementTypes', [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true, new Type(Type::BUILTIN_TYPE_INT), [new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)])], null, null],
['donotexist', null, null, null],
['staticGetter', null, null, null],
['staticSetter', null, null, null],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function testGetProperties()
'i',
'j',
'nullableCollectionOfNonNullableElements',
'nonNullableCollectionOfNullableElements',
'nullableCollectionOfMultipleNonNullableElementTypes',
'emptyVar',
'iteratorCollection',
'iteratorCollectionWithKey',
Expand Down Expand Up @@ -124,6 +126,8 @@ public function testGetPropertiesWithCustomPrefixes()
'i',
'j',
'nullableCollectionOfNonNullableElements',
'nonNullableCollectionOfNullableElements',
'nullableCollectionOfMultipleNonNullableElementTypes',
'emptyVar',
'iteratorCollection',
'iteratorCollectionWithKey',
Expand Down Expand Up @@ -175,6 +179,8 @@ public function testGetPropertiesWithNoPrefixes()
'i',
'j',
'nullableCollectionOfNonNullableElements',
'nonNullableCollectionOfNullableElements',
'nullableCollectionOfMultipleNonNullableElementTypes',
'emptyVar',
'iteratorCollection',
'iteratorCollectionWithKey',
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 @@ -98,6 +98,16 @@ class Dummy extends ParentDummy
*/
public $nullableCollectionOfNonNullableElements;

/**
* @var array<null|int>
*/
public $nonNullableCollectionOfNullableElements;

/**
* @var null|array<int|string>
*/
public $nullableCollectionOfMultipleNonNullableElementTypes;

/**
* @var array
*/
Expand Down
30 changes: 9 additions & 21 deletions src/Symfony/Component/PropertyInfo/Util/PhpDocTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,43 +115,31 @@ private function createType(DocType $type, bool $nullable, string $docType = nul

[$phpType, $class] = $this->getPhpTypeAndClass((string) $fqsen);

$key = $this->getTypes($type->getKeyType());
$value = $this->getTypes($type->getValueType());
$keys = $this->getTypes($type->getKeyType());
$values = $this->getTypes($type->getValueType());

// More than 1 type returned means it is a Compound type, which is
// not handled by Type, so better use a null value.
$key = 1 === \count($key) ? $key[0] : null;
$value = 1 === \count($value) ? $value[0] : null;

return new Type($phpType, $nullable, $class, true, $key, $value);
return new Type($phpType, $nullable, $class, true, $keys, $values);
}

// Cannot guess
if (!$docType || 'mixed' === $docType) {
return null;
}

if (str_ends_with($docType, '[]')) {
$collectionKeyType = new Type(Type::BUILTIN_TYPE_INT);
$collectionValueType = $this->createType($type, false, substr($docType, 0, -2));
if (str_ends_with($docType, '[]') && $type instanceof Array_) {
$collectionKeyTypes = new Type(Type::BUILTIN_TYPE_INT);
$collectionValueTypes = $this->getTypes($type->getValueType());

return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyType, $collectionValueType);
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyTypes, $collectionValueTypes);
}

if ((str_starts_with($docType, 'list<') || str_starts_with($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];

$collectionKeyTypes = $this->getTypes($type->getKeyType());
$collectionValueTypes = $this->getTypes($type->getValueType());
if (1 != \count($collectionValueTypes)) {
// 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);
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyTypes, $collectionValueTypes);
}

$docType = $this->normalizeType($docType);
Expand Down
0