Skip to content

Commit 2eae4a5

Browse files
committed
Discover repository class from #[Entity] attribute on PHP 8
By doing it like this, we don't need to instantiate the object manager at all.
1 parent 04cace0 commit 2eae4a5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Type/Doctrine/ObjectMetadataResolver.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace PHPStan\Type\Doctrine;
44

5+
use Doctrine\ORM\Mapping\Entity;
56
use Doctrine\Persistence\ObjectManager;
67
use PHPStan\Reflection\ReflectionProvider;
8+
use ReflectionClass;
79
use function is_file;
810
use function is_readable;
911

@@ -97,6 +99,19 @@ public function getResolvedRepositoryClass(): string
9799

98100
public function getRepositoryClass(string $className): string
99101
{
102+
if (PHP_MAJOR_VERSION >= 8 && $this->reflectionProvider->hasClass($className)) {
103+
$classReflection = $this->reflectionProvider->getClass($className)->getNativeReflection();
104+
if (method_exists($classReflection, 'getAttributes')) {
105+
$attribute = $classReflection->getAttributes(Entity::class)[0] ?? null;
106+
if ($attribute !== null) {
107+
$attributeInstance = $attribute->newInstance();
108+
if ($attributeInstance->repositoryClass !== null) {
109+
return $attributeInstance->repositoryClass;
110+
}
111+
}
112+
}
113+
}
114+
100115
$objectManager = $this->getObjectManager();
101116
if ($objectManager === null) {
102117
return $this->getResolvedRepositoryClass();

0 commit comments

Comments
 (0)