mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 19:53:14 +01:00
add RectorClassValidator
This commit is contained in:
parent
f528bcab24
commit
30f0569616
45
src/Validator/RectorClassValidator.php
Normal file
45
src/Validator/RectorClassValidator.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Validator;
|
||||
|
||||
use Rector\Contract\Rector\RectorInterface;
|
||||
use Rector\Exception\Validator\InvalidRectorClassException;
|
||||
|
||||
final class RectorClassValidator
|
||||
{
|
||||
/**
|
||||
* @param string[] $rectors
|
||||
*/
|
||||
public function validate(array $rectors): void
|
||||
{
|
||||
foreach ($rectors as $rector) {
|
||||
$this->ensureClassExists($rector);
|
||||
$this->ensureIsRector($rector);
|
||||
}
|
||||
}
|
||||
|
||||
private function ensureClassExists(string $rector): void
|
||||
{
|
||||
if (class_exists($rector)) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new InvalidRectorClassException(sprintf(
|
||||
'Rector "%s" was not found. Make sure class exists and is autoloaded.',
|
||||
$rector
|
||||
));
|
||||
}
|
||||
|
||||
private function ensureIsRector(string $rector): void
|
||||
{
|
||||
if (is_a($rector, RectorInterface::class, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new InvalidRectorClassException(sprintf(
|
||||
'Rector "%s" is not supported. Use class that implements %s.',
|
||||
$rector,
|
||||
RectorInterface::class
|
||||
));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user