Skip to content

Commit 7d950ab

Browse files
authored
do not call setAccessible() on PHP >= 8.1 (doctrine#12182)
1 parent 8fe1200 commit 7d950ab

21 files changed

+182
-42
lines changed

src/Proxy/ProxyFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,10 @@ private function createCloner(ClassMetadata $classMetadata, EntityPersister $ent
426426
continue;
427427
}
428428

429-
$property->setAccessible(true);
429+
if (PHP_VERSION_ID < 80100) {
430+
$property->setAccessible(true);
431+
}
432+
430433
$property->setValue($proxy, $property->getValue($original));
431434
}
432435
};

src/UnitOfWork.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
use function sprintf;
7878
use function strtolower;
7979

80+
use const PHP_VERSION_ID;
81+
8082
/**
8183
* The UnitOfWork is responsible for tracking changes to objects during an
8284
* "object-level" transaction and for writing out changes to the database
@@ -3886,7 +3888,9 @@ private function mergeEntityStateIntoManagedCopy($entity, $managedCopy): void
38863888
foreach ($this->reflectionPropertiesGetter->getProperties($class->name) as $prop) {
38873889
$name = $prop->name;
38883890

3889-
$prop->setAccessible(true);
3891+
if (PHP_VERSION_ID < 80100) {
3892+
$prop->setAccessible(true);
3893+
}
38903894

38913895
if (! isset($class->associationMappings[$name])) {
38923896
if (! $class->isIdentifier($name)) {

tests/Tests/ORM/Cache/DefaultCacheTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
use function array_merge;
2222

23+
use const PHP_VERSION_ID;
24+
2325
/** @group DDC-2183 */
2426
class DefaultCacheTest extends OrmTestCase
2527
{
@@ -244,8 +246,11 @@ public function testToIdentifierArrayShouldLookupForEntityIdentifier(): void
244246
$method = new ReflectionMethod($this->cache, 'toIdentifierArray');
245247
$property = new ReflectionProperty($entity, 'id');
246248

247-
$property->setAccessible(true);
248-
$method->setAccessible(true);
249+
if (PHP_VERSION_ID < 80100) {
250+
$property->setAccessible(true);
251+
$method->setAccessible(true);
252+
}
253+
249254
$property->setValue($entity, $identifier);
250255

251256
self::assertEquals(['id' => $identifier], $method->invoke($this->cache, $metadata, $identifier));

tests/Tests/ORM/Cache/DefaultQueryCacheTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
use function microtime;
3131
use function sprintf;
3232

33+
use const PHP_VERSION_ID;
34+
3335
/** @group DDC-2183 */
3436
class DefaultQueryCacheTest extends OrmTestCase
3537
{
@@ -585,7 +587,9 @@ public function testGetAssociationValue(): void
585587
$rsm = new ResultSetMappingBuilder($this->em);
586588
$key = new QueryCacheKey('query.key1', 0);
587589

588-
$reflection->setAccessible(true);
590+
if (PHP_VERSION_ID < 80100) {
591+
$reflection->setAccessible(true);
592+
}
589593

590594
$germany = new Country('Germany');
591595
$bavaria = new State('Bavaria', $germany);

tests/Tests/ORM/Cache/FileLockRegionTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use function unlink;
2828

2929
use const E_WARNING;
30+
use const PHP_VERSION_ID;
3031

3132
/**
3233
* @extends RegionTestCase<FileLockRegion>
@@ -46,7 +47,9 @@ private function getFileName(FileLockRegion $region, CacheKey $key): string
4647
{
4748
$reflection = new ReflectionMethod($region, 'getLockFileName');
4849

49-
$reflection->setAccessible(true);
50+
if (PHP_VERSION_ID < 80100) {
51+
$reflection->setAccessible(true);
52+
}
5053

5154
return $reflection->invoke($region, $key);
5255
}
@@ -227,7 +230,10 @@ public function testLockLifetime(): void
227230
$file = $this->getFileName($this->region, $key);
228231
$property = new ReflectionProperty($this->region, 'lockLifetime');
229232

230-
$property->setAccessible(true);
233+
if (PHP_VERSION_ID < 80100) {
234+
$property->setAccessible(true);
235+
}
236+
231237
$property->setValue($this->region, -10);
232238

233239
self::assertFalse($this->region->contains($key));
@@ -253,7 +259,10 @@ public function testHandlesScanErrorsGracefullyOnEvictAll(): void
253259
$region = $this->createRegion();
254260
$reflectionDirectory = new ReflectionProperty($region, 'directory');
255261

256-
$reflectionDirectory->setAccessible(true);
262+
if (PHP_VERSION_ID < 80100) {
263+
$reflectionDirectory->setAccessible(true);
264+
}
265+
257266
$reflectionDirectory->setValue($region, str_repeat('a', 10000));
258267

259268
set_error_handler(static function (): bool {

tests/Tests/ORM/Cache/Persister/Collection/ReadWriteCachedCollectionPersisterTest.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Doctrine\Tests\Models\Cache\State;
1616
use ReflectionProperty;
1717

18+
use const PHP_VERSION_ID;
19+
1820
/** @group DDC-2183 */
1921
class ReadWriteCachedCollectionPersisterTest extends CollectionPersisterTestCase
2022
{
@@ -121,7 +123,9 @@ public function testTransactionRollBackDeleteShouldClearQueue(): void
121123
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
122124
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
123125

124-
$property->setAccessible(true);
126+
if (PHP_VERSION_ID < 80100) {
127+
$property->setAccessible(true);
128+
}
125129

126130
$this->region->expects(self::once())
127131
->method('lock')
@@ -153,7 +157,9 @@ public function testTransactionRollBackUpdateShouldClearQueue(): void
153157
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
154158
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
155159

156-
$property->setAccessible(true);
160+
if (PHP_VERSION_ID < 80100) {
161+
$property->setAccessible(true);
162+
}
157163

158164
$this->region->expects(self::once())
159165
->method('lock')
@@ -185,7 +191,9 @@ public function testTransactionRollCommitDeleteShouldClearQueue(): void
185191
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
186192
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
187193

188-
$property->setAccessible(true);
194+
if (PHP_VERSION_ID < 80100) {
195+
$property->setAccessible(true);
196+
}
189197

190198
$this->region->expects(self::once())
191199
->method('lock')
@@ -217,7 +225,9 @@ public function testTransactionRollCommitUpdateShouldClearQueue(): void
217225
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
218226
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
219227

220-
$property->setAccessible(true);
228+
if (PHP_VERSION_ID < 80100) {
229+
$property->setAccessible(true);
230+
}
221231

222232
$this->region->expects(self::once())
223233
->method('lock')
@@ -248,7 +258,9 @@ public function testDeleteLockFailureShouldIgnoreQueue(): void
248258
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
249259
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
250260

251-
$property->setAccessible(true);
261+
if (PHP_VERSION_ID < 80100) {
262+
$property->setAccessible(true);
263+
}
252264

253265
$this->region->expects(self::once())
254266
->method('lock')
@@ -273,7 +285,9 @@ public function testUpdateLockFailureShouldIgnoreQueue(): void
273285
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
274286
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');
275287

276-
$property->setAccessible(true);
288+
if (PHP_VERSION_ID < 80100) {
289+
$property->setAccessible(true);
290+
}
277291

278292
$this->region->expects(self::once())
279293
->method('lock')

tests/Tests/ORM/Cache/Persister/Entity/NonStrictReadWriteCachedEntityPersisterTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Doctrine\Tests\Models\Cache\Country;
1616
use ReflectionProperty;
1717

18+
use const PHP_VERSION_ID;
19+
1820
/** @group DDC-2183 */
1921
class NonStrictReadWriteCachedEntityPersisterTest extends EntityPersisterTestCase
2022
{
@@ -29,7 +31,9 @@ public function testTransactionRollBackShouldClearQueue(): void
2931
$persister = $this->createPersisterDefault();
3032
$property = new ReflectionProperty($persister, 'queuedCache');
3133

32-
$property->setAccessible(true);
34+
if (PHP_VERSION_ID < 80100) {
35+
$property->setAccessible(true);
36+
}
3337

3438
$this->em->getUnitOfWork()->registerManaged($entity, ['id' => 1], ['id' => 1, 'name' => 'Foo']);
3539

@@ -51,7 +55,9 @@ public function testInsertTransactionCommitShouldPutCache(): void
5155
$entry = new EntityCacheEntry(Country::class, ['id' => 1, 'name' => 'Foo']);
5256
$property = new ReflectionProperty($persister, 'queuedCache');
5357

54-
$property->setAccessible(true);
58+
if (PHP_VERSION_ID < 80100) {
59+
$property->setAccessible(true);
60+
}
5561

5662
$this->region->expects(self::once())
5763
->method('put')
@@ -88,7 +94,9 @@ public function testUpdateTransactionCommitShouldPutCache(): void
8894
$entry = new EntityCacheEntry(Country::class, ['id' => 1, 'name' => 'Foo']);
8995
$property = new ReflectionProperty($persister, 'queuedCache');
9096

91-
$property->setAccessible(true);
97+
if (PHP_VERSION_ID < 80100) {
98+
$property->setAccessible(true);
99+
}
92100

93101
$this->region->expects(self::once())
94102
->method('put')
@@ -116,7 +124,9 @@ public function testDeleteTransactionCommitShouldEvictCache(): void
116124
$key = new EntityCacheKey(Country::class, ['id' => 1]);
117125
$property = new ReflectionProperty($persister, 'queuedCache');
118126

119-
$property->setAccessible(true);
127+
if (PHP_VERSION_ID < 80100) {
128+
$property->setAccessible(true);
129+
}
120130

121131
$this->region->expects(self::once())
122132
->method('evict')

tests/Tests/ORM/Cache/Persister/Entity/ReadWriteCachedEntityPersisterTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Doctrine\Tests\Models\Cache\Country;
1717
use ReflectionProperty;
1818

19+
use const PHP_VERSION_ID;
20+
1921
/** @group DDC-2183 */
2022
class ReadWriteCachedEntityPersisterTest extends EntityPersisterTestCase
2123
{
@@ -116,7 +118,9 @@ public function testTransactionRollBackShouldClearQueue(): void
116118
$key = new EntityCacheKey(Country::class, ['id' => 1]);
117119
$property = new ReflectionProperty(ReadWriteCachedEntityPersister::class, 'queuedCache');
118120

119-
$property->setAccessible(true);
121+
if (PHP_VERSION_ID < 80100) {
122+
$property->setAccessible(true);
123+
}
120124

121125
$this->region->expects(self::exactly(2))
122126
->method('lock')
@@ -147,7 +151,9 @@ public function testTransactionCommitShouldClearQueue(): void
147151
$key = new EntityCacheKey(Country::class, ['id' => 1]);
148152
$property = new ReflectionProperty(ReadWriteCachedEntityPersister::class, 'queuedCache');
149153

150-
$property->setAccessible(true);
154+
if (PHP_VERSION_ID < 80100) {
155+
$property->setAccessible(true);
156+
}
151157

152158
$this->region->expects(self::exactly(2))
153159
->method('lock')
@@ -177,7 +183,9 @@ public function testDeleteLockFailureShouldIgnoreQueue(): void
177183
$key = new EntityCacheKey(Country::class, ['id' => 1]);
178184
$property = new ReflectionProperty(ReadWriteCachedEntityPersister::class, 'queuedCache');
179185

180-
$property->setAccessible(true);
186+
if (PHP_VERSION_ID < 80100) {
187+
$property->setAccessible(true);
188+
}
181189

182190
$this->region->expects(self::once())
183191
->method('lock')
@@ -201,7 +209,9 @@ public function testUpdateLockFailureShouldIgnoreQueue(): void
201209
$key = new EntityCacheKey(Country::class, ['id' => 1]);
202210
$property = new ReflectionProperty(ReadWriteCachedEntityPersister::class, 'queuedCache');
203211

204-
$property->setAccessible(true);
212+
if (PHP_VERSION_ID < 80100) {
213+
$property->setAccessible(true);
214+
}
205215

206216
$this->region->expects(self::once())
207217
->method('lock')

tests/Tests/ORM/Functional/PaginationTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use function iterator_to_array;
2828
use function sprintf;
2929

30+
use const PHP_VERSION_ID;
31+
3032
/** @group DDC-1613 */
3133
class PaginationTest extends OrmFunctionalTestCase
3234
{
@@ -630,7 +632,9 @@ public function testCountQueryStripsParametersInSelect(): void
630632

631633
$getCountQuery = new ReflectionMethod($paginator, 'getCountQuery');
632634

633-
$getCountQuery->setAccessible(true);
635+
if (PHP_VERSION_ID < 80100) {
636+
$getCountQuery->setAccessible(true);
637+
}
634638

635639
self::assertCount(2, $getCountQuery->invoke($paginator)->getParameters());
636640
self::assertCount(9, $paginator);

tests/Tests/ORM/Functional/ParserResultSerializationTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use function serialize;
2424
use function unserialize;
2525

26+
use const PHP_VERSION_ID;
27+
2628
class ParserResultSerializationTest extends OrmFunctionalTestCase
2729
{
2830
protected function setUp(): void
@@ -112,7 +114,10 @@ public function testItSerializesParserResultWithAForwardCompatibleFormat(): void
112114
$unserialized = unserialize($serialized);
113115

114116
$r = new ReflectionProperty($unserialized->getSqlExecutor(), '_sqlStatements');
115-
$r->setAccessible(true);
117+
118+
if (PHP_VERSION_ID < 80100) {
119+
$r->setAccessible(true);
120+
}
116121

117122
$this->assertSame(
118123
$r->getValue($unserialized->getSqlExecutor()),
@@ -166,7 +171,10 @@ public function testSymfony44ProvidedData(): void
166171
private static function parseQuery(Query $query): ParserResult
167172
{
168173
$r = new ReflectionMethod($query, 'parse');
169-
$r->setAccessible(true);
174+
175+
if (PHP_VERSION_ID < 80100) {
176+
$r->setAccessible(true);
177+
}
170178

171179
return $r->invoke($query);
172180
}

0 commit comments

Comments
 (0)