mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
add ActiveRectorsProvider, fix reporting 0 rules registered (#4106)
This commit is contained in:
parent
99588fedff
commit
41121a94f0
@ -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',
|
||||
|
55
src/Application/ActiveRectorsProvider.php
Normal file
55
src/Application/ActiveRectorsProvider.php
Normal 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;
|
||||
});
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -46,14 +46,6 @@ final class RectorNodeTraverser extends NodeTraverser
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PhpRectorInterface[]
|
||||
*/
|
||||
public function getAllPhpRectors(): array
|
||||
{
|
||||
return $this->allPhpRectors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @return Node[]
|
||||
|
Loading…
x
Reference in New Issue
Block a user