Skip to content

Commit 868ae57

Browse files
committed
Ignore errors while loading ObjectManager
It's possible to configure a `objectManagerLoader` that returns an instance to your configured Doctrine manager. This works great, and it gives PHPStan super capabilities. In most cases, you would fetch this object manager from the container from your framework (e.g. Symfony). But that requires the Symfony container to be compiled and booted. All good, unless you make a typo in one of your service configurations. You're left with an internal error when running PHPStan: ``` <unknown location> Internal error: You have requested a non-existent service "some_service". while analysing file /Volumes/CS/www/src/SomeFile.php Run PHPStan with -v option and post the stack trace to: https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml Found 1 error ⚠️ Result is incomplete because of severe errors. ⚠️ Fix these errors first and then re-run PHPStan to get all reported errors. `` Making mistakes is a common thing we do, so having PHPStan crash like this is counter productive. Even worse, when using the PHPStan integration in PHPStorm, you get error popups every time that this happened. Since the `objectManagerLoader` is already optional, we can improve things by catching Throwable's that occur while including the `objectManagerLoader` file and then we return `null`. To make it easier to debug this problem later, in the diagnostic extension, we show the last error that occurred.
1 parent a1a9efb commit 868ae57

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Doctrine/DoctrineDiagnoseExtension.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,22 @@ public function __construct(
3030

3131
public function print(Output $output): void
3232
{
33+
$objectManager = $this->objectMetadataResolver->getObjectManager();
34+
3335
$output->writeLineFormatted(sprintf(
3436
'<info>Doctrine\'s objectManagerLoader:</info> %s',
3537
$this->objectMetadataResolver->hasObjectManagerLoader() ? 'In use' : 'No',
3638
));
3739

38-
$objectManager = $this->objectMetadataResolver->getObjectManager();
40+
if ($this->objectMetadataResolver->getLastError() !== null) {
41+
$output->writeLineFormatted(sprintf(
42+
'<error>Doctrine\'s objectManagerLoader error:</error> %s',
43+
$this->objectMetadataResolver->getLastError()->getMessage(),
44+
));
45+
46+
$output->writeLineFormatted('');
47+
}
48+
3949
if ($objectManager instanceof EntityManagerInterface) {
4050
$connection = $objectManager->getConnection();
4151
$driver = $this->driverDetector->detect($connection);

src/Type/Doctrine/ObjectMetadataResolver.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Doctrine\Mapping\ClassMetadataFactory;
1010
use PHPStan\ShouldNotHappenException;
1111
use ReflectionException;
12+
use Throwable;
1213
use function class_exists;
1314
use function is_file;
1415
use function is_readable;
@@ -26,6 +27,8 @@ final class ObjectMetadataResolver
2627

2728
private string $tmpDir;
2829

30+
private ?Throwable $lastError = null;
31+
2932
public function __construct(
3033
?string $objectManagerLoader,
3134
string $tmpDir
@@ -89,6 +92,11 @@ public function isTransient(string $className): bool
8992
}
9093
}
9194

95+
public function getLastError(): ?Throwable
96+
{
97+
return $this->lastError;
98+
}
99+
92100
private function getMetadataFactory(): ?ClassMetadataFactory
93101
{
94102
if ($this->metadataFactory !== null) {
@@ -160,7 +168,13 @@ private function loadObjectManager(string $objectManagerLoader): ?ObjectManager
160168
));
161169
}
162170

163-
return require $objectManagerLoader;
171+
try {
172+
return require $objectManagerLoader;
173+
} catch(Throwable $error) {
174+
$this->lastError = $error;
175+
176+
return null;
177+
}
164178
}
165179

166180
}

0 commit comments

Comments
 (0)