From 998fc08a3b92ca2ef9dbbcefb54b2c5bb9cf579d Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 26 Sep 2017 12:05:32 +0200 Subject: [PATCH] ensure rectors are loaded in ProcessCommand --- src/Console/Command/ProcessCommand.php | 24 +++++++++++++++++++++- src/Exception/NoRectorsLoadedException.php | 10 +++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/Exception/NoRectorsLoadedException.php diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index f68cff3ffac..447d371f8e7 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -4,7 +4,9 @@ namespace Rector\Console\Command; use Nette\Utils\Finder; use Rector\Application\FileProcessor; +use Rector\Exception\NoRectorsLoadedException; use Rector\Naming\CommandNaming; +use Rector\Rector\RectorCollector; use SplFileInfo; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -23,9 +25,15 @@ final class ProcessCommand extends Command */ private $fileProcessor; - public function __construct(FileProcessor $fileProcessor) + /** + * @var RectorCollector + */ + private $rectorCollector; + + public function __construct(FileProcessor $fileProcessor, RectorCollector $rectorCollector) { $this->fileProcessor = $fileProcessor; + $this->rectorCollector = $rectorCollector; parent::__construct(); } @@ -43,6 +51,8 @@ final class ProcessCommand extends Command protected function execute(InputInterface $input, OutputInterface $output): int { + $this->ensureSomeRectorsAreRegistered(); + $source = $input->getArgument(self::ARGUMENT_SOURCE_NAME); $files = $this->findPhpFilesInDirectories($source); $this->fileProcessor->processFiles($files); @@ -61,4 +71,16 @@ final class ProcessCommand extends Command return iterator_to_array($finder->getIterator()); } + + private function ensureSomeRectorsAreRegistered(): void + { + if ($this->rectorCollector->getRectorCount() > 0) { + return; + } + + throw new NoRectorsLoadedException( + 'No rector were found. Registers them in rector.yml config to "rector:" ' + . 'section or load them via "--config .yml" CLI option.' + ); + } } diff --git a/src/Exception/NoRectorsLoadedException.php b/src/Exception/NoRectorsLoadedException.php new file mode 100644 index 00000000000..13511ae9f17 --- /dev/null +++ b/src/Exception/NoRectorsLoadedException.php @@ -0,0 +1,10 @@ +