Skip to content

Commit

Permalink
DX: Report more precise errors in `VerifyRectorServiceExistsCompilerP…
Browse files Browse the repository at this point in the history
…ass` (#97)
  • Loading branch information
staabm committed Jun 14, 2021
1 parent 01a0660 commit 64cea0a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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#'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
);
}
}
Expand Down

0 comments on commit 64cea0a

Please sign in to comment.