add RectorClassValidator

This commit is contained in:
TomasVotruba 2017-09-26 11:39:16 +02:00
parent f528bcab24
commit 30f0569616

View 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
));
}
}