[DX] Make sure provided node types are children of node

This commit is contained in:
TomasVotruba 2020-01-18 00:27:07 +01:00
parent f9f62aa311
commit 69adcb274c
3 changed files with 32 additions and 0 deletions

View File

@ -201,6 +201,7 @@ final class ProcessCommand extends AbstractCommand
$this->configuration->setAreAnyPhpRectorsLoaded((bool) $this->rectorNodeTraverser->getPhpRectorCount());
$this->rectorGuard->ensureSomeRectorsAreRegistered();
$this->rectorGuard->ensureGetNodeTypesAreNodes();
$this->stubLoader->loadStubs();
$source = $this->resolvesSourcePaths($input);

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Rector\Exception\Rector;
use Exception;
final class InvalidRectorException extends Exception
{
}

View File

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace Rector\Guard;
use PhpParser\Node;
use Rector\Exception\NoRectorsLoadedException;
use Rector\Exception\Rector\InvalidRectorException;
use Rector\FileSystemRector\FileSystemFileProcessor;
use Rector\PhpParser\NodeTraverser\RectorNodeTraverser;
@ -45,4 +47,22 @@ final class RectorGuard
PHP_EOL
));
}
public function ensureGetNodeTypesAreNodes(): void
{
foreach ($this->rectorNodeTraverser->getAllPhpRectors() as $phpRector) {
foreach ($phpRector->getNodeTypes() as $nodeTypeClass) {
if (is_a($nodeTypeClass, Node::class, true)) {
continue;
}
throw new InvalidRectorException(sprintf(
'Method "%s::getNodeTypes() provides invalid node class "%s". It must be child of "%s"',
get_class($phpRector),
$nodeTypeClass,
Node::class
));
}
}
}
}