-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathListRulesCommand.php
More file actions
115 lines (92 loc) · 3.35 KB
/
ListRulesCommand.php
File metadata and controls
115 lines (92 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace Rector\Console\Command;
use Nette\Utils\Json;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Configuration\Option;
use Rector\Contract\Rector\RectorInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Rector\Skipper\SkipCriteriaResolver\SkippedClassResolver;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
final class ListRulesCommand extends Command
{
/**
* @param RectorInterface[] $rectors
*/
public function __construct(
private readonly SymfonyStyle $symfonyStyle,
private readonly SkippedClassResolver $skippedClassResolver,
private readonly array $rectors
) {
parent::__construct();
}
protected function configure(): void
{
$this->setName('list-rules');
$this->setDescription('Show loaded Rectors');
$this->setAliases(['show-rules']);
$this->addOption(
Option::OUTPUT_FORMAT,
null,
InputOption::VALUE_REQUIRED,
'Select output format',
ConsoleOutputFormatter::NAME
);
$this->addOption(Option::ONLY, null, InputOption::VALUE_REQUIRED, 'Fully qualified rule class name');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$rectorClasses = $this->resolveRectorClasses();
$skippedClasses = $this->getSkippedRectorClasses();
$outputFormat = $input->getOption(Option::OUTPUT_FORMAT);
if ($outputFormat === 'json') {
$data = [
'rectors' => $rectorClasses,
'skipped-rectors' => $skippedClasses,
];
echo Json::encode($data, pretty: true) . PHP_EOL;
return Command::SUCCESS;
}
$this->symfonyStyle->title('Loaded Rector rules');
$this->symfonyStyle->listing($rectorClasses);
if ($skippedClasses !== []) {
$this->symfonyStyle->title('Skipped Rector rules');
$this->symfonyStyle->listing($skippedClasses);
}
$this->symfonyStyle->newLine();
$this->symfonyStyle->note(sprintf('Loaded %d rules', count($rectorClasses)));
return Command::SUCCESS;
}
/**
* @return array<class-string<RectorInterface>>
*/
private function resolveRectorClasses(): array
{
$customRectors = array_filter(
$this->rectors,
static fn (RectorInterface $rector): bool => ! $rector instanceof PostRectorInterface
);
$rectorClasses = array_map(static fn (RectorInterface $rector): string => $rector::class, $customRectors);
sort($rectorClasses);
return array_unique($rectorClasses);
}
/**
* @return array<class-string>
*/
private function getSkippedRectorClasses(): array
{
$skippedRectorClasses = [];
foreach ($this->skippedClassResolver->resolve() as $rectorClass => $fileList) {
// ignore specific skips
if ($fileList !== null) {
continue;
}
$skippedRectorClasses[] = $rectorClass;
}
return $skippedRectorClasses;
}
}