8000 [PropertyInfo] Deprecate PropertyInfo Type · symfony/symfony@e6740da · GitHub
[go: up one dir, main page]

Skip to content

Commit e6740da

Browse files
mtarldKorbeil
andcommitted
[PropertyInfo] Deprecate PropertyInfo Type
Co-authored-by: Baptiste Leduc <baptiste.leduc@gmail.com>
1 parent eaadb98 commit e6740da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2876
-1350
lines changed

src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

+51-34
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
2222
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
2323
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
24-
use Symfony\Component\PropertyInfo\Type;
24+
use Symfony\Component\PropertyInfo\Util\BackwardCompatibilityHelper;
25+
use Symfony\Component\TypeInfo\Type;
26+
use Symfony\Component\TypeInfo\TypeIdentifier;
2527

2628
/**
2729
* Extracts data using Doctrine ORM and ODM metadata.
@@ -52,7 +54,7 @@ public function getProperties(string $class, array $context = []): ?array
5254
return $properties;
5355
}
5456

55-
public function getTypes(string $class, string $property, array $context = []): ?array
57+
public function getType(string $class, string $property, array $context = []): ?Type
5658
{
5759
if (null === $metadata = $this->getMetadata($class)) {
5860
return null;
@@ -64,16 +66,17 @@ public function getTypes(string $class, string $property, array $context = []):
6466
if ($metadata->isSingleValuedAssociation($property)) {
6567
if ($metadata instanceof ClassMetadata) {
6668
$associationMapping = $metadata->getAssociationMapping($property);
67-
6869
$nullable = $this->isAssociationNullable($associationMapping);
6970
} else {
7071
$nullable = false;
7172
}
7273

73-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $class)];
74+
$t = Type::object($class);
75+
76+
return $nullable ? Type::nullable($t) : $t;
7477
}
7578

76-
$collectionKeyType = Type::BUILTIN_TYPE_INT;
79+
$collectionKeyType = TypeIdentifier::INT;
7780

7881
if ($metadata instanceof ClassMetadata) {
7982
$associationMapping = $metadata->getAssociationMapping($property);
@@ -107,18 +110,11 @@ public function getTypes(string $class, string $property, array $context = []):
107110
}
108111
}
109112

110-
return [new Type(
111-
Type::BUILTIN_TYPE_OBJECT,
112-
false,
113-
Collection::class,
114-
true,
115-
new Type($collectionKeyType),
116-
new Type(Type::BUILTIN_TYPE_OBJECT, false, $class)
117-
)];
113+
return Type::collection(Type::object(Collection::class), Type::object($class), Type::builtin($collectionKeyType));
118114
}
119115

120116
if ($metadata instanceof ClassMetadata && isset($metadata->embeddedClasses[$property])) {
121-
return [new Type(Type::BUILTIN_TYPE_OBJECT, false, $metadata->embeddedClasses[$property]['class'])];
117+
return Type::object($metadata->embeddedClasses[$property]['class']);
122118
}
123119

124120
if ($metadata->hasField($property)) {
@@ -130,32 +126,40 @@ public function getTypes(string $class, string $property, array $context = []):
130126

131127
$nullable = $metadata instanceof ClassMetadata && $metadata->isNullable($property);
132128
$enumType = null;
129+
133130
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
134-
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
131+
$enumType = Type::enum($enumClass);
132+
$enumType = $nullable ? Type::nullable($enumType) : $enumType;
135133
}
136134

137135
switch ($builtinType) {
138-
case Type::BUILTIN_TYPE_OBJECT:
136+
case TypeIdentifier::OBJECT:
139137
switch ($typeOfField) {
140138
case Types::DATE_MUTABLE:
141139
case Types::DATETIME_MUTABLE:
142140
case Types::DATETIMETZ_MUTABLE:
143141
case 'vardatetime':
144142
case Types::TIME_MUTABLE:
145-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
143+
$t = Type::object(\DateTime::class);
144+
145+
return $nullable ? Type::nullable($t) : $t;
146146

147147
case Types::DATE_IMMUTABLE:
148148
case Types::DATETIME_IMMUTABLE:
149149
case Types::DATETIMETZ_IMMUTABLE:
150150
case Types::TIME_IMMUTABLE:
151-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTimeImmutable')];
151+
$t = Type::object(\DateTimeImmutable::class);
152+
153+
return $nullable ? Type::nullable($t) : $t;
152154

153155
case Types::DATEINTERVAL:
154-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateInterval')];
156+
$t = Type::object(\DateInterval::class);
157+
158+
return $nullable ? Type::nullable($t) : $t;
155159
}
156160

157161
break;
158-
case Type::BUILTIN_TYPE_ARRAY:
162+
case TypeIdentifier::ARRAY:
159163
switch ($typeOfField) {
160164
case 'array': // DBAL < 4
161165
case 'json_array': // DBAL < 3
@@ -164,26 +168,39 @@ public function getTypes(string $class, string $property, array $context = []):
164168
return null;
165169
}
166170

167-
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
171+
$t = Type::array();
172+
173+
return $nullable ? Type::nullable($t) : $t;
168174

169175
case Types::SIMPLE_ARRAY:
170-
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), $enumType ?? new Type(Type::BUILTIN_TYPE_STRING))];
176+
$t = Type::list($enumType ?? Type::string());
177+
178+
return $nullable ? Type::nullable($t) : $t;
171179
}
172180
break;
173-
case Type::BUILTIN_TYPE_INT:
174-
case Type::BUILTIN_TYPE_STRING:
181+
case TypeIdentifier::INT:
182+
case TypeIdentifier::STRING:
175183
if ($enumType) {
176-
return [$enumType];
184+
return $enumType;
177185
}
178186
break;
179187
}
180188

181-
return [new Type($builtinType, $nullable)];
189+
$t = Type::builtin($builtinType);
190+
191+
return $nullable ? Type::nullable($t) : $t;
182192
}
183193

184194
return null;
185195
}
186196

197+
public function getTypes(string $class, string $property, array $context = []): ?array
198+
{
199+
trigger_deprecation('symfony/doctrine-bridge', '7.1', 'The "%s()" method is deprecated, use "%s::getType()" instead.', __METHOD__, self::class);
200+
201+
return BackwardCompatibilityHelper::convertTypeToLegacyTypes($this->getType($class, $property, $context, true));
202+
}
203+
187204
public function isReadable(string $class, string $property, array $context = []): ?bool
188205
{
189206
return null;
@@ -241,20 +258,20 @@ private function isAssociationNullable(array|AssociationMapping $associationMapp
241258
/**
242259
* Gets the corresponding built-in PHP type.
243260
*/
244-
private function getPhpType(string $doctrineType): ?string
261+
private function getPhpType(string $doctrineType): ?TypeIdentifier
245262
{
246263
return match ($doctrineType) {
247264
Types::SMALLINT,
248-
Types::INTEGER => Type::BUILTIN_TYPE_INT,
249-
Types::FLOAT => Type::BUILTIN_TYPE_FLOAT,
265+
Types::INTEGER => TypeIdentifier::INT,
266+
Types::FLOAT => TypeIdentifier::FLOAT,
250267
Types::BIGINT,
251268
Types::STRING,
252269
Types::TEXT,
253270
Types::GUID,
254-
Types::DECIMAL => Type::BUILTIN_TYPE_STRING,
255-
Types::BOOLEAN => Type::BUILTIN_TYPE_BOOL,
271+
Types::DECIMAL => TypeIdentifier::STRING,
272+
Types::BOOLEAN => TypeIdentifier::BOOL,
256273
Types::BLOB,
257-
Types::BINARY => Type::BUILTIN_TYPE_RESOURCE,
274+
Types::BINARY => TypeIdentifier::RESOURCE,
258275
'object', // DBAL < 4
259276
Types::DATE_MUTABLE,
260277
Types::DATETIME_MUTABLE,
@@ -265,10 +282,10 @@ private function getPhpType(string $doctrineType): ?string
265282
Types::DATETIME_IMMUTABLE,
266283
Types::DATETIMETZ_IMMUTABLE,
267284
Types::TIME_IMMUTABLE,
268-
Types::DATEINTERVAL => Type::BUILTIN_TYPE_OBJECT,
285+
Types::DATEINTERVAL => TypeIdentifier::OBJECT,
269286
'array', // DBAL < 4
270287
'json_array', // DBAL < 3
271-
Types::SIMPLE_ARRAY => Type::BUILTIN_TYPE_ARRAY,
288+
Types::SIMPLE_ARRAY => TypeIdentifier::ARRAY,
272289
default => null,
273290
};
274291
}

0 commit comments

Comments
 (0)
0