Skip to content

Commit ddb9fab

Browse files
committed
Add backward compatibility with old Doctrine
1 parent aed8cdb commit ddb9fab

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
@@ -34,6 +34,17 @@ parameters:
3434
path: 'src/EntityPreloader.php'
3535
-
3636
message: '#internal interface Doctrine\\ORM\\Mapping\\PropertyAccessors\\PropertyAccessor#' # internal, although returned from public ClassMetadata::getPropertyAccessor
37+
reportUnmatched: false # only new Doctrine
38+
path: 'src/EntityPreloader.php'
39+
-
40+
message: '#Doctrine\\ORM\\Mapping\\PropertyAccessors\\PropertyAccessor#'
41+
reportUnmatched: false # only old Doctrine
42+
identifier: class.notFound
43+
path: 'src/EntityPreloader.php'
44+
-
45+
message: '#Call to function method_exists#'
46+
reportUnmatched: false # only new Doctrine
47+
identifier: function.alreadyNarrowedType
3748
path: 'src/EntityPreloader.php'
3849
-
3950
identifier: shipmonk.defaultMatchArmWithEnum

src/EntityPreloader.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
use Doctrine\ORM\PersistentCollection;
1313
use Doctrine\ORM\QueryBuilder;
1414
use LogicException;
15+
use ReflectionProperty;
1516
use function array_chunk;
1617
use function array_values;
1718
use function count;
1819
use function get_parent_class;
1920
use function is_a;
21+
use function method_exists;
2022

2123
/**
2224
* @template E of object
@@ -123,7 +125,7 @@ private function loadProxies(
123125
int $maxFetchJoinSameFieldCount,
124126
): array
125127
{
126-
$identifierAccessor = $classMetadata->getSingleIdPropertyAccessor(); // e.g. Order::$id reflection
128+
$identifierAccessor = $this->getSingleIdPropertyAccessor($classMetadata); // e.g. Order::$id reflection
127129
$identifierName = $classMetadata->getSingleIdentifierFieldName(); // e.g. 'id'
128130

129131
if ($identifierAccessor === null) {
@@ -170,9 +172,9 @@ private function preloadToMany(
170172
int $maxFetchJoinSameFieldCount,
171173
): array
172174
{
173-
$sourceIdentifierAccessor = $sourceClassMetadata->getSingleIdPropertyAccessor(); // e.g. Order::$id reflection
174-
$sourcePropertyAccessor = $sourceClassMetadata->getPropertyAccessor($sourcePropertyName); // e.g. Order::$items reflection
175-
$targetIdentifierAccessor = $targetClassMetadata->getSingleIdPropertyAccessor();
175+
$sourceIdentifierAccessor = $this->getSingleIdPropertyAccessor($sourceClassMetadata); // e.g. Order::$id reflection
176+
$sourcePropertyAccessor = $this->getPropertyAccessor($sourceClassMetadata, $sourcePropertyName); // e.g. Order::$items reflection
177+
$targetIdentifierAccessor = $this->getSingleIdPropertyAccessor($targetClassMetadata);
176178

177179
if ($sourceIdentifierAccessor === null || $sourcePropertyAccessor === null || $targetIdentifierAccessor === null) {
178180
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
@@ -253,17 +255,17 @@ private function preloadToMany(
253255
private function preloadOneToManyInner(
254256
array|ArrayAccess $associationMapping,
255257
ClassMetadata $sourceClassMetadata,
256-
PropertyAccessor $sourceIdentifierAccessor,
258+
PropertyAccessor|ReflectionProperty $sourceIdentifierAccessor,
257259
string $sourcePropertyName,
258260
ClassMetadata $targetClassMetadata,
259-
PropertyAccessor $targetIdentifierAccessor,
261+
PropertyAccessor|ReflectionProperty $targetIdentifierAccessor,
260262
array $uninitializedSourceEntityIdsChunk,
261263
array $uninitializedCollections,
262264
int $maxFetchJoinSameFieldCount,
263265
): array
264266
{
265267
$targetPropertyName = $sourceClassMetadata->getAssociationMappedByTargetField($sourcePropertyName); // e.g. 'order'
266-
$targetPropertyAccessor = $targetClassMetadata->getPropertyAccessor($targetPropertyName); // e.g. Item::$order reflection
268+
$targetPropertyAccessor = $this->getPropertyAccessor($targetClassMetadata, $targetPropertyName); // e.g. Item::$order reflection
267269
$targetEntities = [];
268270

269271
if ($targetPropertyAccessor === null) {
@@ -306,10 +308,10 @@ private function preloadOneToManyInner(
306308
private function preloadManyToManyInner(
307309
array|ArrayAccess $associationMapping,
308310
ClassMetadata $sourceClassMetadata,
309-
PropertyAccessor $sourceIdentifierAccessor,
311+
PropertyAccessor|ReflectionProperty $sourceIdentifierAccessor,
310312
string $sourcePropertyName,
311313
ClassMetadata $targetClassMetadata,
312-
PropertyAccessor $targetIdentifierAccessor,
314+
PropertyAccessor|ReflectionProperty $targetIdentifierAccessor,
313315
array $uninitializedSourceEntityIdsChunk,
314316
array $uninitializedCollections,
315317
int $maxFetchJoinSameFieldCount,
@@ -389,7 +391,7 @@ private function preloadToOne(
389391
int $maxFetchJoinSameFieldCount,
390392
): array
391393
{
392-
$sourcePropertyAccessor = $sourceClassMetadata->getPropertyAccessor($sourcePropertyName); // e.g. Item::$order reflection
394+
$sourcePropertyAccessor = $this->getPropertyAccessor($sourceClassMetadata, $sourcePropertyName); // e.g. Item::$order reflection
393395

394396
if ($sourcePropertyAccessor === null) {
395397
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
@@ -549,4 +551,31 @@ private function addFetchJoinsToPreventFetchDuringHydration(
549551
}
550552
}
551553

554+
/**
555+
* @param ClassMetadata<object> $classMetadata
556+
*/
557+
private function getSingleIdPropertyAccessor(ClassMetadata $classMetadata): PropertyAccessor|ReflectionProperty|null
558+
{
559+
if (method_exists($classMetadata, 'getSingleIdPropertyAccessor')) {
560+
return $classMetadata->getSingleIdPropertyAccessor();
561+
}
562+
563+
return $classMetadata->getSingleIdReflectionProperty();
564+
}
565+
566+
/**
567+
* @param ClassMetadata<object> $classMetadata
568+
*/
569+
private function getPropertyAccessor(
570+
ClassMetadata $classMetadata,
571+
string $property,
572+
): PropertyAccessor|ReflectionProperty|null
573+
{
574+
if (method_exists($classMetadata, 'getPropertyAccessor')) {
575+
return $classMetadata->getPropertyAccessor($property);
576+
}
577+
578+
return $classMetadata->getReflectionProperty($property);
579+
}
580+
552581
}

0 commit comments

Comments
 (0)