Skip to content

[PropertyInfo] Deprecate PropertyInfo Type #53160

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
Apr 5, 2024
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
10 changes: 10 additions & 0 deletions UPGRADE-7.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Cache

* Deprecate `CouchbaseBucketAdapter`, use `CouchbaseCollectionAdapter` instead

DoctrineBridge
--------------

* The `DoctrineExtractor::getTypes()` method is deprecated, use `DoctrineExtractor::getType()` instead

ExpressionLanguage
------------------

Expand All @@ -22,6 +27,11 @@ FrameworkBundle

* Mark classes `ConfigBuilderCacheWarmer`, `Router`, `SerializerCacheWarmer`, `TranslationsCacheWarmer`, `Translator` and `ValidatorCacheWarmer` as `final`

PropertyInfo
------------

* The `PropertyTypeExtractorInterface::getTypes()` method is deprecated, use `PropertyTypeExtractorInterface::getType()` instead

SecurityBundle
--------------

Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Deprecate the `DoctrineExtractor::getTypes()` method, use `DoctrineExtractor::getType()` instead

7.0
---

Expand All @@ -17,7 +22,7 @@ CHANGELOG
6.4
---

* [BC BREAK] Add argument `$buildDir` to `ProxyCacheWarmer::warmUp()`
* [BC BREAK] Add argument `$buildDir` to `ProxyCacheWarmer::warmUp()`
* [BC BREAK] Add return type-hints to `EntityFactory`
* Deprecate `DbalLogger`, use a middleware instead
* Deprecate not constructing `DoctrineDataCollector` with an instance of `DebugDataHolder`
Expand Down
194 changes: 165 additions & 29 deletions src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\PropertyInfo\Type as LegacyType;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* Extracts data using Doctrine ORM and ODM metadata.
Expand Down Expand Up @@ -55,8 +57,110 @@ public function getProperties(string $class, array $context = []): ?array
return $properties;
}

public function getType(string $class, string $property, array $context = []): ?Type
{
if (null === $metadata = $this->getMetadata($class)) {
return null;
}

if ($metadata->hasAssociation($property)) {
$class = $metadata->getAssociationTargetClass($property);

if ($metadata->isSingleValuedAssociation($property)) {
if ($metadata instanceof ClassMetadata) {
$associationMapping = $metadata->getAssociationMapping($property);
$nullable = $this->isAssociationNullable($associationMapping);
} else {
$nullable = false;
}

return $nullable ? Type::nullable(Type::object($class)) : Type::object($class);
}

$collectionKeyType = TypeIdentifier::INT;

if ($metadata instanceof ClassMetadata) {
$associationMapping = $metadata->getAssociationMapping($property);

if (self::getMappingValue($associationMapping, 'indexBy')) {
$subMetadata = $this->entityManager->getClassMetadata(self::getMappingValue($associationMapping, 'targetEntity'));

// Check if indexBy value is a property
$fieldName = self::getMappingValue($associationMapping, 'indexBy');
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
$fieldName = $subMetadata->getFieldForColumn(self::getMappingValue($associationMapping, 'indexBy'));
// Not a property, maybe a column name?
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
// Maybe the column name is the association join column?
$associationMapping = $subMetadata->getAssociationMapping($fieldName);

$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
$subMetadata = $this->entityManager->getClassMetadata(self::getMappingValue($associationMapping, 'targetEntity'));

// Not a property, maybe a column name?
if (null === ($typeOfField = $subMetadata->getTypeOfField($indexProperty))) {
$fieldName = $subMetadata->getFieldForColumn($indexProperty);
$typeOfField = $subMetadata->getTypeOfField($fieldName);
}
}
}

if (!$collectionKeyType = $this->getTypeIdentifier($typeOfField)) {
return null;
}
}
}

return Type::collection(Type::object(Collection::class), Type::object($class), Type::builtin($collectionKeyType));
}

if ($metadata instanceof ClassMetadata && isset($metadata->embeddedClasses[$property])) {
return Type::object(self::getMappingValue($metadata->embeddedClasses[$property], 'class'));
}

if (!$metadata->hasField($property)) {
return null;
}

$typeOfField = $metadata->getTypeOfField($property);

if (!$typeIdentifier = $this->getTypeIdentifier($typeOfField)) {
return null;
}

$nullable = $metadata instanceof ClassMetadata && $metadata->isNullable($property);
$enumType = null;

if (null !== $enumClass = self::getMappingValue($metadata->getFieldMapping($property), 'enumType') ?? null) {
$enumType = $nullable ? Type::nullable(Type::enum($enumClass)) : Type::enum($enumClass);
}

$builtinType = $nullable ? Type::nullable(Type::builtin($typeIdentifier)) : Type::builtin($typeIdentifier);

return match ($typeIdentifier) {
TypeIdentifier::OBJECT => match ($typeOfField) {
Types::DATE_MUTABLE, Types::DATETIME_MUTABLE, Types::DATETIMETZ_MUTABLE, 'vardatetime', Types::TIME_MUTABLE => $nullable ? Type::nullable(Type::object(\DateTime::class)) : Type::object(\DateTime::class),
Types::DATE_IMMUTABLE, Types::DATETIME_IMMUTABLE, Types::DATETIMETZ_IMMUTABLE, Types::TIME_IMMUTABLE => $nullable ? Type::nullable(Type::object(\DateTimeImmutable::class)) : Type::object(\DateTimeImmutable::class),
Types::DATEINTERVAL => $nullable ? Type::nullable(Type::object(\DateInterval::class)) : Type::object(\DateInterval::class),
default => $builtinType,
},
TypeIdentifier::ARRAY => match ($typeOfField) {
'array', 'json_array' => $enumType ? null : ($nullable ? Type::nullable(Type::array()) : Type::array()),
Types::SIMPLE_ARRAY => $nullable ? Type::nullable(Type::list($enumType ?? Type::string())) : Type::list($enumType ?? Type::string()),
default => $builtinType,
},
TypeIdentifier::INT, TypeIdentifier::STRING => $enumType ? $enumType : $builtinType,
default => $builtinType,
};
}

/**
* @deprecated since Symfony 7.1, use "getType" instead
*/
public function getTypes(string $class, string $property, array $context = []): ?array
{
trigger_deprecation('symfony/property-info', '7.1', 'The "%s()" method is deprecated, use "%s::getType()" instead.', __METHOD__, self::class);

if (null === $metadata = $this->getMetadata($class)) {
return null;
}
Expand All @@ -73,10 +177,10 @@ public function getTypes(string $class, string $property, array $context = []):
$nullable = false;
}

return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $class)];
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, $class)];
}

$collectionKeyType = Type::BUILTIN_TYPE_INT;
$collectionKeyType = LegacyType::BUILTIN_TYPE_INT;

if ($metadata instanceof ClassMetadata) {
$associationMapping = $metadata->getAssociationMapping($property);
Expand Down Expand Up @@ -104,61 +208,61 @@ public function getTypes(string $class, string $property, array $context = []):
}
}

if (!$collectionKeyType = $this->getPhpType($typeOfField)) {
if (!$collectionKeyType = $this->getTypeIdentifierLegacy($typeOfField)) {
return null;
}
}
}

return [new Type(
Type::BUILTIN_TYPE_OBJECT,
return [new LegacyType(
LegacyType::BUILTIN_TYPE_OBJECT,
false,
Collection::class,
true,
new Type($collectionKeyType),
new Type(Type::BUILTIN_TYPE_OBJECT, false, $class)
new LegacyType($collectionKeyType),
new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, $class)
)];
}

if ($metadata instanceof ClassMetadata && isset($metadata->embeddedClasses[$property])) {
return [new Type(Type::BUILTIN_TYPE_OBJECT, false, self::getMappingValue($metadata->embeddedClasses[$property], 'class'))];
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, self::getMappingValue($metadata->embeddedClasses[$property], 'class'))];
}

if ($metadata->hasField($property)) {
$typeOfField = $metadata->getTypeOfField($property);

if (!$builtinType = $this->getPhpType($typeOfField)) {
if (!$builtinType = $this->getTypeIdentifierLegacy($typeOfField)) {
return null;
}

$nullable = $metadata instanceof ClassMetadata && $metadata->isNullable($property);
$enumType = null;
if (null !== $enumClass = self::getMappingValue($metadata->getFieldMapping($property), 'enumType') ?? null) {
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
$enumType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
}

switch ($builtinType) {
case Type::BUILTIN_TYPE_OBJECT:
case LegacyType::BUILTIN_TYPE_OBJECT:
switch ($typeOfField) {
case Types::DATE_MUTABLE:
case Types::DATETIME_MUTABLE:
case Types::DATETIMETZ_MUTABLE:
case 'vardatetime':
case Types::TIME_MUTABLE:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];

case Types::DATE_IMMUTABLE:
case Types::DATETIME_IMMUTABLE:
case Types::DATETIMETZ_IMMUTABLE:
case Types::TIME_IMMUTABLE:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTimeImmutable')];
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, 'DateTimeImmutable')];

case Types::DATEINTERVAL:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateInterval')];
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, 'DateInterval')];
}

break;
case Type::BUILTIN_TYPE_ARRAY:
case LegacyType::BUILTIN_TYPE_ARRAY:
switch ($typeOfField) {
case 'array': // DBAL < 4
case 'json_array': // DBAL < 3
Expand All @@ -167,21 +271,21 @@ public function getTypes(string $class, string $property, array $context = []):
return null;
}

return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
return [new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, $nullable, null, true)];

case Types::SIMPLE_ARRAY:
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), $enumType ?? new Type(Type::BUILTIN_TYPE_STRING))];
return [new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, $nullable, null, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT), $enumType ?? new LegacyType(LegacyType::BUILTIN_TYPE_STRING))];
}
break;
case Type::BUILTIN_TYPE_INT:
case Type::BUILTIN_TYPE_STRING:
case LegacyType::BUILTIN_TYPE_INT:
case LegacyType::BUILTIN_TYPE_STRING:
if ($enumType) {
return [$enumType];
}
break;
}

return [new Type($builtinType, $nullable)];
return [new LegacyType($builtinType, $nullable)];
}

return null;
Expand Down Expand Up @@ -244,20 +348,52 @@ private function isAssociationNullable(array|AssociationMapping $associationMapp
/**
* Gets the corresponding built-in PHP type.
*/
private function getPhpType(string $doctrineType): ?string
private function getTypeIdentifier(string $doctrineType): ?TypeIdentifier
{
return match ($doctrineType) {
Types::SMALLINT,
Types::INTEGER => TypeIdentifier::INT,
Types::FLOAT => TypeIdentifier::FLOAT,
Types::BIGINT,
Types::STRING,
Types::TEXT,
Types::GUID,
Types::DECIMAL => TypeIdentifier::STRING,
Types::BOOLEAN => TypeIdentifier::BOOL,
Types::BLOB,
Types::BINARY => TypeIdentifier::RESOURCE,
'object', // DBAL < 4
Types::DATE_MUTABLE,
Types::DATETIME_MUTABLE,
Types::DATETIMETZ_MUTABLE,
'vardatetime',
Types::TIME_MUTABLE,
Types::DATE_IMMUTABLE,
Types::DATETIME_IMMUTABLE,
Types::DATETIMETZ_IMMUTABLE,
Types::TIME_IMMUTABLE,
Types::DATEINTERVAL => TypeIdentifier::OBJECT,
'array', // DBAL < 4
'json_array', // DBAL < 3
Types::SIMPLE_ARRAY => TypeIdentifier::ARRAY,
default => null,
};
}

private function getTypeIdentifierLegacy(string $doctrineType): ?string
{
return match ($doctrineType) {
Types::SMALLINT,
Types::INTEGER => Type::BUILTIN_TYPE_INT,
Types::FLOAT => Type::BUILTIN_TYPE_FLOAT,
Types::INTEGER => LegacyType::BUILTIN_TYPE_INT,
Types::FLOAT => LegacyType::BUILTIN_TYPE_FLOAT,
Types::BIGINT,
Types::STRING,
Types::TEXT,
Types::GUID,
Types::DECIMAL => Type::BUILTIN_TYPE_STRING,
Types::BOOLEAN => Type::BUILTIN_TYPE_BOOL,
Types::DECIMAL => LegacyType::BUILTIN_TYPE_STRING,
Types::BOOLEAN => LegacyType::BUILTIN_TYPE_BOOL,
Types::BLOB,
Types::BINARY => Type::BUILTIN_TYPE_RESOURCE,
Types::BINARY => LegacyType::BUILTIN_TYPE_RESOURCE,
'object', // DBAL < 4
Types::DATE_MUTABLE,
Types::DATETIME_MUTABLE,
Expand All @@ -268,10 +404,10 @@ private function getPhpType(string $doctrineType): ?string
Types::DATETIME_IMMUTABLE,
Types::DATETIMETZ_IMMUTABLE,
Types::TIME_IMMUTABLE,
Types::DATEINTERVAL => Type::BUILTIN_TYPE_OBJECT,
Types::DATEINTERVAL => LegacyType::BUILTIN_TYPE_OBJECT,
'array', // DBAL < 4
'json_array', // DBAL < 3
Types::SIMPLE_ARRAY => Type::BUILTIN_TYPE_ARRAY,
Types::SIMPLE_ARRAY => LegacyType::BUILTIN_TYPE_ARRAY,
default => null,
};
}
Expand Down
Loading
Loading