diff --git a/src/Symfony/Component/PropertyInfo/Type.php b/src/Symfony/Component/PropertyInfo/Type.php index 5798306735ca7..31ffaf388422c 100644 --- a/src/Symfony/Component/PropertyInfo/Type.php +++ b/src/Symfony/Component/PropertyInfo/Type.php @@ -179,16 +179,7 @@ public function getCollectionValueType(): ?self { trigger_deprecation('symfony/property-info', '5.3', 'The "%s()" method is deprecated, use "getCollectionValueTypes()" instead.', __METHOD__); - $type = $this->getCollectionValueTypes(); - if (0 === \count($type)) { - return null; - } - - if (\is_array($type)) { - [$type] = $type; - } - - return $type; + return $this->getCollectionValueTypes()[0] ?? null; } /** diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index 6a151c31b7647..9ab8c684c9cce 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -473,13 +473,13 @@ private function validateAndDenormalize(string $currentClass, string $attribute, if (null !== $collectionKeyType = $type->getCollectionKeyTypes()) { [$context['key_type']] = $collectionKeyType; } - } elseif ($type->isCollection() && null !== ($collectionValueType = $type->getCollectionValueTypes()) && \count($collectionValueType) > 0 && Type::BUILTIN_TYPE_ARRAY === $collectionValueType[0]->getBuiltinType()) { + } elseif ($type->isCollection() && \count($collectionValueType = $type->getCollectionValueTypes()) > 0 && Type::BUILTIN_TYPE_ARRAY === $collectionValueType[0]->getBuiltinType()) { // get inner type for any nested array [$innerType] = $collectionValueType; // note that it will break for any other builtinType $dimensions = '[]'; - while (null !== $innerType->getCollectionValueTypes() && Type::BUILTIN_TYPE_ARRAY === $innerType->getBuiltinType()) { + while (\count($innerType->getCollectionValueTypes()) > 0 && Type::BUILTIN_TYPE_ARRAY === $innerType->getBuiltinType()) { $dimensions .= '[]'; [$innerType] = $innerType->getCollectionValueTypes(); } diff --git a/src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php index 3f83da8d0517e..118c159445282 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php @@ -119,7 +119,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool } if (!$hasTypeConstraint) { if (1 === \count($builtinTypes)) { - if ($types[0]->isCollection() && (null !== $collectionValueType = $types[0]->getCollectionValueTypes())) { + if ($types[0]->isCollection() && \count($collectionValueType = $types[0]->getCollectionValueTypes()) > 0) { [$collectionValueType] = $collectionValueType; $this->handleAllConstraint($property, $allConstraint, $collectionValueType, $metadata); } diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/PropertyInfoLoaderEntity.php b/src/Symfony/Component/Validator/Tests/Fixtures/PropertyInfoLoaderEntity.php index 144a0016e84c9..2cc46e77de295 100644 --- a/src/Symfony/Component/Validator/Tests/Fixtures/PropertyInfoLoaderEntity.php +++ b/src/Symfony/Component/Validator/Tests/Fixtures/PropertyInfoLoaderEntity.php @@ -23,6 +23,7 @@ class PropertyInfoLoaderEntity public $scalar; public $object; public $collection; + public $collectionOfUnknown; /** * @Assert\Type(type="int") diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/PropertyInfoLoaderTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Loader/PropertyInfoLoaderTest.php index 767a5ab5c6978..0965d8e135fa0 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Loader/PropertyInfoLoaderTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/PropertyInfoLoaderTest.php @@ -44,6 +44,7 @@ public function testLoadClassMetadata() 'scalar', 'object', 'collection', + 'collectionOfUnknown', 'alreadyMappedType', 'alreadyMappedNotNull', 'alreadyMappedNotBlank', @@ -61,6 +62,7 @@ public function testLoadClassMetadata() [new Type(Type::BUILTIN_TYPE_STRING, true), new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_BOOL)], [new Type(Type::BUILTIN_TYPE_OBJECT, true, Entity::class)], [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, Entity::class))], + [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)], [new Type(Type::BUILTIN_TYPE_FLOAT, true)], // The existing constraint is float [new Type(Type::BUILTIN_TYPE_STRING, true)], [new Type(Type::BUILTIN_TYPE_STRING, true)], @@ -81,6 +83,7 @@ public function testLoadClassMetadata() true, true, true, + true, false, true )) @@ -135,6 +138,13 @@ public function testLoadClassMetadata() $this->assertInstanceOf(TypeConstraint::class, $collectionConstraints[0]->constraints[1]); $this->assertSame(Entity::class, $collectionConstraints[0]->constraints[1]->type); + $collectionOfUnknownMetadata = $classMetadata->getPropertyMetadata('collectionOfUnknown'); + $this->assertCount(1, $collectionOfUnknownMetadata); + $collectionOfUnknownConstraints = $collectionOfUnknownMetadata[0]->getConstraints(); + $this->assertCount(1, $collectionOfUnknownConstraints); + $this->assertInstanceOf(TypeConstraint::class, $collectionOfUnknownConstraints[0]); + $this->assertSame('array', $collectionOfUnknownConstraints[0]->type); + $alreadyMappedTypeMetadata = $classMetadata->getPropertyMetadata('alreadyMappedType'); $this->assertCount(1, $alreadyMappedTypeMetadata); $alreadyMappedTypeConstraints = $alreadyMappedTypeMetadata[0]->getConstraints();
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: