Skip to content

Commit 9cc24fc

Browse files
committed
[TypeInfo] Redesign Type methods and nullability
1 parent b85d46f commit 9cc24fc

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

Mapping/Loader/PropertyInfoLoader.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
1717
use Symfony\Component\PropertyInfo\Type as PropertyInfoType;
1818
use Symfony\Component\TypeInfo\Type as TypeInfoType;
19+
use Symfony\Component\TypeInfo\Type\BuiltinType;
1920
use Symfony\Component\TypeInfo\Type\CollectionType;
20-
use Symfony\Component\TypeInfo\Type\IntersectionType;
21+
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
22+
use Symfony\Component\TypeInfo\Type\NullableType;
2123
use Symfony\Component\TypeInfo\Type\ObjectType;
22-
use Symfony\Component\TypeInfo\Type\UnionType;
2324
use Symfony\Component\TypeInfo\TypeIdentifier;
25+
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
2426
use Symfony\Component\Validator\Constraints\All;
2527
use Symfony\Component\Validator\Constraints\NotBlank;
2628
use Symfony\Component\Validator\Constraints\NotNull;
@@ -139,11 +141,10 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
139141
}
140142

141143
$type = $types;
142-
$nullable = false;
144+
$nullable = $type->isNullable();
143145

144-
if ($type instanceof UnionType && $type->isNullable()) {
145-
$nullable = true;
146-
$type = $type->asNonNullable();
146+
if ($type instanceof NullableType) {
147+
$type = $type->getWrappedType();
147148
}
148149

149150
if ($type instanceof CollectionType) {
@@ -193,18 +194,27 @@ private function getTypeConstraintLegacy(string $builtinType, PropertyInfoType $
193194

194195
private function getTypeConstraint(TypeInfoType $type): ?Type
195196
{
196-
if ($type instanceof UnionType || $type instanceof IntersectionType) {
197-
return ($type->isA(TypeIdentifier::INT) || $type->isA(TypeIdentifier::FLOAT) || $type->isA(TypeIdentifier::STRING) || $type->isA(TypeIdentifier::BOOL)) ? new Type(['type' => 'scalar']) : null;
197+
if ($type instanceof CompositeTypeInterface) {
198+
return $type->isIdentifiedBy(
199+
TypeIdentifier::INT,
200+
TypeIdentifier::FLOAT,
201+
TypeIdentifier::STRING,
202+
TypeIdentifier::BOOL,
203+
TypeIdentifier::TRUE,
204+
TypeIdentifier::FALSE,
205+
) ? new Type(['type' => 'scalar']) : null;
198206
}
199207

200-
$baseType = $type->getBaseType();
208+
while ($type instanceof WrappingTypeInterface) {
209+
$type = $type->getWrappedType();
210+
}
201211

202-
if ($baseType instanceof ObjectType) {
203-
return new Type(['type' => $baseType->getClassName()]);
212+
if ($type instanceof ObjectType) {
213+
return new Type(['type' => $type->getClassName()]);
204214
}
205215

206-
if (TypeIdentifier::MIXED !== $baseType->getTypeIdentifier()) {
207-
return new Type(['type' => $baseType->getTypeIdentifier()->value]);
216+
if ($type instanceof BuiltinType && TypeIdentifier::MIXED !== $type->getTypeIdentifier()) {
217+
return new Type(['type' => $type->getTypeIdentifier()->value]);
208218
}
209219

210220
return null;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"symfony/property-access": "^6.4|^7.0",
4040
"symfony/property-info": "^6.4|^7.0",
4141
"symfony/translation": "^6.4.3|^7.0.3",
42-
"symfony/type-info": "^7.1",
42+
"symfony/type-info": "^7.2",
4343
"egulias/email-validator": "^2.1.10|^3|^4"
4444
},
4545
"conflict": {

0 commit comments

Comments
 (0)