Skip to content

Commit af11641

Browse files
committed
Add backward compatibility with old Doctrine
1 parent 7b00c45 commit af11641

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

phpstan.neon.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ parameters:
3131
path: 'src/EntityPreloader.php'
3232
-
3333
message: '#internal interface Doctrine\\ORM\\Mapping\\PropertyAccessors\\PropertyAccessor#' # internal, although returned from public ClassMetadata::getPropertyAccessor
34+
reportUnmatched: false # only new Doctrine
35+
path: 'src/EntityPreloader.php'
36+
-
37+
message: '#Doctrine\\ORM\\Mapping\\PropertyAccessors\\PropertyAccessor#'
38+
reportUnmatched: false # only old Doctrine
39+
identifier: class.notFound
40+
path: 'src/EntityPreloader.php'
41+
-
42+
message: '#Call to function method_exists#'
43+
reportUnmatched: false # only new Doctrine
44+
identifier: function.alreadyNarrowedType
3445
path: 'src/EntityPreloader.php'
3546
-
3647
message: '#Result of \|\| is always false#'

src/EntityPreloader.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
use Doctrine\ORM\PersistentCollection;
1010
use Doctrine\ORM\QueryBuilder;
1111
use LogicException;
12+
use ReflectionProperty;
1213
use function array_chunk;
1314
use function array_values;
1415
use function count;
1516
use function get_parent_class;
1617
use function is_a;
18+
use function method_exists;
1719

1820
/**
1921
* @template E of object
@@ -120,7 +122,7 @@ private function loadProxies(
120122
int $maxFetchJoinSameFieldCount,
121123
): array
122124
{
123-
$identifierAccessor = $classMetadata->getSingleIdPropertyAccessor(); // e.g. Order::$id reflection
125+
$identifierAccessor = $this->getSingleIdPropertyAccessor($classMetadata); // e.g. Order::$id reflection
124126
$identifierName = $classMetadata->getSingleIdentifierFieldName(); // e.g. 'id'
125127

126128
if ($identifierAccessor === null) {
@@ -167,9 +169,9 @@ private function preloadToMany(
167169
int $maxFetchJoinSameFieldCount,
168170
): array
169171
{
170-
$sourceIdentifierAccessor = $sourceClassMetadata->getSingleIdPropertyAccessor(); // e.g. Order::$id reflection
171-
$sourcePropertyAccessor = $sourceClassMetadata->getPropertyAccessor($sourcePropertyName); // e.g. Order::$items reflection
172-
$targetIdentifierAccessor = $targetClassMetadata->getSingleIdPropertyAccessor();
172+
$sourceIdentifierAccessor = $this->getSingleIdPropertyAccessor($sourceClassMetadata); // e.g. Order::$id reflection
173+
$sourcePropertyAccessor = $this->getPropertyAccessor($sourceClassMetadata, $sourcePropertyName); // e.g. Order::$items reflection
174+
$targetIdentifierAccessor = $this->getSingleIdPropertyAccessor($targetClassMetadata);
173175

174176
if ($sourceIdentifierAccessor === null || $sourcePropertyAccessor === null || $targetIdentifierAccessor === null) {
175177
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
@@ -250,17 +252,17 @@ private function preloadToMany(
250252
private function preloadOneToManyInner(
251253
array|ArrayAccess $associationMapping,
252254
ClassMetadata $sourceClassMetadata,
253-
PropertyAccessor $sourceIdentifierAccessor,
255+
PropertyAccessor|ReflectionProperty $sourceIdentifierAccessor,
254256
string $sourcePropertyName,
255257
ClassMetadata $targetClassMetadata,
256-
PropertyAccessor $targetIdentifierAccessor,
258+
PropertyAccessor|ReflectionProperty $targetIdentifierAccessor,
257259
array $uninitializedSourceEntityIdsChunk,
258260
array $uninitializedCollections,
259261
int $maxFetchJoinSameFieldCount,
260262
): array
261263
{
262264
$targetPropertyName = $sourceClassMetadata->getAssociationMappedByTargetField($sourcePropertyName); // e.g. 'order'
263-
$targetPropertyAccessor = $targetClassMetadata->getPropertyAccessor($targetPropertyName); // e.g. Item::$order reflection
265+
$targetPropertyAccessor = $this->getPropertyAccessor($targetClassMetadata, $targetPropertyName); // e.g. Item::$order reflection
264266
$targetEntities = [];
265267

266268
if ($targetPropertyAccessor === null) {
@@ -302,10 +304,10 @@ private function preloadOneToManyInner(
302304
private function preloadManyToManyInner(
303305
array|ArrayAccess $associationMapping,
304306
ClassMetadata $sourceClassMetadata,
305-
PropertyAccessor $sourceIdentifierAccessor,
307+
PropertyAccessor|ReflectionProperty $sourceIdentifierAccessor,
306308
string $sourcePropertyName,
307309
ClassMetadata $targetClassMetadata,
308-
PropertyAccessor $targetIdentifierAccessor,
310+
PropertyAccessor|ReflectionProperty $targetIdentifierAccessor,
309311
array $uninitializedSourceEntityIdsChunk,
310312
array $uninitializedCollections,
311313
int $maxFetchJoinSameFieldCount,
@@ -379,7 +381,7 @@ private function preloadToOne(
379381
int $maxFetchJoinSameFieldCount,
380382
): array
381383
{
382-
$sourcePropertyAccessor = $sourceClassMetadata->getPropertyAccessor($sourcePropertyName); // e.g. Item::$order reflection
384+
$sourcePropertyAccessor = $this->getPropertyAccessor($sourceClassMetadata, $sourcePropertyName); // e.g. Item::$order reflection
383385

384386
if ($sourcePropertyAccessor === null) {
385387
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
@@ -483,4 +485,31 @@ private function addFetchJoinsToPreventFetchDuringHydration(
483485
}
484486
}
485487

488+
/**
489+
* @param ClassMetadata<object> $classMetadata
490+
*/
491+
private function getSingleIdPropertyAccessor(ClassMetadata $classMetadata): PropertyAccessor|ReflectionProperty|null
492+
{
493+
if (method_exists($classMetadata, 'getSingleIdPropertyAccessor')) {
494+
return $classMetadata->getSingleIdPropertyAccessor();
495+
}
496+
497+
return $classMetadata->getSingleIdReflectionProperty();
498+
}
499+
500+
/**
501+
* @param ClassMetadata<object> $classMetadata
502+
*/
503+
private function getPropertyAccessor(
504+
ClassMetadata $classMetadata,
505+
string $property,
506+
): PropertyAccessor|ReflectionProperty|null
507+
{
508+
if (method_exists($classMetadata, 'getPropertyAccessor')) {
509+
return $classMetadata->getPropertyAccessor($property);
510+
}
511+
512+
return $classMetadata->getReflectionProperty($property);
513+
}
514+
486515
}

0 commit comments

Comments
 (0)