Skip to content

Commit

Permalink
Merge pull request #10617 from greg0ire/field-mapping-object-api
Browse files Browse the repository at this point in the history
Use Rector to migrate to the object API of FieldMapping
  • Loading branch information
greg0ire committed Apr 14, 2023
2 parents eaac5cd + 2acb298 commit c49bba7
Show file tree
Hide file tree
Showing 28 changed files with 250 additions and 265 deletions.
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, ob
}

foreach ($metadata->fieldMappings as $name => $fieldMapping) {
if (isset($fieldMapping['generated'])) {
if (isset($fieldMapping->generated)) {
$data[$name] = $metadata->getFieldValue($entity, $name);
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, ob
if (isset($targetClassMetadata->fieldMappings[$fieldName])) {
$fieldMapping = $targetClassMetadata->fieldMappings[$fieldName];

$data[$owningAssociation['targetToSourceKeyColumns'][$fieldMapping['columnName']]] = $fieldValue;
$data[$owningAssociation['targetToSourceKeyColumns'][$fieldMapping->columnName]] = $fieldValue;

continue;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ protected function hydrateColumnInfo(string $key): array|null
$columnInfo = [
'isIdentifier' => in_array($fieldName, $classMetadata->identifier, true),
'fieldName' => $fieldName,
'type' => Type::getType($fieldMapping['type']),
'type' => Type::getType($fieldMapping->type),
'dqlAlias' => $ownerMap,
'enumType' => $this->rsm->enumMappings[$key] ?? null,
];
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/AnsiQuoteStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getColumnName(
ClassMetadata $class,
AbstractPlatform $platform,
): string {
return $class->fieldMappings[$fieldName]['columnName'];
return $class->fieldMappings[$fieldName]->columnName;
}

public function getTableName(ClassMetadata $class, AbstractPlatform $platform): string
Expand Down
104 changes: 53 additions & 51 deletions lib/Doctrine/ORM/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,33 +822,35 @@ public function wakeupReflection(ReflectionService $reflService): void
}

foreach ($this->fieldMappings as $field => $mapping) {
if (isset($mapping['declaredField']) && isset($parentReflFields[$mapping['declaredField']])) {
$childProperty = $this->getAccessibleProperty($reflService, $mapping['originalClass'], $mapping['originalField']);
if (isset($mapping->declaredField) && isset($parentReflFields[$mapping->declaredField])) {
assert($mapping->originalField !== null);
assert($mapping->originalClass !== null);
$childProperty = $this->getAccessibleProperty($reflService, $mapping->originalClass, $mapping->originalField);
assert($childProperty !== null);

if (isset($mapping['enumType'])) {
if (isset($mapping->enumType)) {
$childProperty = new ReflectionEnumProperty(
$childProperty,
$mapping['enumType'],
$mapping->enumType,
);
}

$this->reflFields[$field] = new ReflectionEmbeddedProperty(
$parentReflFields[$mapping['declaredField']],
$parentReflFields[$mapping->declaredField],
$childProperty,
$mapping['originalClass'],
$mapping->originalClass,
);
continue;
}

$this->reflFields[$field] = isset($mapping['declared'])
? $this->getAccessibleProperty($reflService, $mapping['declared'], $field)
$this->reflFields[$field] = isset($mapping->declared)
? $this->getAccessibleProperty($reflService, $mapping->declared, $field)
: $this->getAccessibleProperty($reflService, $this->name, $field);

if (isset($mapping['enumType']) && $this->reflFields[$field] !== null) {
if (isset($mapping->enumType) && $this->reflFields[$field] !== null) {
$this->reflFields[$field] = new ReflectionEnumProperty(
$this->reflFields[$field],
$mapping['enumType'],
$mapping->enumType,
);
}
}
Expand Down Expand Up @@ -1034,14 +1036,14 @@ public function isUniqueField(string $fieldName): bool
{
$mapping = $this->getFieldMapping($fieldName);

return $mapping !== false && isset($mapping['unique']) && $mapping['unique'];
return $mapping !== false && isset($mapping->unique) && $mapping->unique;
}

public function isNullable(string $fieldName): bool
{
$mapping = $this->getFieldMapping($fieldName);

return $mapping !== false && isset($mapping['nullable']) && $mapping['nullable'];
return $mapping !== false && isset($mapping->nullable) && $mapping->nullable;
}

/**
Expand Down Expand Up @@ -1195,27 +1197,27 @@ protected function validateAndCompleteFieldMapping(array $mapping): FieldMapping

$mapping = FieldMapping::fromMappingArray($mapping);

if ($mapping['columnName'][0] === '`') {
$mapping['columnName'] = trim($mapping['columnName'], '`');
$mapping['quoted'] = true;
if ($mapping->columnName[0] === '`') {
$mapping->columnName = trim($mapping->columnName, '`');
$mapping->quoted = true;
}

$this->columnNames[$mapping['fieldName']] = $mapping['columnName'];
$this->columnNames[$mapping->fieldName] = $mapping->columnName;

if (isset($this->fieldNames[$mapping['columnName']]) || ($this->discriminatorColumn && $this->discriminatorColumn['name'] === $mapping['columnName'])) {
throw MappingException::duplicateColumnName($this->name, $mapping['columnName']);
if (isset($this->fieldNames[$mapping->columnName]) || ($this->discriminatorColumn && $this->discriminatorColumn['name'] === $mapping->columnName)) {
throw MappingException::duplicateColumnName($this->name, $mapping->columnName);
}

$this->fieldNames[$mapping['columnName']] = $mapping['fieldName'];
$this->fieldNames[$mapping->columnName] = $mapping->fieldName;

// Complete id mapping
if (isset($mapping['id']) && $mapping['id'] === true) {
if ($this->versionField === $mapping['fieldName']) {
throw MappingException::cannotVersionIdField($this->name, $mapping['fieldName']);
if (isset($mapping->id) && $mapping->id === true) {
if ($this->versionField === $mapping->fieldName) {
throw MappingException::cannotVersionIdField($this->name, $mapping->fieldName);
}

if (! in_array($mapping['fieldName'], $this->identifier, true)) {
$this->identifier[] = $mapping['fieldName'];
if (! in_array($mapping->fieldName, $this->identifier, true)) {
$this->identifier[] = $mapping->fieldName;
}

// Check for composite key
Expand All @@ -1224,22 +1226,22 @@ protected function validateAndCompleteFieldMapping(array $mapping): FieldMapping
}
}

if (isset($mapping['generated'])) {
if (! in_array($mapping['generated'], [self::GENERATED_NEVER, self::GENERATED_INSERT, self::GENERATED_ALWAYS])) {
throw MappingException::invalidGeneratedMode($mapping['generated']);
if (isset($mapping->generated)) {
if (! in_array($mapping->generated, [self::GENERATED_NEVER, self::GENERATED_INSERT, self::GENERATED_ALWAYS])) {
throw MappingException::invalidGeneratedMode($mapping->generated);
}

if ($mapping['generated'] === self::GENERATED_NEVER) {
unset($mapping['generated']);
if ($mapping->generated === self::GENERATED_NEVER) {
unset($mapping->generated);
}
}

if (isset($mapping['enumType'])) {
if (! enum_exists($mapping['enumType'])) {
throw MappingException::nonEnumTypeMapped($this->name, $mapping['fieldName'], $mapping['enumType']);
if (isset($mapping->enumType)) {
if (! enum_exists($mapping->enumType)) {
throw MappingException::nonEnumTypeMapped($this->name, $mapping->fieldName, $mapping->enumType);
}

if (! empty($mapping['id'])) {
if (! empty($mapping->id)) {
$this->containsEnumIdentifier = true;
}
}
Expand Down Expand Up @@ -1512,7 +1514,7 @@ public function getIdentifierColumnNames(): array

foreach ($this->identifier as $idProperty) {
if (isset($this->fieldMappings[$idProperty])) {
$columnNames[] = $this->fieldMappings[$idProperty]['columnName'];
$columnNames[] = $this->fieldMappings[$idProperty]->columnName;

continue;
}
Expand Down Expand Up @@ -1607,7 +1609,7 @@ public function isIdentifierNatural(): bool
public function getTypeOfField(string $fieldName): string|null
{
return isset($this->fieldMappings[$fieldName])
? $this->fieldMappings[$fieldName]['type']
? $this->fieldMappings[$fieldName]->type
: null;
}

Expand Down Expand Up @@ -1751,29 +1753,29 @@ public function setAttributeOverride(string $fieldName, array $overrideMapping):

$mapping = $this->fieldMappings[$fieldName];

if (isset($mapping['inherited'])) {
throw MappingException::illegalOverrideOfInheritedProperty($this->name, $fieldName, $mapping['inherited']);
if (isset($mapping->inherited)) {
throw MappingException::illegalOverrideOfInheritedProperty($this->name, $fieldName, $mapping->inherited);
}

if (isset($mapping['id'])) {
$overrideMapping['id'] = $mapping['id'];
if (isset($mapping->id)) {
$overrideMapping['id'] = $mapping->id;
}

if (! isset($overrideMapping['type'])) {
$overrideMapping['type'] = $mapping['type'];
$overrideMapping['type'] = $mapping->type;
}

if (! isset($overrideMapping['fieldName'])) {
$overrideMapping['fieldName'] = $mapping['fieldName'];
$overrideMapping['fieldName'] = $mapping->fieldName;
}

if ($overrideMapping['type'] !== $mapping['type']) {
if ($overrideMapping['type'] !== $mapping->type) {
throw MappingException::invalidOverrideFieldType($this->name, $fieldName);
}

unset($this->fieldMappings[$fieldName]);
unset($this->fieldNames[$mapping['columnName']]);
unset($this->columnNames[$mapping['fieldName']]);
unset($this->fieldNames[$mapping->columnName]);
unset($this->columnNames[$mapping->fieldName]);

$overrideMapping = $this->validateAndCompleteFieldMapping($overrideMapping);

Expand All @@ -1785,7 +1787,7 @@ public function setAttributeOverride(string $fieldName, array $overrideMapping):
*/
public function isInheritedField(string $fieldName): bool
{
return isset($this->fieldMappings[$fieldName]['inherited']);
return isset($this->fieldMappings[$fieldName]->inherited);
}

/**
Expand Down Expand Up @@ -1888,13 +1890,13 @@ private function isInheritanceType(int $type): bool
public function mapField(array $mapping): void
{
$mapping = $this->validateAndCompleteFieldMapping($mapping);
$this->assertFieldNotMapped($mapping['fieldName']);
$this->assertFieldNotMapped($mapping->fieldName);

if (isset($mapping['generated'])) {
if (isset($mapping->generated)) {
$this->requiresFetchAfterChange = true;
}

$this->fieldMappings[$mapping['fieldName']] = $mapping;
$this->fieldMappings[$mapping->fieldName] = $mapping;
}

/**
Expand All @@ -1920,9 +1922,9 @@ public function addInheritedAssociationMapping(AssociationMapping $mapping/*, $o
*/
public function addInheritedFieldMapping(FieldMapping $fieldMapping): void
{
$this->fieldMappings[$fieldMapping['fieldName']] = $fieldMapping;
$this->columnNames[$fieldMapping['fieldName']] = $fieldMapping['columnName'];
$this->fieldNames[$fieldMapping['columnName']] = $fieldMapping['fieldName'];
$this->fieldMappings[$fieldMapping->fieldName] = $fieldMapping;
$this->columnNames[$fieldMapping->fieldName] = $fieldMapping->columnName;
$this->fieldNames[$fieldMapping->columnName] = $fieldMapping->fieldName;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,12 @@ private function addMappingInheritanceInformation(
AssociationMapping|EmbeddedClassMapping|FieldMapping $mapping,
ClassMetadata $parentClass,
): void {
if (! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
$mapping['inherited'] = $parentClass->name;
if (! isset($mapping->inherited) && ! $parentClass->isMappedSuperclass) {
$mapping->inherited = $parentClass->name;
}

if (! isset($mapping['declared'])) {
$mapping['declared'] = $parentClass->name;
if (! isset($mapping->declared)) {
$mapping->declared = $parentClass->name;
}
}

Expand Down Expand Up @@ -519,7 +519,7 @@ private function completeIdGeneratorMapping(ClassMetadata $class): void
$sequenceName = null;
$fieldName = $class->identifier ? $class->getSingleIdentifierFieldName() : null;

$generator = $fieldName && $class->fieldMappings[$fieldName]['type'] === 'bigint'
$generator = $fieldName && $class->fieldMappings[$fieldName]->type === 'bigint'
? new BigIntegerIdentityGenerator()
: new IdentityGenerator();

Expand All @@ -534,7 +534,7 @@ private function completeIdGeneratorMapping(ClassMetadata $class): void
if (! $definition) {
$fieldName = $class->getSingleIdentifierFieldName();
$sequenceName = $class->getSequenceName($this->getTargetPlatform());
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
$quoted = isset($class->fieldMappings[$fieldName]->quoted) || isset($class->table['quoted']);

$definition = [
'sequenceName' => $this->truncateSequenceName($sequenceName),
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class DefaultQuoteStrategy implements QuoteStrategy

public function getColumnName(string $fieldName, ClassMetadata $class, AbstractPlatform $platform): string
{
return isset($class->fieldMappings[$fieldName]['quoted'])
? $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName'])
: $class->fieldMappings[$fieldName]['columnName'];
return isset($class->fieldMappings[$fieldName]->quoted)
? $platform->quoteIdentifier($class->fieldMappings[$fieldName]->columnName)
: $class->fieldMappings[$fieldName]->columnName;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function getSelectColumnSQL(string $field, ClassMetadata $class, strin
{
$tableAlias = $alias === 'r' ? '' : $alias;
$fieldMapping = $class->fieldMappings[$field];
$columnAlias = $this->getSQLColumnAlias($fieldMapping['columnName']);
$columnAlias = $this->getSQLColumnAlias($fieldMapping->columnName);
$sql = sprintf(
'%s.%s',
$this->getSQLTableAlias($class->name, $tableAlias),
Expand All @@ -49,7 +49,7 @@ protected function getSelectColumnSQL(string $field, ClassMetadata $class, strin

$this->currentPersisterContext->rsm->addFieldResult($alias, $columnAlias, $field, $class->name);

$type = Type::getType($fieldMapping['type']);
$type = Type::getType($fieldMapping->type);
$sql = $type->convertToPHPValueSQL($sql, $this->platform);

return $sql . ' AS ' . $columnAlias;
Expand Down
Loading

0 comments on commit c49bba7

Please sign in to comment.