Skip to content

Commit 008f324

Browse files
committed
Nicer error when StubValidator throws an internal exception
1 parent 609a0dd commit 008f324

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/Command/AnalyseApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function analyse(
6969
if ($projectConfigArray !== null) {
7070
$projectStubFiles = $projectConfigArray['parameters']['stubFiles'] ?? [];
7171
}
72-
$stubErrors = $this->stubValidator->validate($projectStubFiles);
72+
$stubErrors = $this->stubValidator->validate($projectStubFiles, $debug);
7373

7474
register_shutdown_function(function (): void {
7575
$error = error_get_last();

src/PhpDoc/StubValidator.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\PhpDoc;
44

5+
use PHPStan\Analyser\Error;
56
use PHPStan\Analyser\FileAnalyser;
67
use PHPStan\Analyser\NodeScopeResolver;
78
use PHPStan\Broker\Broker;
@@ -60,7 +61,7 @@ public function __construct(
6061
* @param string[] $stubFiles
6162
* @return \PHPStan\Analyser\Error[]
6263
*/
63-
public function validate(array $stubFiles): array
64+
public function validate(array $stubFiles, bool $debug): array
6465
{
6566
if (count($stubFiles) === 0) {
6667
return [];
@@ -84,15 +85,24 @@ public function validate(array $stubFiles): array
8485

8586
$errors = [];
8687
foreach ($stubFiles as $stubFile) {
87-
$tmpErrors = $fileAnalyser->analyseFile(
88-
$stubFile,
89-
$analysedFiles,
90-
$ruleRegistry,
91-
static function (): void {
88+
try {
89+
$tmpErrors = $fileAnalyser->analyseFile(
90+
$stubFile,
91+
$analysedFiles,
92+
$ruleRegistry,
93+
static function (): void {
94+
}
95+
)->getErrors();
96+
foreach ($tmpErrors as $tmpError) {
97+
$errors[] = $tmpError->withoutTip();
9298
}
93-
)->getErrors();
94-
foreach ($tmpErrors as $tmpError) {
95-
$errors[] = $tmpError->withoutTip();
99+
} catch (\Throwable $e) {
100+
if ($debug) {
101+
throw $e;
102+
}
103+
104+
$internalErrorMessage = sprintf('Internal error: %s', $e->getMessage());
105+
$errors[] = new Error($internalErrorMessage, $stubFile, null, $e);
96106
}
97107
}
98108

0 commit comments

Comments
 (0)