Skip to content

Commit 038c7a5

Browse files
committed
Nicer error message about missing autowired parser service
1 parent fc2b308 commit 038c7a5

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

src/Command/CommandHelper.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,37 @@ public static function begin(
235235
} catch (\Nette\DI\InvalidConfigurationException | \Nette\Utils\AssertionException $e) {
236236
$errorOutput->writeLineFormatted('<error>Invalid configuration:</error>');
237237
$errorOutput->writeLineFormatted($e->getMessage());
238+
throw new \PHPStan\Command\InceptionNotSuccessfulException();
239+
} catch (\Nette\DI\ServiceCreationException $e) {
240+
$matches = Strings::match($e->getMessage(), '#Service of type (?<serviceType>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\\\\]*[a-zA-Z0-9_\x7f-\xff]): Service of type (?<parserServiceType>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\\\\]*[a-zA-Z0-9_\x7f-\xff]) needed by \$(?<parameterName>[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*) in (?<methodName>[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*)\(\)#');
241+
if ($matches === null) {
242+
throw $e;
243+
}
244+
245+
if ($matches['parserServiceType'] !== 'PHPStan\\Parser\\Parser') {
246+
throw $e;
247+
}
248+
249+
if ($matches['methodName'] !== '__construct') {
250+
throw $e;
251+
}
252+
253+
$errorOutput->writeLineFormatted('<error>Invalid configuration:</error>');
254+
$errorOutput->writeLineFormatted(sprintf("Service of type <fg=cyan>%s</> is no longer autowired.\n", $matches['parserServiceType']));
255+
$errorOutput->writeLineFormatted('You need to choose one of the following services');
256+
$errorOutput->writeLineFormatted(sprintf('and use it in the %s argument of your service <fg=cyan>%s</>:', $matches['parameterName'], $matches['serviceType']));
257+
$errorOutput->writeLineFormatted('* <fg=cyan>defaultAnalysisParser</> (if you\'re parsing files from analysed paths)');
258+
$errorOutput->writeLineFormatted('* <fg=cyan>currentPhpVersionSimpleDirectParser</> (in most other situations)');
259+
260+
$errorOutput->writeLineFormatted('');
261+
$errorOutput->writeLineFormatted('After fixing this problem, your configuration will look something like this:');
262+
$errorOutput->writeLineFormatted('');
263+
$errorOutput->writeLineFormatted('-');
264+
$errorOutput->writeLineFormatted(sprintf("\tclass: %s", $matches['serviceType']));
265+
$errorOutput->writeLineFormatted(sprintf("\targuments:"));
266+
$errorOutput->writeLineFormatted(sprintf("\t\t%s: @defaultAnalysisParser", $matches['parameterName']));
267+
$errorOutput->writeLineFormatted('');
268+
238269
throw new \PHPStan\Command\InceptionNotSuccessfulException();
239270
}
240271

0 commit comments

Comments
 (0)