2022-11-23 11:11:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2024-01-02 02:40:38 +00:00
|
|
|
namespace Rector\Console\Command;
|
2022-11-23 11:11:18 +00:00
|
|
|
|
2024-04-01 16:51:34 +00:00
|
|
|
use RectorPrefix202404\Nette\Utils\Json;
|
2022-11-23 11:11:18 +00:00
|
|
|
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Configuration\Option;
|
|
|
|
use Rector\Contract\Rector\RectorInterface;
|
2022-11-23 11:11:18 +00:00
|
|
|
use Rector\PostRector\Contract\Rector\PostRectorInterface;
|
2022-12-06 13:36:55 +00:00
|
|
|
use Rector\Skipper\SkipCriteriaResolver\SkippedClassResolver;
|
2024-04-01 16:51:34 +00:00
|
|
|
use RectorPrefix202404\Symfony\Component\Console\Command\Command;
|
|
|
|
use RectorPrefix202404\Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use RectorPrefix202404\Symfony\Component\Console\Input\InputOption;
|
|
|
|
use RectorPrefix202404\Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use RectorPrefix202404\Symfony\Component\Console\Style\SymfonyStyle;
|
2022-11-23 11:11:18 +00:00
|
|
|
final class ListRulesCommand extends Command
|
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2023-08-07 21:49:15 +00:00
|
|
|
* @var \Symfony\Component\Console\Style\SymfonyStyle
|
2022-11-23 11:11:18 +00:00
|
|
|
*/
|
2023-08-07 21:49:15 +00:00
|
|
|
private $symfonyStyle;
|
2022-12-06 13:36:55 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-12-06 13:36:55 +00:00
|
|
|
* @var \Rector\Skipper\SkipCriteriaResolver\SkippedClassResolver
|
|
|
|
*/
|
|
|
|
private $skippedClassResolver;
|
2022-11-23 11:11:18 +00:00
|
|
|
/**
|
|
|
|
* @var RectorInterface[]
|
2023-08-15 23:30:13 +00:00
|
|
|
* @readonly
|
2022-11-23 11:11:18 +00:00
|
|
|
*/
|
2023-08-15 23:30:13 +00:00
|
|
|
private $rectors;
|
2022-11-23 11:11:18 +00:00
|
|
|
/**
|
2023-08-15 23:30:13 +00:00
|
|
|
* @param RectorInterface[] $rectors
|
2022-11-23 11:11:18 +00:00
|
|
|
*/
|
2023-08-15 23:30:13 +00:00
|
|
|
public function __construct(SymfonyStyle $symfonyStyle, SkippedClassResolver $skippedClassResolver, array $rectors)
|
2022-11-23 11:11:18 +00:00
|
|
|
{
|
2023-08-07 21:49:15 +00:00
|
|
|
$this->symfonyStyle = $symfonyStyle;
|
2022-12-06 13:36:55 +00:00
|
|
|
$this->skippedClassResolver = $skippedClassResolver;
|
2023-08-07 18:15:37 +00:00
|
|
|
$this->rectors = $rectors;
|
2023-08-15 23:30:13 +00:00
|
|
|
parent::__construct();
|
2022-11-23 11:11:18 +00:00
|
|
|
}
|
|
|
|
protected function configure() : void
|
|
|
|
{
|
|
|
|
$this->setName('list-rules');
|
|
|
|
$this->setDescription('Show loaded Rectors');
|
2024-02-05 08:01:59 +00:00
|
|
|
$this->setAliases(['show-rules']);
|
2022-11-23 11:11:18 +00:00
|
|
|
$this->addOption(Option::OUTPUT_FORMAT, null, InputOption::VALUE_REQUIRED, 'Select output format', ConsoleOutputFormatter::NAME);
|
|
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : int
|
|
|
|
{
|
|
|
|
$rectorClasses = $this->resolveRectorClasses();
|
2022-12-06 13:36:55 +00:00
|
|
|
$skippedClasses = $this->getSkippedCheckers();
|
2022-11-23 11:11:18 +00:00
|
|
|
$outputFormat = $input->getOption(Option::OUTPUT_FORMAT);
|
|
|
|
if ($outputFormat === 'json') {
|
2022-12-06 13:36:55 +00:00
|
|
|
$data = ['rectors' => $rectorClasses, 'skipped-rectors' => $skippedClasses];
|
2024-04-09 20:37:51 +00:00
|
|
|
echo Json::encode($data, \true) . \PHP_EOL;
|
2022-11-23 11:11:18 +00:00
|
|
|
return Command::SUCCESS;
|
|
|
|
}
|
2023-08-07 21:49:15 +00:00
|
|
|
$this->symfonyStyle->title('Loaded Rector rules');
|
|
|
|
$this->symfonyStyle->listing($rectorClasses);
|
2022-12-06 13:36:55 +00:00
|
|
|
if ($skippedClasses !== []) {
|
2023-08-07 21:49:15 +00:00
|
|
|
$this->symfonyStyle->title('Skipped Rector rules');
|
|
|
|
$this->symfonyStyle->listing($skippedClasses);
|
2022-12-06 13:36:55 +00:00
|
|
|
}
|
2023-08-24 17:27:26 +00:00
|
|
|
$this->symfonyStyle->newLine();
|
|
|
|
$this->symfonyStyle->note(\sprintf('Loaded %d rules', \count($rectorClasses)));
|
2022-11-23 11:11:18 +00:00
|
|
|
return Command::SUCCESS;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<RectorInterface>>
|
|
|
|
*/
|
|
|
|
private function resolveRectorClasses() : array
|
|
|
|
{
|
|
|
|
$customRectors = \array_filter($this->rectors, static function (RectorInterface $rector) : bool {
|
2023-08-10 12:02:49 +00:00
|
|
|
return !$rector instanceof PostRectorInterface;
|
2022-11-23 11:11:18 +00:00
|
|
|
});
|
|
|
|
$rectorClasses = \array_map(static function (RectorInterface $rector) : string {
|
|
|
|
return \get_class($rector);
|
|
|
|
}, $customRectors);
|
|
|
|
\sort($rectorClasses);
|
2023-08-24 17:27:26 +00:00
|
|
|
return \array_unique($rectorClasses);
|
2022-11-23 11:11:18 +00:00
|
|
|
}
|
2022-12-06 13:36:55 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function getSkippedCheckers() : array
|
|
|
|
{
|
|
|
|
$skippedCheckers = [];
|
|
|
|
foreach ($this->skippedClassResolver->resolve() as $checkerClass => $fileList) {
|
|
|
|
// ignore specific skips
|
|
|
|
if ($fileList !== null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$skippedCheckers[] = $checkerClass;
|
|
|
|
}
|
|
|
|
return $skippedCheckers;
|
|
|
|
}
|
2022-11-23 11:11:18 +00:00
|
|
|
}
|