Skip to content

Commit 7b00c45

Browse files
committed
Eliminate Doctrine deprecations
1 parent 03ca7e1 commit 7b00c45

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ parameters:
2929
identifier: 'identical.alwaysFalse'
3030
reportUnmatched: false
3131
path: 'src/EntityPreloader.php'
32+
-
33+
message: '#internal interface Doctrine\\ORM\\Mapping\\PropertyAccessors\\PropertyAccessor#' # internal, although returned from public ClassMetadata::getPropertyAccessor
34+
path: 'src/EntityPreloader.php'
3235
-
3336
message: '#Result of \|\| is always false#'
3437
identifier: 'booleanOr.alwaysFalse'

phpunit.xml.dist

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@
1313
displayDetailsOnTestsThatTriggerErrors="true"
1414
displayDetailsOnTestsThatTriggerWarnings="true"
1515
displayDetailsOnTestsThatTriggerNotices="true"
16+
displayDetailsOnTestsThatTriggerDeprecations="true"
1617
cacheDirectory="cache/phpunit"
1718
>
19+
<php>
20+
<ini name="error_reporting" value="-1"/>
21+
<env name="DOCTRINE_DEPRECATIONS" value="trigger"/>
22+
</php>
23+
1824
<testsuites>
1925
<testsuite name="default">
2026
<directory>tests</directory>
2127
</testsuite>
2228
</testsuites>
2329

24-
<source>
30+
<source ignoreSuppressionOfDeprecations="true">
2531
<include>
2632
<directory>src</directory>
2733
</include>

src/EntityPreloader.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use ArrayAccess;
66
use Doctrine\ORM\EntityManagerInterface;
77
use Doctrine\ORM\Mapping\ClassMetadata;
8+
use Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor;
89
use Doctrine\ORM\PersistentCollection;
910
use Doctrine\ORM\QueryBuilder;
1011
use LogicException;
11-
use ReflectionProperty;
1212
use function array_chunk;
1313
use function array_values;
1414
use function count;
@@ -120,18 +120,18 @@ private function loadProxies(
120120
int $maxFetchJoinSameFieldCount,
121121
): array
122122
{
123-
$identifierReflection = $classMetadata->getSingleIdReflectionProperty(); // e.g. Order::$id reflection
123+
$identifierAccessor = $classMetadata->getSingleIdPropertyAccessor(); // e.g. Order::$id reflection
124124
$identifierName = $classMetadata->getSingleIdentifierFieldName(); // e.g. 'id'
125125

126-
if ($identifierReflection === null) {
126+
if ($identifierAccessor === null) {
127127
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
128128
}
129129

130130
$uniqueEntities = [];
131131
$uninitializedIds = [];
132132

133133
foreach ($entities as $entity) {
134-
$entityId = $identifierReflection->getValue($entity);
134+
$entityId = $identifierAccessor->getValue($entity);
135135
$entityKey = (string) $entityId;
136136
$uniqueEntities[$entityKey] = $entity;
137137

@@ -167,11 +167,11 @@ private function preloadToMany(
167167
int $maxFetchJoinSameFieldCount,
168168
): array
169169
{
170-
$sourceIdentifierReflection = $sourceClassMetadata->getSingleIdReflectionProperty(); // e.g. Order::$id reflection
171-
$sourcePropertyReflection = $sourceClassMetadata->getReflectionProperty($sourcePropertyName); // e.g. Order::$items reflection
172-
$targetIdentifierReflection = $targetClassMetadata->getSingleIdReflectionProperty();
170+
$sourceIdentifierAccessor = $sourceClassMetadata->getSingleIdPropertyAccessor(); // e.g. Order::$id reflection
171+
$sourcePropertyAccessor = $sourceClassMetadata->getPropertyAccessor($sourcePropertyName); // e.g. Order::$items reflection
172+
$targetIdentifierAccessor = $targetClassMetadata->getSingleIdPropertyAccessor();
173173

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

@@ -181,9 +181,9 @@ private function preloadToMany(
181181
$uninitializedCollections = [];
182182

183183
foreach ($sourceEntities as $sourceEntity) {
184-
$sourceEntityId = $sourceIdentifierReflection->getValue($sourceEntity);
184+
$sourceEntityId = $sourceIdentifierAccessor->getValue($sourceEntity);
185185
$sourceEntityKey = (string) $sourceEntityId;
186-
$sourceEntityCollection = $sourcePropertyReflection->getValue($sourceEntity);
186+
$sourceEntityCollection = $sourcePropertyAccessor->getValue($sourceEntity);
187187

188188
if (
189189
$sourceEntityCollection instanceof PersistentCollection
@@ -196,7 +196,7 @@ private function preloadToMany(
196196
}
197197

198198
foreach ($sourceEntityCollection as $targetEntity) {
199-
$targetEntityKey = (string) $targetIdentifierReflection->getValue($targetEntity);
199+
$targetEntityKey = (string) $targetIdentifierAccessor->getValue($targetEntity);
200200
$targetEntities[$targetEntityKey] = $targetEntity;
201201
}
202202
}
@@ -213,10 +213,10 @@ private function preloadToMany(
213213
$targetEntitiesChunk = $innerLoader(
214214
associationMapping: $associationMapping,
215215
sourceClassMetadata: $sourceClassMetadata,
216-
sourceIdentifierReflection: $sourceIdentifierReflection,
216+
sourceIdentifierAccessor: $sourceIdentifierAccessor,
217217
sourcePropertyName: $sourcePropertyName,
218218
targetClassMetadata: $targetClassMetadata,
219-
targetIdentifierReflection: $targetIdentifierReflection,
219+
targetIdentifierAccessor: $targetIdentifierAccessor,
220220
uninitializedSourceEntityIdsChunk: array_values($uninitializedSourceEntityIdsChunk),
221221
uninitializedCollections: $uninitializedCollections,
222222
maxFetchJoinSameFieldCount: $maxFetchJoinSameFieldCount,
@@ -250,20 +250,20 @@ private function preloadToMany(
250250
private function preloadOneToManyInner(
251251
array|ArrayAccess $associationMapping,
252252
ClassMetadata $sourceClassMetadata,
253-
ReflectionProperty $sourceIdentifierReflection,
253+
PropertyAccessor $sourceIdentifierAccessor,
254254
string $sourcePropertyName,
255255
ClassMetadata $targetClassMetadata,
256-
ReflectionProperty $targetIdentifierReflection,
256+
PropertyAccessor $targetIdentifierAccessor,
257257
array $uninitializedSourceEntityIdsChunk,
258258
array $uninitializedCollections,
259259
int $maxFetchJoinSameFieldCount,
260260
): array
261261
{
262262
$targetPropertyName = $sourceClassMetadata->getAssociationMappedByTargetField($sourcePropertyName); // e.g. 'order'
263-
$targetPropertyReflection = $targetClassMetadata->getReflectionProperty($targetPropertyName); // e.g. Item::$order reflection
263+
$targetPropertyAccessor = $targetClassMetadata->getPropertyAccessor($targetPropertyName); // e.g. Item::$order reflection
264264
$targetEntities = [];
265265

266-
if ($targetPropertyReflection === null) {
266+
if ($targetPropertyAccessor === null) {
267267
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
268268
}
269269

@@ -276,11 +276,11 @@ private function preloadOneToManyInner(
276276
);
277277

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

283-
$targetEntityKey = (string) $targetIdentifierReflection->getValue($targetEntity);
283+
$targetEntityKey = (string) $targetIdentifierAccessor->getValue($targetEntity);
284284
$targetEntities[$targetEntityKey] = $targetEntity;
285285
}
286286

@@ -302,10 +302,10 @@ private function preloadOneToManyInner(
302302
private function preloadManyToManyInner(
303303
array|ArrayAccess $associationMapping,
304304
ClassMetadata $sourceClassMetadata,
305-
ReflectionProperty $sourceIdentifierReflection,
305+
PropertyAccessor $sourceIdentifierAccessor,
306306
string $sourcePropertyName,
307307
ClassMetadata $targetClassMetadata,
308-
ReflectionProperty $targetIdentifierReflection,
308+
PropertyAccessor $targetIdentifierAccessor,
309309
array $uninitializedSourceEntityIdsChunk,
310310
array $uninitializedCollections,
311311
int $maxFetchJoinSameFieldCount,
@@ -346,7 +346,7 @@ private function preloadManyToManyInner(
346346
}
347347

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

@@ -379,17 +379,17 @@ private function preloadToOne(
379379
int $maxFetchJoinSameFieldCount,
380380
): array
381381
{
382-
$sourcePropertyReflection = $sourceClassMetadata->getReflectionProperty($sourcePropertyName); // e.g. Item::$order reflection
382+
$sourcePropertyAccessor = $sourceClassMetadata->getPropertyAccessor($sourcePropertyName); // e.g. Item::$order reflection
383383

384-
if ($sourcePropertyReflection === null) {
384+
if ($sourcePropertyAccessor === null) {
385385
throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.');
386386
}
387387

388388
$batchSize ??= self::PRELOAD_ENTITY_DEFAULT_BATCH_SIZE;
389389
$targetEntities = [];
390390

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

394394
if ($targetEntity === null) {
395395
continue;

tests/Fixtures/Blog/Article.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Doctrine\ORM\Mapping\Entity;
1010
use Doctrine\ORM\Mapping\GeneratedValue;
1111
use Doctrine\ORM\Mapping\Id;
12+
use Doctrine\ORM\Mapping\InverseJoinColumn;
13+
use Doctrine\ORM\Mapping\JoinColumn;
1214
use Doctrine\ORM\Mapping\ManyToMany;
1315
use Doctrine\ORM\Mapping\ManyToOne;
1416
use Doctrine\ORM\Mapping\OneToMany;
@@ -36,6 +38,8 @@ class Article
3638
* @var Collection<int, Tag>
3739
*/
3840
#[ManyToMany(targetEntity: Tag::class, inversedBy: 'articles')]
41+
#[JoinColumn(nullable: false)]
42+
#[InverseJoinColumn(nullable: false)]
3943
private Collection $tags;
4044

4145
/**

0 commit comments

Comments
 (0)