Skip to content

Eliminate Doctrine deprecations #21

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ parameters:
identifier: 'identical.alwaysFalse'
reportUnmatched: false
path: 'src/EntityPreloader.php'
-
message: '#internal interface Doctrine\\ORM\\Mapping\\PropertyAccessors\\PropertyAccessor#' # internal, although returned from public ClassMetadata::getPropertyAccessor
reportUnmatched: false # only new Doctrine
path: 'src/EntityPreloader.php'
-
message: '#Doctrine\\ORM\\Mapping\\PropertyAccessors\\PropertyAccessor#'
reportUnmatched: false # only old Doctrine
identifier: class.notFound
path: 'src/EntityPreloader.php'
-
message: '#Call to function method_exists#'
reportUnmatched: false # only new Doctrine
identifier: function.alreadyNarrowedType
path: 'src/EntityPreloader.php'
-
message: '#Result of \|\| is always false#'
identifier: 'booleanOr.alwaysFalse'
Expand Down
8 changes: 7 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
cacheDirectory="cache/phpunit"
>
<php>
<ini name="error_reporting" value="-1"/>
<env name="DOCTRINE_DEPRECATIONS" value="trigger"/>
</php>

<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source>
<source ignoreSuppressionOfDeprecations="true">
<include>
<directory>src</directory>
</include>
Expand Down
79 changes: 54 additions & 25 deletions src/EntityPreloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayAccess;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\QueryBuilder;
use LogicException;
Expand All @@ -14,6 +15,7 @@
use function count;
use function get_parent_class;
use function is_a;
use function method_exists;

/**
* @template E of object
Expand Down Expand Up @@ -120,18 +122,18 @@ private function loadProxies(
int $maxFetchJoinSameFieldCount,
): array
{
$identifierReflection = $classMetadata->getSingleIdReflectionProperty(); // e.g. Order::$id reflection
$identifierAccessor = $this->getSingleIdPropertyAccessor($classMetadata); // e.g. Order::$id reflection
$identifierName = $classMetadata->getSingleIdentifierFieldName(); // e.g. 'id'

if ($identifierReflection === null) {
if ($identifierAccessor === null) {
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
}

$uniqueEntities = [];
$uninitializedIds = [];

foreach ($entities as $entity) {
$entityId = $identifierReflection->getValue($entity);
$entityId = $identifierAccessor->getValue($entity);
$entityKey = (string) $entityId;
$uniqueEntities[$entityKey] = $entity;

Expand Down Expand Up @@ -167,11 +169,11 @@ private function preloadToMany(
int $maxFetchJoinSameFieldCount,
): array
{
$sourceIdentifierReflection = $sourceClassMetadata->getSingleIdReflectionProperty(); // e.g. Order::$id reflection
$sourcePropertyReflection = $sourceClassMetadata->getReflectionProperty($sourcePropertyName); // e.g. Order::$items reflection
$targetIdentifierReflection = $targetClassMetadata->getSingleIdReflectionProperty();
$sourceIdentifierAccessor = $this->getSingleIdPropertyAccessor($sourceClassMetadata); // e.g. Order::$id reflection
$sourcePropertyAccessor = $this->getPropertyAccessor($sourceClassMetadata, $sourcePropertyName); // e.g. Order::$items reflection
$targetIdentifierAccessor = $this->getSingleIdPropertyAccessor($targetClassMetadata);

if ($sourceIdentifierReflection === null || $sourcePropertyReflection === null || $targetIdentifierReflection === null) {
if ($sourceIdentifierAccessor === null || $sourcePropertyAccessor === null || $targetIdentifierAccessor === null) {
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
}

Expand All @@ -181,9 +183,9 @@ private function preloadToMany(
$uninitializedCollections = [];

foreach ($sourceEntities as $sourceEntity) {
$sourceEntityId = $sourceIdentifierReflection->getValue($sourceEntity);
$sourceEntityId = $sourceIdentifierAccessor->getValue($sourceEntity);
$sourceEntityKey = (string) $sourceEntityId;
$sourceEntityCollection = $sourcePropertyReflection->getValue($sourceEntity);
$sourceEntityCollection = $sourcePropertyAccessor->getValue($sourceEntity);

if (
$sourceEntityCollection instanceof PersistentCollection
Expand All @@ -196,7 +198,7 @@ private function preloadToMany(
}

foreach ($sourceEntityCollection as $targetEntity) {
$targetEntityKey = (string) $targetIdentifierReflection->getValue($targetEntity);
$targetEntityKey = (string) $targetIdentifierAccessor->getValue($targetEntity);
$targetEntities[$targetEntityKey] = $targetEntity;
}
}
Expand All @@ -213,10 +215,10 @@ private function preloadToMany(
$targetEntitiesChunk = $innerLoader(
associationMapping: $associationMapping,
sourceClassMetadata: $sourceClassMetadata,
sourceIdentifierReflection: $sourceIdentifierReflection,
sourceIdentifierAccessor: $sourceIdentifierAccessor,
sourcePropertyName: $sourcePropertyName,
targetClassMetadata: $targetClassMetadata,
targetIdentifierReflection: $targetIdentifierReflection,
targetIdentifierAccessor: $targetIdentifierAccessor,
uninitializedSourceEntityIdsChunk: array_values($uninitializedSourceEntityIdsChunk),
uninitializedCollections: $uninitializedCollections,
maxFetchJoinSameFieldCount: $maxFetchJoinSameFieldCount,
Expand Down Expand Up @@ -250,20 +252,20 @@ private function preloadToMany(
private function preloadOneToManyInner(
array|ArrayAccess $associationMapping,
ClassMetadata $sourceClassMetadata,
ReflectionProperty $sourceIdentifierReflection,
PropertyAccessor|ReflectionProperty $sourceIdentifierAccessor,
string $sourcePropertyName,
ClassMetadata $targetClassMetadata,
ReflectionProperty $targetIdentifierReflection,
PropertyAccessor|ReflectionProperty $targetIdentifierAccessor,
array $uninitializedSourceEntityIdsChunk,
array $uninitializedCollections,
int $maxFetchJoinSameFieldCount,
): array
{
$targetPropertyName = $sourceClassMetadata->getAssociationMappedByTargetField($sourcePropertyName); // e.g. 'order'
$targetPropertyReflection = $targetClassMetadata->getReflectionProperty($targetPropertyName); // e.g. Item::$order reflection
$targetPropertyAccessor = $this->getPropertyAccessor($targetClassMetadata, $targetPropertyName); // e.g. Item::$order reflection
$targetEntities = [];

if ($targetPropertyReflection === null) {
if ($targetPropertyAccessor === null) {
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
}

Expand All @@ -276,11 +278,11 @@ private function preloadOneToManyInner(
);

foreach ($targetEntitiesList as $targetEntity) {
$sourceEntity = $targetPropertyReflection->getValue($targetEntity);
$sourceEntityKey = (string) $sourceIdentifierReflection->getValue($sourceEntity);
$sourceEntity = $targetPropertyAccessor->getValue($targetEntity);
$sourceEntityKey = (string) $sourceIdentifierAccessor->getValue($sourceEntity);
$uninitializedCollections[$sourceEntityKey]->add($targetEntity);

$targetEntityKey = (string) $targetIdentifierReflection->getValue($targetEntity);
$targetEntityKey = (string) $targetIdentifierAccessor->getValue($targetEntity);
$targetEntities[$targetEntityKey] = $targetEntity;
}

Expand All @@ -302,10 +304,10 @@ private function preloadOneToManyInner(
private function preloadManyToManyInner(
array|ArrayAccess $associationMapping,
ClassMetadata $sourceClassMetadata,
ReflectionProperty $sourceIdentifierReflection,
PropertyAccessor|ReflectionProperty $sourceIdentifierAccessor,
string $sourcePropertyName,
ClassMetadata $targetClassMetadata,
ReflectionProperty $targetIdentifierReflection,
PropertyAccessor|ReflectionProperty $targetIdentifierAccessor,
array $uninitializedSourceEntityIdsChunk,
array $uninitializedCollections,
int $maxFetchJoinSameFieldCount,
Expand Down Expand Up @@ -346,7 +348,7 @@ private function preloadManyToManyInner(
}

foreach ($this->loadEntitiesBy($targetClassMetadata, $targetIdentifierName, array_values($uninitializedTargetEntityIds), $maxFetchJoinSameFieldCount) as $targetEntity) {
$targetEntityKey = (string) $targetIdentifierReflection->getValue($targetEntity);
$targetEntityKey = (string) $targetIdentifierAccessor->getValue($targetEntity);
$targetEntities[$targetEntityKey] = $targetEntity;
}

Expand Down Expand Up @@ -379,17 +381,17 @@ private function preloadToOne(
int $maxFetchJoinSameFieldCount,
): array
{
$sourcePropertyReflection = $sourceClassMetadata->getReflectionProperty($sourcePropertyName); // e.g. Item::$order reflection
$sourcePropertyAccessor = $this->getPropertyAccessor($sourceClassMetadata, $sourcePropertyName); // e.g. Item::$order reflection

if ($sourcePropertyReflection === null) {
if ($sourcePropertyAccessor === null) {
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
}

$batchSize ??= self::PRELOAD_ENTITY_DEFAULT_BATCH_SIZE;
$targetEntities = [];

foreach ($sourceEntities as $sourceEntity) {
$targetEntity = $sourcePropertyReflection->getValue($sourceEntity);
$targetEntity = $sourcePropertyAccessor->getValue($sourceEntity);

if ($targetEntity === null) {
continue;
Expand Down Expand Up @@ -483,4 +485,31 @@ private function addFetchJoinsToPreventFetchDuringHydration(
}
}

/**
* @param ClassMetadata<object> $classMetadata
*/
private function getSingleIdPropertyAccessor(ClassMetadata $classMetadata): PropertyAccessor|ReflectionProperty|null
{
if (method_exists($classMetadata, 'getSingleIdPropertyAccessor')) {
return $classMetadata->getSingleIdPropertyAccessor();
}

return $classMetadata->getSingleIdReflectionProperty();
}

/**
* @param ClassMetadata<object> $classMetadata
*/
private function getPropertyAccessor(
ClassMetadata $classMetadata,
string $property,
): PropertyAccessor|ReflectionProperty|null
{
if (method_exists($classMetadata, 'getPropertyAccessor')) {
return $classMetadata->getPropertyAccessor($property);
}

return $classMetadata->getReflectionProperty($property);
}

}
4 changes: 4 additions & 0 deletions tests/Fixtures/Blog/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InverseJoinColumn;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
Expand Down Expand Up @@ -36,6 +38,8 @@ class Article
* @var Collection<int, Tag>
*/
#[ManyToMany(targetEntity: Tag::class, inversedBy: 'articles')]
#[JoinColumn(nullable: false)]
#[InverseJoinColumn(nullable: false)]
private Collection $tags;

/**
Expand Down