From 64cea0afd63f580e0e990a0ca4d27f936133ff19 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 15 Jun 2021 01:08:04 +0200 Subject: [PATCH] DX: Report more precise errors in `VerifyRectorServiceExistsCompilerPass` (#97) --- phpstan.neon | 5 +++++ .../VerifyRectorServiceExistsCompilerPass.php | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index 155d0c33114..97a24bca34b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -212,6 +212,11 @@ parameters: message: '#Call to function is_string\(\) with float will always evaluate to false#' path: src/PhpParser/Printer/BetterStandardPrinter.php + # class_exists is forbidden to enforce static reflection, but in a compiler pass we want runtime autoloading + - + message: '#Function "class_exists\(\)" cannot be used/left in the code#' + path: src/DependencyInjection/CompilerPass/VerifyRectorServiceExistsCompilerPass.php + # known values from other methods - message: '#Negated boolean expression is always true#' diff --git a/src/DependencyInjection/CompilerPass/VerifyRectorServiceExistsCompilerPass.php b/src/DependencyInjection/CompilerPass/VerifyRectorServiceExistsCompilerPass.php index 20d20d1b92c..aa27f8546ec 100644 --- a/src/DependencyInjection/CompilerPass/VerifyRectorServiceExistsCompilerPass.php +++ b/src/DependencyInjection/CompilerPass/VerifyRectorServiceExistsCompilerPass.php @@ -6,6 +6,7 @@ use Rector\Core\Contract\Rector\RectorInterface; use Rector\Core\Exception\ShouldNotHappenException; +use Rector\Core\Rector\AbstractRector; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -23,9 +24,15 @@ public function process(ContainerBuilder $containerBuilder): void continue; } + if (!class_exists($class)) { + throw new ShouldNotHappenException( + sprintf('Rector rule "%s" not found, please verify that the class exists and is autoloadable.', $class) + ); + } + if (! is_a($class, RectorInterface::class, true)) { throw new ShouldNotHappenException( - sprintf('Rector rule "%s" not found, please verify that the rule exists', $class) + sprintf('Rector rule "%s" should extend "%s" or implement "%s".', $class, AbstractRector::class, RectorInterface::class) ); } }