Skip to content

Commit 7c11cd7

Browse files
committed
Fix error with nonexistent Doctrine\ORM\Mapping\ClassMetadataFactory
1 parent 2eb2e0c commit 7c11cd7

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/Type/Doctrine/ObjectMetadataResolver.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Doctrine\Persistence\ObjectManager;
1111
use PHPStan\ShouldNotHappenException;
1212
use ReflectionException;
13+
use function class_exists;
1314
use function is_file;
1415
use function is_readable;
1516
use function sprintf;
@@ -69,7 +70,12 @@ public function isTransient(string $className): bool
6970

7071
try {
7172
if ($objectManager === null) {
72-
return $this->getMetadataFactory()->isTransient($className);
73+
$metadataFactory = $this->getMetadataFactory();
74+
if ($metadataFactory === null) {
75+
return true;
76+
}
77+
78+
return $metadataFactory->isTransient($className);
7379
}
7480

7581
return $objectManager->getMetadataFactory()->isTransient($className);
@@ -81,12 +87,16 @@ public function isTransient(string $className): bool
8187
/**
8288
* @return ClassMetadataFactory<ClassMetadata>
8389
*/
84-
private function getMetadataFactory(): ClassMetadataFactory
90+
private function getMetadataFactory(): ?ClassMetadataFactory
8591
{
8692
if ($this->metadataFactory !== null) {
8793
return $this->metadataFactory;
8894
}
8995

96+
if (!class_exists(\Doctrine\ORM\Mapping\ClassMetadataFactory::class)) {
97+
return null;
98+
}
99+
90100
$metadataFactory = new \PHPStan\Doctrine\Mapping\ClassMetadataFactory();
91101

92102
return $this->metadataFactory = $metadataFactory;
@@ -107,7 +117,12 @@ public function getClassMetadata(string $className): ?ClassMetadataInfo
107117

108118
try {
109119
if ($objectManager === null) {
110-
$metadata = $this->getMetadataFactory()->getMetadataFor($className);
120+
$metadataFactory = $this->getMetadataFactory();
121+
if ($metadataFactory === null) {
122+
return null;
123+
}
124+
125+
$metadata = $metadataFactory->getMetadataFor($className);
111126
} else {
112127
$metadata = $objectManager->getClassMetadata($className);
113128
}

0 commit comments

Comments
 (0)