Skip to content

Commit b7ff9db

Browse files
authored
Remove deprecated handling of custom ObjectRepository implementations (#9634)
1 parent 953c633 commit b7ff9db

File tree

6 files changed

+24
-110
lines changed

6 files changed

+24
-110
lines changed

UPGRADE.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
# Upgrade to 3.0
22

3-
## Removed: support for entity namespace alias
3+
## BC BREAK: Repository classes have to extend `EntityRepository`
4+
5+
Although undocumented, it was possible to configure a custom repository
6+
class that implements `ObjectRepository` but does not extend the
7+
`EntityRepository` base class. Repository classes have to extend
8+
`EntityRepository` now.
9+
10+
## BC BREAK: Removed support for entity namespace alias
411

512
- `EntityManager::getRepository()` no longer accepts the entity namespace alias
613
notation.
714
- `Configuration::addEntityNamespace()` and
815
`Configuration::getEntityNamespace()` have been removed.
916

10-
1117
## BC BREAK: Remove helper methods from `AbstractCollectionPersister`
1218

1319
The following protected methods of

lib/Doctrine/ORM/Configuration.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Doctrine\ORM;
66

77
use Doctrine\Common\Proxy\AbstractProxyFactory;
8-
use Doctrine\Deprecations\Deprecation;
98
use Doctrine\ORM\Cache\CacheConfiguration;
109
use Doctrine\ORM\Exception\InvalidEntityRepository;
1110
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
@@ -21,7 +20,6 @@
2120
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
2221
use Doctrine\ORM\Repository\RepositoryFactory;
2322
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
24-
use Doctrine\Persistence\ObjectRepository;
2523
use Psr\Cache\CacheItemPoolInterface;
2624

2725
use function class_exists;
@@ -456,20 +454,10 @@ public function getFilterClassName($name)
456454
*/
457455
public function setDefaultRepositoryClassName($className)
458456
{
459-
if (! class_exists($className) || ! is_a($className, ObjectRepository::class, true)) {
457+
if (! class_exists($className) || ! is_a($className, EntityRepository::class, true)) {
460458
throw InvalidEntityRepository::fromClassName($className);
461459
}
462460

463-
if (! is_a($className, EntityRepository::class, true)) {
464-
Deprecation::trigger(
465-
'doctrine/orm',
466-
'https://github.com/doctrine/orm/pull/9533',
467-
'Configuring %s as default repository class is deprecated because it does not extend %s.',
468-
$className,
469-
EntityRepository::class
470-
);
471-
}
472-
473461
$this->_attributes['defaultRepositoryClassName'] = $className;
474462
}
475463

lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Doctrine\ORM\Repository;
66

7-
use Doctrine\Deprecations\Deprecation;
87
use Doctrine\ORM\EntityManagerInterface;
98
use Doctrine\ORM\EntityRepository;
109
use Doctrine\Persistence\ObjectRepository;
@@ -20,22 +19,15 @@ final class DefaultRepositoryFactory implements RepositoryFactory
2019
* The list of EntityRepository instances.
2120
*
2221
* @var ObjectRepository[]
23-
* @psalm-var array<string, ObjectRepository>
22+
* @psalm-var array<string, EntityRepository>
2423
*/
25-
private $repositoryList = [];
24+
private array $repositoryList = [];
2625

27-
/**
28-
* {@inheritdoc}
29-
*/
30-
public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository
26+
public function getRepository(EntityManagerInterface $entityManager, string $entityName): EntityRepository
3127
{
3228
$repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_id($entityManager);
3329

34-
if (isset($this->repositoryList[$repositoryHash])) {
35-
return $this->repositoryList[$repositoryHash];
36-
}
37-
38-
return $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName);
30+
return $this->repositoryList[$repositoryHash] ??= $this->createRepository($entityManager, $entityName);
3931
}
4032

4133
/**
@@ -47,22 +39,11 @@ public function getRepository(EntityManagerInterface $entityManager, $entityName
4739
private function createRepository(
4840
EntityManagerInterface $entityManager,
4941
string $entityName
50-
): ObjectRepository {
42+
): EntityRepository {
5143
$metadata = $entityManager->getClassMetadata($entityName);
5244
$repositoryClassName = $metadata->customRepositoryClassName
5345
?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
5446

55-
$repository = new $repositoryClassName($entityManager, $metadata);
56-
if (! $repository instanceof EntityRepository) {
57-
Deprecation::trigger(
58-
'doctrine/orm',
59-
'https://github.com/doctrine/orm/pull/9533',
60-
'Configuring %s as repository class is deprecated because it does not extend %s.',
61-
$repositoryClassName,
62-
EntityRepository::class
63-
);
64-
}
65-
66-
return $repository;
47+
return new $repositoryClassName($entityManager, $metadata);
6748
}
6849
}

lib/Doctrine/ORM/Repository/RepositoryFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Doctrine\ORM\Repository;
66

77
use Doctrine\ORM\EntityManagerInterface;
8-
use Doctrine\Persistence\ObjectRepository;
8+
use Doctrine\ORM\EntityRepository;
99

1010
/**
1111
* Interface for entity repository factory.
@@ -18,9 +18,9 @@ interface RepositoryFactory
1818
* @param EntityManagerInterface $entityManager The EntityManager instance.
1919
* @param class-string<T> $entityName The name of the entity.
2020
*
21-
* @return ObjectRepository<T> This type will change to {@see EntityRepository} in 3.0.
21+
* @return EntityRepository<T>
2222
*
2323
* @template T of object
2424
*/
25-
public function getRepository(EntityManagerInterface $entityManager, $entityName);
25+
public function getRepository(EntityManagerInterface $entityManager, string $entityName): EntityRepository;
2626
}

psalm-baseline.xml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,6 @@
205205
<code>ClassMetadataFactory</code>
206206
<code>Mapping\ClassMetadata</code>
207207
</InvalidReturnType>
208-
<LessSpecificReturnStatement occurrences="1">
209-
<code>$this-&gt;repositoryFactory-&gt;getRepository($this, $entityName)</code>
210-
</LessSpecificReturnStatement>
211-
<MoreSpecificReturnType occurrences="1">
212-
<code>EntityRepository&lt;T&gt;</code>
213-
</MoreSpecificReturnType>
214208
<ParamNameMismatch occurrences="6">
215209
<code>$entity</code>
216210
<code>$entity</code>
@@ -2324,9 +2318,12 @@
23242318
</PossiblyNullArgument>
23252319
</file>
23262320
<file src="lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php">
2327-
<TypeDoesNotContainType occurrences="1">
2328-
<code>$repository instanceof EntityRepository</code>
2329-
</TypeDoesNotContainType>
2321+
<InvalidReturnStatement occurrences="1">
2322+
<code>$this-&gt;repositoryList[$repositoryHash] ??= $this-&gt;createRepository($entityManager, $entityName)</code>
2323+
</InvalidReturnStatement>
2324+
<InvalidReturnType occurrences="1">
2325+
<code>EntityRepository</code>
2326+
</InvalidReturnType>
23302327
<UnsafeInstantiation occurrences="1">
23312328
<code>new $repositoryClassName($entityManager, $metadata)</code>
23322329
</UnsafeInstantiation>

tests/Doctrine/Tests/ORM/ConfigurationTest.php

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@
1313
use Doctrine\ORM\Mapping as AnnotationNamespace;
1414
use Doctrine\ORM\Mapping\EntityListenerResolver;
1515
use Doctrine\ORM\Mapping\NamingStrategy;
16-
use Doctrine\ORM\Mapping\PrePersist;
1716
use Doctrine\ORM\Mapping\QuoteStrategy;
1817
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
19-
use Doctrine\Persistence\ObjectRepository;
2018
use Doctrine\Tests\DoctrineTestCase;
2119
use Doctrine\Tests\Models\DDC753\DDC753CustomRepository;
2220
use Psr\Cache\CacheItemPoolInterface;
23-
use stdClass;
2421

2522
/**
2623
* Tests for the Configuration object
@@ -171,14 +168,6 @@ public function setDefaultRepositoryClassName(): void
171168
$this->configuration->setDefaultRepositoryClassName(self::class);
172169
}
173170

174-
public function testSetDeprecatedDefaultRepositoryClassName(): void
175-
{
176-
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/9533');
177-
178-
$this->configuration->setDefaultRepositoryClassName(DeprecatedRepository::class);
179-
self::assertSame(DeprecatedRepository::class, $this->configuration->getDefaultRepositoryClassName());
180-
}
181-
182171
public function testSetGetNamingStrategy(): void
183172
{
184173
self::assertInstanceOf(NamingStrategy::class, $this->configuration->getNamingStrategy());
@@ -219,50 +208,3 @@ public function testSetGetSecondLevelCacheConfig(): void
219208
self::assertEquals($mockClass, $this->configuration->getSecondLevelCacheConfiguration());
220209
}
221210
}
222-
223-
class ConfigurationTestAnnotationReaderChecker
224-
{
225-
/** @PrePersist */
226-
public function simpleAnnotationMethod(): void
227-
{
228-
}
229-
230-
/** @AnnotationNamespace\PrePersist */
231-
public function namespacedAnnotationMethod(): void
232-
{
233-
}
234-
}
235-
236-
class DeprecatedRepository implements ObjectRepository
237-
{
238-
/**
239-
* {@inheritdoc}
240-
*/
241-
public function find($id)
242-
{
243-
return null;
244-
}
245-
246-
public function findAll(): array
247-
{
248-
return [];
249-
}
250-
251-
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
252-
{
253-
return [];
254-
}
255-
256-
/**
257-
* {@inheritdoc}
258-
*/
259-
public function findOneBy(array $criteria)
260-
{
261-
return null;
262-
}
263-
264-
public function getClassName(): string
265-
{
266-
return stdClass::class;
267-
}
268-
}

0 commit comments

Comments
 (0)