Skip to content

Commit

Permalink
Remove setAccessible() calls (doctrine#10286)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Dec 10, 2022
1 parent a17809c commit 9d8cadf
Show file tree
Hide file tree
Showing 15 changed files with 14 additions and 70 deletions.
1 change: 0 additions & 1 deletion lib/Doctrine/ORM/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ private function createLazyInitializer(ClassMetadata $classMetadata, EntityPersi
continue;
}

$property->setAccessible(true);
$property->setValue($proxy, $property->getValue($entity));
}
};
Expand Down
2 changes: 0 additions & 2 deletions tests/Doctrine/Tests/ORM/Cache/DefaultCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ public function testToIdentifierArrayShouldLookupForEntityIdentifier(): void
$method = new ReflectionMethod($this->cache, 'toIdentifierArray');
$property = new ReflectionProperty($entity, 'id');

$property->setAccessible(true);
$method->setAccessible(true);
$property->setValue($entity, $identifier);

self::assertEquals(['id' => $identifier], $method->invoke($this->cache, $metadata, $identifier));
Expand Down
2 changes: 0 additions & 2 deletions tests/Doctrine/Tests/ORM/Cache/DefaultQueryCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,6 @@ public function testGetAssociationValue(): void
$rsm = new ResultSetMappingBuilder($this->em);
$key = new QueryCacheKey('query.key1', 0);

$reflection->setAccessible(true);

$germany = new Country('Germany');
$bavaria = new State('Bavaria', $germany);
$wurzburg = new City('Würzburg', $bavaria);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ public function testTransactionRollBackDeleteShouldClearQueue(): void
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('lock')
->with(self::equalTo($key))
Expand Down Expand Up @@ -154,8 +152,6 @@ public function testTransactionRollBackUpdateShouldClearQueue(): void
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('lock')
->with(self::equalTo($key))
Expand Down Expand Up @@ -186,8 +182,6 @@ public function testTransactionRollCommitDeleteShouldClearQueue(): void
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('lock')
->with(self::equalTo($key))
Expand Down Expand Up @@ -218,8 +212,6 @@ public function testTransactionRollCommitUpdateShouldClearQueue(): void
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('lock')
->with(self::equalTo($key))
Expand Down Expand Up @@ -249,8 +241,6 @@ public function testDeleteLockFailureShouldIgnoreQueue(): void
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('lock')
->with(self::equalTo($key))
Expand All @@ -274,8 +264,6 @@ public function testUpdateLockFailureShouldIgnoreQueue(): void
$key = new CollectionCacheKey(State::class, 'cities', ['id' => 1]);
$property = new ReflectionProperty(ReadWriteCachedCollectionPersister::class, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('lock')
->with(self::equalTo($key))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public function testTransactionRollBackShouldClearQueue(): void
$persister = $this->createPersisterDefault();
$property = new ReflectionProperty($persister, 'queuedCache');

$property->setAccessible(true);

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

$persister->update($entity);
Expand All @@ -51,8 +49,6 @@ public function testInsertTransactionCommitShouldPutCache(): void
$entry = new EntityCacheEntry(Country::class, ['id' => 1, 'name' => 'Foo']);
$property = new ReflectionProperty($persister, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('put')
->with(self::equalTo($key), self::equalTo($entry));
Expand Down Expand Up @@ -88,8 +84,6 @@ public function testUpdateTransactionCommitShouldPutCache(): void
$entry = new EntityCacheEntry(Country::class, ['id' => 1, 'name' => 'Foo']);
$property = new ReflectionProperty($persister, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('put')
->with(self::equalTo($key), self::equalTo($entry));
Expand All @@ -116,8 +110,6 @@ public function testDeleteTransactionCommitShouldEvictCache(): void
$key = new EntityCacheKey(Country::class, ['id' => 1]);
$property = new ReflectionProperty($persister, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('evict')
->with(self::equalTo($key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ public function testTransactionRollBackShouldClearQueue(): void
$key = new EntityCacheKey(Country::class, ['id' => 1]);
$property = new ReflectionProperty(ReadWriteCachedEntityPersister::class, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::exactly(2))
->method('lock')
->with(self::equalTo($key))
Expand Down Expand Up @@ -148,8 +146,6 @@ public function testTransactionCommitShouldClearQueue(): void
$key = new EntityCacheKey(Country::class, ['id' => 1]);
$property = new ReflectionProperty(ReadWriteCachedEntityPersister::class, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::exactly(2))
->method('lock')
->with(self::equalTo($key))
Expand Down Expand Up @@ -178,8 +174,6 @@ public function testDeleteLockFailureShouldIgnoreQueue(): void
$key = new EntityCacheKey(Country::class, ['id' => 1]);
$property = new ReflectionProperty(ReadWriteCachedEntityPersister::class, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('lock')
->with(self::equalTo($key))
Expand All @@ -202,8 +196,6 @@ public function testUpdateLockFailureShouldIgnoreQueue(): void
$key = new EntityCacheKey(Country::class, ['id' => 1]);
$property = new ReflectionProperty(ReadWriteCachedEntityPersister::class, 'queuedCache');

$property->setAccessible(true);

$this->region->expects(self::once())
->method('lock')
->with(self::equalTo($key))
Expand Down
2 changes: 0 additions & 2 deletions tests/Doctrine/Tests/ORM/Functional/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,6 @@ public function testCountQueryStripsParametersInSelect(): void

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

$getCountQuery->setAccessible(true);

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

Expand Down
1 change: 0 additions & 1 deletion tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ public function testSQLFilterGetConnection(): void
$filter = new MyLocaleFilter($em);

$reflMethod = new ReflectionMethod(SQLFilter::class, 'getConnection');
$reflMethod->setAccessible(true);

self::assertSame($conn, $reflMethod->invoke($filter));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,6 @@ public function testQueryCacheLifetime(): void

$getHash = static function (AbstractQuery $query) {
$method = new ReflectionMethod($query, 'getHash');
$method->setAccessible(true);

return $method->invoke($query);
};
Expand Down
1 change: 0 additions & 1 deletion tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3123Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function __construct(UnitOfWork $uow)
public function postFlush(): void
{
$property = new ReflectionProperty(UnitOfWork::class, 'extraUpdates');
$property->setAccessible(true);

Assert::assertEmpty(
$property->getValue($this->uow),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ public function testAcceptsEntityManagerInterfaceInstances(): void
// not really the cleanest way to check it, but we won't add a getter to the CMF just for the sake of testing.
$class = new ReflectionClass(ClassMetadataFactory::class);
$property = $class->getProperty('em');
$property->setAccessible(true);
self::assertSame($entityManager, $property->getValue($classMetadataFactory));
}

Expand Down
33 changes: 12 additions & 21 deletions tests/Doctrine/Tests/ORM/Mapping/ReflectionEmbeddedPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,46 +77,37 @@ public function getTestedReflectionProperties(): array
{
return [
[
$this->getReflectionProperty(BooleanModel::class, 'id'),
$this->getReflectionProperty(BooleanModel::class, 'id'),
new ReflectionProperty(BooleanModel::class, 'id'),
new ReflectionProperty(BooleanModel::class, 'id'),
BooleanModel::class,
],
// reflection on embeddables that have properties defined in abstract ancestors:
[
$this->getReflectionProperty(BooleanModel::class, 'id'),
$this->getReflectionProperty(AbstractEmbeddable::class, 'propertyInAbstractClass'),
new ReflectionProperty(BooleanModel::class, 'id'),
new ReflectionProperty(AbstractEmbeddable::class, 'propertyInAbstractClass'),
ConcreteEmbeddable::class,
],
[
$this->getReflectionProperty(BooleanModel::class, 'id'),
$this->getReflectionProperty(ConcreteEmbeddable::class, 'propertyInConcreteClass'),
new ReflectionProperty(BooleanModel::class, 'id'),
new ReflectionProperty(ConcreteEmbeddable::class, 'propertyInConcreteClass'),
ConcreteEmbeddable::class,
],
// reflection on classes extending internal PHP classes:
[
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'privateProperty'),
new ReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
new ReflectionProperty(ArrayObjectExtendingClass::class, 'privateProperty'),
ArrayObjectExtendingClass::class,
],
[
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'protectedProperty'),
new ReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
new ReflectionProperty(ArrayObjectExtendingClass::class, 'protectedProperty'),
ArrayObjectExtendingClass::class,
],
[
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
new ReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
new ReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
ArrayObjectExtendingClass::class,
],
];
}

private function getReflectionProperty(string $className, string $propertyName): ReflectionProperty
{
$reflectionProperty = new ReflectionProperty($className, $propertyName);

$reflectionProperty->setAccessible(true);

return $reflectionProperty;
}
}
1 change: 0 additions & 1 deletion tests/Doctrine/Tests/ORM/ORMSetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function testCacheNamespaceShouldBeGeneratedForApcu(): void
$cache = $config->getMetadataCache();

$namespaceProperty = new ReflectionProperty(AbstractAdapter::class, 'namespace');
$namespaceProperty->setAccessible(true);

self::assertInstanceOf(ApcuAdapter::class, $cache);
self::assertSame('dc2_1effb2475fcfba4f9e8b8a1dbc8f3caf:', $namespaceProperty->getValue($cache));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ protected function setUp(): void
public function testGetInsertSQLUsesTypeValuesSQL(): void
{
$method = new ReflectionMethod($this->persister, 'getInsertSQL');
$method->setAccessible(true);

$sql = $method->invoke($this->persister);
$sql = $method->invoke($this->persister);

self::assertEquals('INSERT INTO customtype_parents (customInteger, child_id) VALUES (ABS(?), ?)', $sql);
}
Expand Down Expand Up @@ -101,9 +99,7 @@ public function testUpdateUsesTypeValuesSQL(): void
public function testGetSelectConditionSQLUsesTypeValuesSQL(): void
{
$method = new ReflectionMethod($this->persister, 'getSelectConditionSQL');
$method->setAccessible(true);

$sql = $method->invoke($this->persister, ['customInteger' => 1, 'child' => 1]);
$sql = $method->invoke($this->persister, ['customInteger' => 1, 'child' => 1]);

self::assertEquals('t0.customInteger = ABS(?) AND t0.child_id = ?', $sql);
}
Expand All @@ -113,7 +109,6 @@ public function testStripNonAlphanumericCharactersFromSelectColumnListSQL(): voi
{
$persister = new BasicEntityPersister($this->entityManager, $this->entityManager->getClassMetadata(NonAlphaColumnsEntity::class));
$method = new ReflectionMethod($persister, 'getSelectColumnsSQL');
$method->setAccessible(true);

self::assertEquals('t0."simple-entity-id" AS simpleentityid_1, t0."simple-entity-value" AS simpleentityvalue_2', $method->invoke($persister));
}
Expand Down
2 changes: 0 additions & 2 deletions tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ public function testProxyClonesParentFields(): void

// Set the id of the CompanyEmployee (which is in the parent CompanyPerson)
$property = new ReflectionProperty(CompanyPerson::class, 'id');

$property->setAccessible(true);
$property->setValue($companyEmployee, 42);

$classMetaData = $this->emMock->getClassMetadata(CompanyEmployee::class);
Expand Down

0 comments on commit 9d8cadf

Please sign in to comment.