Skip to content

Commit

Permalink
Use ClassMetadata::$propertyAccessors in all places.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Oct 10, 2024
1 parent 5c38cb5 commit 498d399
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 87 deletions.
10 changes: 5 additions & 5 deletions src/Internal/Hydration/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private function initRelatedCollection(
): PersistentCollection {
$oid = spl_object_id($entity);
$relation = $class->associationMappings[$fieldName];
$value = $class->reflFields[$fieldName]->getValue($entity);
$value = $class->propertyAccessors[$fieldName]->getValue($entity);

if ($value === null || is_array($value)) {
$value = new ArrayCollection((array) $value);
Expand All @@ -186,7 +186,7 @@ private function initRelatedCollection(
);
$value->setOwner($entity, $relation);

$class->reflFields[$fieldName]->setValue($entity, $value);
$class->propertyAccessors[$fieldName]->setValue($entity, $value);
$this->uow->setOriginalEntityProperty($oid, $fieldName, $value);

$this->initializedCollections[$oid . $fieldName] = $value;
Expand Down Expand Up @@ -346,7 +346,7 @@ protected function hydrateRowData(array $row, array &$result): void
$parentClass = $this->metadataCache[$this->resultSetMapping()->aliasMap[$parentAlias]];
$relationField = $this->resultSetMapping()->relationMap[$dqlAlias];
$relation = $parentClass->associationMappings[$relationField];
$reflField = $parentClass->reflFields[$relationField];
$reflField = $parentClass->propertyAccessors[$relationField];

// Get a reference to the parent object to which the joined element belongs.
if ($this->resultSetMapping()->isMixed && isset($this->rootAliases[$parentAlias])) {
Expand Down Expand Up @@ -446,13 +446,13 @@ protected function hydrateRowData(array $row, array &$result): void
if ($relation->inversedBy !== null) {
$inverseAssoc = $targetClass->associationMappings[$relation->inversedBy];
if ($inverseAssoc->isToOne()) {
$targetClass->reflFields[$inverseAssoc->fieldName]->setValue($element, $parentObject);
$targetClass->propertyAccessors[$inverseAssoc->fieldName]->setValue($element, $parentObject);
$this->uow->setOriginalEntityProperty(spl_object_id($element), $inverseAssoc->fieldName, $parentObject);
}
}
} else {
// For sure bidirectional, as there is no inverse side in unidirectional mappings
$targetClass->reflFields[$relation->mappedBy]->setValue($element, $parentObject);
$targetClass->propertyAccessors[$relation->mappedBy]->setValue($element, $parentObject);
$this->uow->setOriginalEntityProperty(spl_object_id($element), $relation->mappedBy, $parentObject);
}

Expand Down
22 changes: 11 additions & 11 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,19 +569,19 @@ public function __construct(public string $name, NamingStrategy|null $namingStra
* Gets the ReflectionProperties of the mapped class.
*
* @return ReflectionProperty[]|null[] An array of ReflectionProperty instances.
* @psalm-return array<ReflectionProperty|null>|LegacyReflectionFields<string, ReflectionProperty>
* @psalm-return array<ReflectionProperty|null>

Check failure on line 572 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (3.8.2)

InvalidReturnType

src/Mapping/ClassMetadata.php:572:22: InvalidReturnType: The declared return type 'array<array-key, ReflectionProperty|null>' for Doctrine\ORM\Mapping\ClassMetadata::getPropertyAccessors is incorrect, got 'array<string, Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor>' (see https://psalm.dev/011)

Check failure on line 572 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (default)

InvalidReturnType

src/Mapping/ClassMetadata.php:572:22: InvalidReturnType: The declared return type 'array<array-key, ReflectionProperty|null>' for Doctrine\ORM\Mapping\ClassMetadata::getPropertyAccessors is incorrect, got 'array<string, Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor>' (see https://psalm.dev/011)
*/
public function getReflectionProperties(): array|LegacyReflectionFields
public function getPropertyAccessors(): array
{
return $this->reflFields;
return $this->propertyAccessors;

Check failure on line 576 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (3.8.2)

InvalidReturnStatement

src/Mapping/ClassMetadata.php:576:16: InvalidReturnStatement: The inferred type 'array<string, Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor>' does not match the declared return type 'array<array-key, ReflectionProperty|null>' for Doctrine\ORM\Mapping\ClassMetadata::getPropertyAccessors (see https://psalm.dev/128)

Check failure on line 576 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (default)

InvalidReturnStatement

src/Mapping/ClassMetadata.php:576:16: InvalidReturnStatement: The inferred type 'array<string, Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor>' does not match the declared return type 'array<array-key, ReflectionProperty|null>' for Doctrine\ORM\Mapping\ClassMetadata::getPropertyAccessors (see https://psalm.dev/128)

Check failure on line 576 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (3.8.2, phpstan-dbal3.neon)

Method Doctrine\ORM\Mapping\ClassMetadata::getPropertyAccessors() should return array<ReflectionProperty|null> but returns array<string, Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor>.

Check failure on line 576 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (default, phpstan.neon)

Method Doctrine\ORM\Mapping\ClassMetadata::getPropertyAccessors() should return array<ReflectionProperty|null> but returns array<string, Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor>.
}

/**
* Gets a ReflectionProperty for a specific field of the mapped class.
*/
public function getReflectionProperty(string $name): ReflectionProperty|null
public function getPropertyAccessor(string $name): PropertyAccessor|null
{
return $this->reflFields[$name];
return $this->propertyAccessors[$name];
}

/**
Expand All @@ -595,7 +595,7 @@ public function getSingleIdReflectionProperty(): ReflectionProperty|null
throw new BadMethodCallException('Class ' . $this->name . ' has a composite identifier.');
}

return $this->reflFields[$this->identifier[0]];
return $this->propertyAccessors[$this->identifier[0]];

Check failure on line 598 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (3.8.2)

InvalidReturnStatement

src/Mapping/ClassMetadata.php:598:16: InvalidReturnStatement: The inferred type 'Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor' does not match the declared return type 'ReflectionProperty|null' for Doctrine\ORM\Mapping\ClassMetadata::getSingleIdReflectionProperty (see https://psalm.dev/128)

Check failure on line 598 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (default)

InvalidReturnStatement

src/Mapping/ClassMetadata.php:598:16: InvalidReturnStatement: The inferred type 'Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor' does not match the declared return type 'ReflectionProperty|null' for Doctrine\ORM\Mapping\ClassMetadata::getSingleIdReflectionProperty (see https://psalm.dev/128)

Check failure on line 598 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (3.8.2, phpstan-dbal3.neon)

Method Doctrine\ORM\Mapping\ClassMetadata::getSingleIdReflectionProperty() should return ReflectionProperty|null but returns Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor.

Check failure on line 598 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (default, phpstan.neon)

Method Doctrine\ORM\Mapping\ClassMetadata::getSingleIdReflectionProperty() should return ReflectionProperty|null but returns Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor.
}

/**
Expand All @@ -612,7 +612,7 @@ public function getIdentifierValues(object $entity): array
$id = [];

foreach ($this->identifier as $idField) {
$value = $this->reflFields[$idField]->getValue($entity);
$value = $this->propertyAccessors[$idField]->getValue($entity);

if ($value !== null) {
$id[$idField] = $value;
Expand All @@ -623,7 +623,7 @@ public function getIdentifierValues(object $entity): array
}

$id = $this->identifier[0];
$value = $this->reflFields[$id]->getValue($entity);
$value = $this->propertyAccessors[$id]->getValue($entity);

if ($value === null) {
return [];
Expand All @@ -642,7 +642,7 @@ public function getIdentifierValues(object $entity): array
public function setIdentifierValues(object $entity, array $id): void
{
foreach ($id as $idField => $idValue) {
$this->reflFields[$idField]->setValue($entity, $idValue);
$this->propertyAccessors[$idField]->setValue($entity, $idValue);
}
}

Expand All @@ -651,15 +651,15 @@ public function setIdentifierValues(object $entity, array $id): void
*/
public function setFieldValue(object $entity, string $field, mixed $value): void
{
$this->reflFields[$field]->setValue($entity, $value);
$this->propertyAccessors[$field]->setValue($entity, $value);
}

/**
* Gets the specified field's value off the given entity.
*/
public function getFieldValue(object $entity, string $field): mixed
{
return $this->reflFields[$field]->getValue($entity);
return $this->propertyAccessors[$field]->getValue($entity);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $pare
$subClass->addInheritedFieldMapping($subClassMapping);
}

foreach ($parentClass->reflFields as $name => $field) {
$subClass->reflFields[$name] = $field;
foreach ($parentClass->propertyAccessors as $name => $field) {
$subClass->propertyAccessors[$name] = $field;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/PropertyAccessors/EnumPropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Doctrine\ORM\Mapping\PropertyAccessors;

use ReflectionProperty;
use BackedEnum;

class EnumPropertyAccessor implements PropertyAccessor
{
Expand Down
4 changes: 2 additions & 2 deletions src/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function hydrateAdd(mixed $element): void
if ($this->backRefFieldName && $this->getMapping()->isOneToMany()) {
assert($this->typeClass !== null);
// Set back reference to owner
$this->typeClass->reflFields[$this->backRefFieldName]->setValue(
$this->typeClass->propertyAccessors[$this->backRefFieldName]->setValue(
$element,
$this->owner,
);
Expand All @@ -166,7 +166,7 @@ public function hydrateSet(mixed $key, mixed $element): void
if ($this->backRefFieldName && $this->getMapping()->isOneToMany()) {
assert($this->typeClass !== null);
// Set back reference to owner
$this->typeClass->reflFields[$this->backRefFieldName]->setValue(
$this->typeClass->propertyAccessors[$this->backRefFieldName]->setValue(
$element,
$this->owner,
);
Expand Down
16 changes: 8 additions & 8 deletions src/Persisters/Entity/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ final protected function updateTable(

$where[] = $versionColumn;
$types[] = $this->class->fieldMappings[$versionField]->type;
$params[] = $this->class->reflFields[$versionField]->getValue($entity);
$params[] = $this->class->propertyAccessors[$versionField]->getValue($entity);

switch ($versionFieldType) {
case Types::SMALLINT:
Expand Down Expand Up @@ -779,7 +779,7 @@ public function loadOneToOneEntity(AssociationMapping $assoc, object $sourceEnti

// Complete bidirectional association, if necessary
if ($targetEntity !== null && $isInverseSingleValued) {
$targetClass->reflFields[$assoc->inversedBy]->setValue($targetEntity, $sourceEntity);
$targetClass->propertyAccessors[$assoc->inversedBy]->setValue($targetEntity, $sourceEntity);
}

return $targetEntity;
Expand Down Expand Up @@ -826,7 +826,7 @@ public function loadOneToOneEntity(AssociationMapping $assoc, object $sourceEnti
}
} else {
$computedIdentifier[$targetClass->getFieldForColumn($targetKeyColumn)] =
$sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
$sourceClass->propertyAccessors[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
}
}

Expand Down Expand Up @@ -1043,7 +1043,7 @@ private function getManyToManyStatement(
switch (true) {
case $sourceClass->containsForeignIdentifier:
$field = $sourceClass->getFieldForColumn($sourceKeyColumn);
$value = $sourceClass->reflFields[$field]->getValue($sourceEntity);
$value = $sourceClass->propertyAccessors[$field]->getValue($sourceEntity);

if (isset($sourceClass->associationMappings[$field])) {
$value = $this->em->getUnitOfWork()->getEntityIdentifier($value);
Expand All @@ -1054,7 +1054,7 @@ private function getManyToManyStatement(

case isset($sourceClass->fieldNames[$sourceKeyColumn]):
$field = $sourceClass->fieldNames[$sourceKeyColumn];
$value = $sourceClass->reflFields[$field]->getValue($sourceEntity);
$value = $sourceClass->propertyAccessors[$field]->getValue($sourceEntity);

break;

Expand Down Expand Up @@ -1451,7 +1451,7 @@ protected function getInsertColumnList(): array
{
$columns = [];

foreach ($this->class->reflFields as $name => $field) {
foreach ($this->class->propertyAccessors as $name => $field) {
if ($this->class->isVersioned && $this->class->versionField === $name) {
continue;
}
Expand Down Expand Up @@ -1797,7 +1797,7 @@ private function getOneToManyStatement(
foreach ($owningAssoc->targetToSourceKeyColumns as $sourceKeyColumn => $targetKeyColumn) {
if ($sourceClass->containsForeignIdentifier) {
$field = $sourceClass->getFieldForColumn($sourceKeyColumn);
$value = $sourceClass->reflFields[$field]->getValue($sourceEntity);
$value = $sourceClass->propertyAccessors[$field]->getValue($sourceEntity);

if (isset($sourceClass->associationMappings[$field])) {
$value = $this->em->getUnitOfWork()->getEntityIdentifier($value);
Expand All @@ -1815,7 +1815,7 @@ private function getOneToManyStatement(
}

$field = $sourceClass->fieldNames[$sourceKeyColumn];
$value = $sourceClass->reflFields[$field]->getValue($sourceEntity);
$value = $sourceClass->propertyAccessors[$field]->getValue($sourceEntity);

$criteria[$tableAlias . '.' . $targetKeyColumn] = $value;
$parameters[] = [
Expand Down
2 changes: 1 addition & 1 deletion src/Persisters/Entity/JoinedSubclassPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ protected function getInsertColumnList(): array
? $this->class->getIdentifierColumnNames()
: [];

foreach ($this->class->reflFields as $name => $field) {
foreach ($this->class->propertyAccessors as $name => $field) {
if (
isset($this->class->fieldMappings[$name]->inherited)
&& ! isset($this->class->fieldMappings[$name]->id)
Expand Down
6 changes: 3 additions & 3 deletions src/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ private function createLazyInitializer(ClassMetadata $classMetadata, EntityPersi

$class = $entityPersister->getClassMetadata();

foreach ($class->getReflectionProperties() as $property) {
if (! $property || isset($identifier[$property->getName()]) || ! $class->hasField($property->getName()) && ! $class->hasAssociation($property->getName())) {
foreach ($class->getPropertyAccessors() as $name => $property) {
if (! $property || isset($identifier[$name]) || ! $class->hasField($name) && ! $class->hasAssociation($name)) {
continue;
}

Expand Down Expand Up @@ -282,7 +282,7 @@ private function getProxyFactory(string $className): Closure
$identifierFields = [];

foreach ($identifiers as $identifier => $_) {

Check failure on line 284 in src/Proxy/ProxyFactory.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.3)

Variable "_" is not in valid camel caps format
$identifierFields[$identifier] = $class->getReflectionProperty($identifier);
$identifierFields[$identifier] = $class->getPropertyAccessor($identifier);
}

$proxyFactory = Closure::bind(static function (array $identifier) use ($initializer, $skippedProperties, $identifierFields, $className): InternalProxy {
Expand Down
Loading

0 comments on commit 498d399

Please sign in to comment.