Skip to content

Commit

Permalink
Deprecate array access
Browse files Browse the repository at this point in the history
We now have proper value objects with properties for everything we need.
  • Loading branch information
greg0ire committed Feb 3, 2024
1 parent 3bc2cb6 commit f5dea25
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 25 deletions.
12 changes: 12 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Upgrade to 3.1

## Deprecate array access

Use array access on instances of the following classes is deprecated:

- `Doctrine\ORM\Mapping\DiscriminatorColumnMapping`
- `Doctrine\ORM\Mapping\EmbedClassMapping`
- `Doctrine\ORM\Mapping\FieldMapping`
- `Doctrine\ORM\Mapping\JoinColumnMapping`
- `Doctrine\ORM\Mapping\JoinTableMapping`

# Upgrade to 3.0

## BC BREAK: `Doctrine\ORM\Proxy\Autoloader` no longer extends `Doctrine\Common\Proxy\Autoloader`
Expand Down
29 changes: 29 additions & 0 deletions src/Mapping/ArrayAccessImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM\Mapping;

use Doctrine\Deprecations\Deprecation;
use InvalidArgumentException;

use function property_exists;
Expand All @@ -14,12 +15,26 @@ trait ArrayAccessImplementation
/** @param string $offset */
public function offsetExists(mixed $offset): bool
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/11211',
'Using ArrayAccess on %s is deprecated and will not be possible in Doctrine ORM 4.0. Use the corresponding property instead.',
static::class,
);

return isset($this->$offset);
}

/** @param string $offset */
public function offsetGet(mixed $offset): mixed
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/11211',
'Using ArrayAccess on %s is deprecated and will not be possible in Doctrine ORM 4.0. Use the corresponding property instead.',
static::class,
);

if (! property_exists($this, $offset)) {
throw new InvalidArgumentException('Undefined property: ' . $offset);
}
Expand All @@ -30,12 +45,26 @@ public function offsetGet(mixed $offset): mixed
/** @param string $offset */
public function offsetSet(mixed $offset, mixed $value): void
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/11211',
'Using ArrayAccess on %s is deprecated and will not be possible in Doctrine ORM 4.0. Use the corresponding property instead.',
static::class,
);

$this->$offset = $value;
}

/** @param string $offset */
public function offsetUnset(mixed $offset): void
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/11211',
'Using ArrayAccess on %s is deprecated and will not be possible in Doctrine ORM 4.0. Use the corresponding property instead.',
static::class,
);

$this->$offset = null;
}
}
8 changes: 4 additions & 4 deletions src/Persisters/Entity/JoinedSubclassPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ protected function getInsertColumnList(): array
|| isset($this->class->associationMappings[$name]->inherited)
|| ($this->class->isVersioned && $this->class->versionField === $name)
|| isset($this->class->embeddedClasses[$name])
|| isset($this->class->fieldMappings[$name]['notInsertable'])
|| isset($this->class->fieldMappings[$name]->notInsertable)
) {
continue;
}
Expand Down Expand Up @@ -519,9 +519,9 @@ protected function fetchVersionAndNotUpsertableValues(ClassMetadata $versionedCl
$class = null;
if ($this->class->isVersioned && $key === $versionedClass->versionField) {
$class = $versionedClass;
} elseif (isset($column['generated'])) {
$class = isset($column['inherited'])
? $this->em->getClassMetadata($column['inherited'])
} elseif (isset($column->generated)) {
$class = isset($column->inherited)
? $this->em->getClassMetadata($column->inherited)
: $this->class;
} else {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Query/ResultSetMappingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected function addAllClassFields(string $class, string $alias, array $column

$this->addFieldResult($alias, $columnAlias, $propertyName);

$enumType = $classMetadata->getFieldMapping($propertyName)['enumType'] ?? null;
$enumType = $classMetadata->getFieldMapping($propertyName)->enumType ?? null;
if (! empty($enumType)) {
$this->addEnumResult($columnAlias, $enumType);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Tools/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ function (FieldMapping $fieldMapping) use ($class): string|null {
return null;
}

$metadataFieldType = $this->findBuiltInType(Type::getType($fieldMapping['type']));
$metadataFieldType = $this->findBuiltInType(Type::getType($fieldMapping->type));

//If the metadata field type is not a mapped built-in type, we cannot check it
if ($metadataFieldType === null) {
Expand Down Expand Up @@ -371,7 +371,7 @@ function (FieldMapping $fieldMapping) use ($class): string|null {
);
}

if (! isset($fieldMapping['enumType']) || $propertyType === $fieldMapping['enumType']) {
if (! isset($fieldMapping->enumType) || $propertyType === $fieldMapping->enumType) {
return null;
}

Expand All @@ -380,17 +380,17 @@ function (FieldMapping $fieldMapping) use ($class): string|null {
$class->name,
$fieldName,
$propertyType,
$fieldMapping['enumType'],
$fieldMapping->enumType,
);
}

if (
isset($fieldMapping['enumType'])
&& $propertyType !== $fieldMapping['enumType']
isset($fieldMapping->enumType)
&& $propertyType !== $fieldMapping->enumType
&& interface_exists($propertyType)
&& is_a($fieldMapping['enumType'], $propertyType, true)
&& is_a($fieldMapping->enumType, $propertyType, true)
) {
$backingType = (string) (new ReflectionEnum($fieldMapping['enumType']))->getBackingType();
$backingType = (string) (new ReflectionEnum($fieldMapping->enumType))->getBackingType();

if ($metadataFieldType === $backingType) {
return null;
Expand All @@ -400,14 +400,14 @@ function (FieldMapping $fieldMapping) use ($class): string|null {
"The field '%s#%s' has the metadata enumType '%s' with a backing type of '%s' that differs from the metadata field type '%s'.",
$class->name,
$fieldName,
$fieldMapping['enumType'],
$fieldMapping->enumType,
$backingType,
$metadataFieldType,
);
}

if (
$fieldMapping['type'] === 'json'
$fieldMapping->type === 'json'
&& in_array($propertyType, ['string', 'int', 'float', 'bool', 'true', 'false', 'null'], true)
) {
return null;
Expand All @@ -419,7 +419,7 @@ function (FieldMapping $fieldMapping) use ($class): string|null {
$fieldName,
$propertyType,
$metadataFieldType,
$fieldMapping['type'],
$fieldMapping->type,
);
},
$class->fieldMappings,
Expand Down
8 changes: 4 additions & 4 deletions src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ public function recomputeSingleEntityChangeSet(ClassMetadata $class, object $ent
foreach ($actualData as $propName => $actualValue) {
$orgValue = $originalData[$propName] ?? null;

if (isset($class->fieldMappings[$propName]['enumType'])) {
if (isset($class->fieldMappings[$propName]->enumType)) {
if (is_array($orgValue)) {
foreach ($orgValue as $id => $val) {
if ($val instanceof BackedEnum) {
Expand Down Expand Up @@ -1267,16 +1267,16 @@ private function computeDeleteExecutionOrder(): array
}

$joinColumns = reset($assoc->joinColumns);
if (! isset($joinColumns['onDelete'])) {
if (! isset($joinColumns->onDelete)) {
continue;
}

$onDeleteOption = strtolower($joinColumns['onDelete']);
$onDeleteOption = strtolower($joinColumns->onDelete);
if ($onDeleteOption !== 'cascade') {
continue;
}

$targetEntity = $class->getFieldValue($entity, $assoc['fieldName']);
$targetEntity = $class->getFieldValue($entity, $assoc->fieldName);

// If the association does not refer to another entity or that entity
// is not to be deleted, there is no ordering problem and we can
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests/ORM/Functional/Ticket/GH11135Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function testOverrideInheritsDeclaringClass(): void
$cm1 = $this->_em->getClassMetadata(GH11135EntityWithOverride::class);
$cm2 = $this->_em->getClassMetadata(GH11135EntityWithoutOverride::class);

self::assertSame($cm1->getFieldMapping('id')['declared'], $cm2->getFieldMapping('id')['declared']);
self::assertSame($cm1->getAssociationMapping('ref')['declared'], $cm2->getAssociationMapping('ref')['declared']);
self::assertSame($cm1->getFieldMapping('id')->declared, $cm2->getFieldMapping('id')->declared);
self::assertSame($cm1->getAssociationMapping('ref')->declared, $cm2->getAssociationMapping('ref')->declared);
}
}

Expand Down
3 changes: 1 addition & 2 deletions tests/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,7 @@ public function testAttributeOverrideKeepsDeclaringClass(): void

$mapping = $cm->getFieldMapping('id');

self::assertArrayHasKey('declared', $mapping);
self::assertSame(AbstractContentItem::class, $mapping['declared']);
self::assertSame(AbstractContentItem::class, $mapping->declared);
}

public function testAssociationOverrideKeepsDeclaringClass(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests/ORM/Mapping/XmlMappingDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ public function testClassNameInFieldOrId(): void
/** @var array{type: string} $name */
$name = $class->getFieldMapping('name');

self::assertEquals(ProjectId::class, $id['type']);
self::assertEquals(ProjectName::class, $name['type']);
self::assertEquals(ProjectId::class, $id->type);
self::assertEquals(ProjectName::class, $name->type);
}

public function testDisablingXmlValidationIsPossible(): void
Expand Down

0 comments on commit f5dea25

Please sign in to comment.