Repository navigation
Expand file tree
/
Copy pathValidateConfigCommand.php
More file actions
125 lines (104 loc) · 4.49 KB
/
Copy pathValidateConfigCommand.php
File metadata and controls
125 lines (104 loc) · 4.49 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
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace Rector\Console\Command;
use Nette\Utils\Json;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\ChangesReporting\Output\JsonOutputFormatter;
use Rector\Configuration\Option;
use Rector\Console\ExitCode;
use Rector\Reporting\DeprecatedRulesReporter;
use Rector\Reporting\MissConfigurationReporter;
use Rector\Skipper\Skipper\Skipper;
use Rector\ValueObject\Configuration;
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;
/**
* @see \Rector\Tests\Console\Command\ValidateConfigCommandTest
*/
final class ValidateConfigCommand extends Command
{
public function __construct(
private readonly SymfonyStyle $symfonyStyle,
private readonly DeprecatedRulesReporter $deprecatedRulesReporter,
private readonly MissConfigurationReporter $missConfigurationReporter,
private readonly Skipper $skipper,
) {
parent::__construct();
}
protected function configure(): void
{
$this->setName('validate-config');
$this->setDescription('Report config hygiene issues without processing any files');
$this->addOption(
Option::OUTPUT_FORMAT,
null,
InputOption::VALUE_REQUIRED,
sprintf('Output format: "%s" or "%s"', ConsoleOutputFormatter::NAME, JsonOutputFormatter::NAME),
ConsoleOutputFormatter::NAME
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$isJsonOutput = $input->getOption(Option::OUTPUT_FORMAT) === JsonOutputFormatter::NAME;
// silence the human-readable warnings, so only the JSON payload lands on stdout
if ($isJsonOutput) {
$this->symfonyStyle->setVerbosity(OutputInterface::VERBOSITY_QUIET);
}
$issueCount = 0;
$issueCount += $this->deprecatedRulesReporter->reportDeprecatedRules();
$issueCount += $this->deprecatedRulesReporter->reportDeprecatedSkippedRules();
$issueCount += $this->deprecatedRulesReporter->reportDeprecatedAttributesSetsArgs();
$issueCount += $this->deprecatedRulesReporter->reportDeprecatedComposerBasedArgs();
$issueCount += $this->missConfigurationReporter->reportSkippedNeverRegisteredRules();
$issueCount += $this->missConfigurationReporter->reportSkippedNonRectorClasses();
$issueCount += $this->reportDeprecatedSkippedClasses();
$issueCount += $this->reportSetAndRulesDuplicatedRegistrations();
if ($isJsonOutput) {
echo Json::encode([
'valid' => $issueCount === 0,
'issue_count' => $issueCount,
], pretty: true) . PHP_EOL;
return $issueCount === 0 ? ExitCode::SUCCESS : ExitCode::FAILURE;
}
if ($issueCount === 0) {
$this->symfonyStyle->success('Config is valid, no issues found');
return ExitCode::SUCCESS;
}
$this->symfonyStyle->error(sprintf(
'%d config %s found, see the warnings above',
$issueCount,
$issueCount === 1 ? 'issue' : 'issues'
));
return ExitCode::FAILURE;
}
private function reportDeprecatedSkippedClasses(): int
{
$deprecatedSkippedClasses = $this->skipper->resolveDeprecatedSkippedClasses();
if ($deprecatedSkippedClasses === []) {
return 0;
}
$this->symfonyStyle->warning(sprintf(
'These rules are skipped, but are deprecated. Most likely you do not need to skip them anymore, remove them: %s%s',
"\n\n",
'* ' . implode("\n* ", $deprecatedSkippedClasses) . "\n"
));
return count($deprecatedSkippedClasses);
}
private function reportSetAndRulesDuplicatedRegistrations(): int
{
$setAndRulesDuplicatedRegistrations = new Configuration()
->getBothSetAndRulesDuplicatedRegistrations();
if ($setAndRulesDuplicatedRegistrations === []) {
return 0;
}
$this->symfonyStyle->warning(sprintf(
'These rules are registered in both sets and "withRules()". Remove them from "withRules()" to avoid duplications: %s* %s',
"\n\n",
implode(' * ', $setAndRulesDuplicatedRegistrations) . "\n"
));
return count($setAndRulesDuplicatedRegistrations);
}
}