ensure rectors are loaded in ProcessCommand

This commit is contained in:
TomasVotruba 2017-09-26 12:05:32 +02:00
parent 0d05e11bce
commit 998fc08a3b
2 changed files with 33 additions and 1 deletions

View File

@ -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 <file>.yml" CLI option.'
);
}
}

View File

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