add ActiveRectorsProvider, fix reporting 0 rules registered (#4106)

This commit is contained in:
Tomas Votruba 2020-09-02 14:54:11 +02:00 committed by GitHub
parent 99588fedff
commit 41121a94f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 77 additions and 87 deletions

View File

@ -3,12 +3,10 @@
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::SETS, [SetList::TYPE_DECLARATION]);
$parameters->set(Option::PATHS, [
__DIR__ . '/src',

View File

@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Application;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Rector\RectorGenerator\Contract\InternalRectorInterface;
/**
* Provides list of Rector rules, that are not internal only those registered by user
*/
final class ActiveRectorsProvider
{
/**
* @var RectorInterface[]
*/
private $rectors = [];
/**
* @param RectorInterface[] $rectors
*/
public function __construct(array $rectors)
{
$this->rectors = $rectors;
}
/**
* @return RectorInterface[]
*/
public function provide(): array
{
return $this->filterOutInternalRectorsAndSort($this->rectors);
}
/**
* @param RectorInterface[] $rectors
* @return RectorInterface[]
*/
private function filterOutInternalRectorsAndSort(array $rectors): array
{
sort($rectors);
return array_filter($rectors, function (RectorInterface $rector): bool {
// utils rules
if ($rector instanceof InternalRectorInterface) {
return false;
}
// skip as internal and always run
return ! $rector instanceof PostRectorInterface;
});
}
}

View File

@ -128,12 +128,11 @@ final class ProcessCommand extends AbstractCommand
$this->stubLoader = $stubLoader;
$this->nonPhpFileProcessor = $nonPhpFileProcessor;
$this->unchangedFilesFilter = $unchangedFilesFilter;
parent::__construct();
$this->changedFilesDetector = $changedFilesDetector;
$this->symfonyStyle = $symfonyStyle;
$this->eventDispatcher = $eventDispatcher;
parent::__construct();
}
protected function configure(): void
@ -211,7 +210,6 @@ final class ProcessCommand extends AbstractCommand
$this->configuration->setAreAnyPhpRectorsLoaded((bool) $this->rectorNodeTraverser->getPhpRectorCount());
$this->rectorGuard->ensureSomeRectorsAreRegistered();
$this->rectorGuard->ensureGetNodeTypesAreNodes();
$this->stubLoader->loadStubs();

View File

@ -4,11 +4,10 @@ declare(strict_types=1);
namespace Rector\Core\Console\Command;
use Rector\Core\Application\ActiveRectorsProvider;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\NeonYaml\YamlPrinter;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Rector\RectorGenerator\Contract\InternalRectorInterface;
use ReflectionClass;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@ -18,11 +17,6 @@ use Symplify\PackageBuilder\Console\ShellCode;
final class ShowCommand extends AbstractCommand
{
/**
* @var RectorInterface[]
*/
private $rectors = [];
/**
* @var SymfonyStyle
*/
@ -34,13 +28,18 @@ final class ShowCommand extends AbstractCommand
private $yamlPrinter;
/**
* @param RectorInterface[] $rectors
* @var ActiveRectorsProvider
*/
public function __construct(SymfonyStyle $symfonyStyle, array $rectors, YamlPrinter $yamlPrinter)
{
private $activeRectorsProvider;
public function __construct(
SymfonyStyle $symfonyStyle,
ActiveRectorsProvider $activeRectorsProvider,
YamlPrinter $yamlPrinter
) {
$this->symfonyStyle = $symfonyStyle;
$this->rectors = $rectors;
$this->yamlPrinter = $yamlPrinter;
$this->activeRectorsProvider = $activeRectorsProvider;
parent::__construct();
}
@ -53,38 +52,19 @@ final class ShowCommand extends AbstractCommand
protected function execute(InputInterface $input, OutputInterface $output): int
{
$rectors = $this->filterAndSortRectors($this->rectors);
$activeRectors = $this->activeRectorsProvider->provide();
foreach ($rectors as $rector) {
foreach ($activeRectors as $rector) {
$this->symfonyStyle->writeln(' * ' . get_class($rector));
$this->printConfiguration($rector);
}
$message = sprintf('%d loaded Rectors', count($rectors));
$message = sprintf('%d loaded Rectors', count($activeRectors));
$this->symfonyStyle->success($message);
return ShellCode::SUCCESS;
}
/**
* @param RectorInterface[] $rectors
* @return RectorInterface[]
*/
private function filterAndSortRectors(array $rectors): array
{
sort($rectors);
return array_filter($rectors, function (RectorInterface $rector): bool {
// utils rules
if ($rector instanceof InternalRectorInterface) {
return false;
}
// skip as internal and always run
return ! $rector instanceof PostRectorInterface;
});
}
private function printConfiguration(RectorInterface $rector): void
{
$configuration = $this->resolveConfiguration($rector);

View File

@ -4,39 +4,24 @@ declare(strict_types=1);
namespace Rector\Core\Guard;
use PhpParser\Node;
use Rector\Core\Application\ActiveRectorsProvider;
use Rector\Core\Exception\NoRectorsLoadedException;
use Rector\Core\Exception\Rector\InvalidRectorException;
use Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser;
use Rector\FileSystemRector\FileSystemFileProcessor;
final class RectorGuard
{
/**
* @var RectorNodeTraverser
* @var ActiveRectorsProvider
*/
private $rectorNodeTraverser;
private $activeRectorsProvider;
/**
* @var FileSystemFileProcessor
*/
private $fileSystemFileProcessor;
public function __construct(
FileSystemFileProcessor $fileSystemFileProcessor,
RectorNodeTraverser $rectorNodeTraverser
) {
$this->rectorNodeTraverser = $rectorNodeTraverser;
$this->fileSystemFileProcessor = $fileSystemFileProcessor;
public function __construct(ActiveRectorsProvider $activeRectorsProvider)
{
$this->activeRectorsProvider = $activeRectorsProvider;
}
public function ensureSomeRectorsAreRegistered(): void
{
if ($this->rectorNodeTraverser->getPhpRectorCount() > 0) {
return;
}
if ($this->fileSystemFileProcessor->getFileSystemRectorsCount() > 0) {
if ($this->activeRectorsProvider->provide() !== []) {
return;
}
@ -48,22 +33,4 @@ 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
));
}
}
}
}

View File

@ -46,14 +46,6 @@ final class RectorNodeTraverser extends NodeTraverser
}
}
/**
* @return PhpRectorInterface[]
*/
public function getAllPhpRectors(): array
{
return $this->allPhpRectors;
}
/**
* @param Node[] $nodes
* @return Node[]