44
55namespace Rector \Monitor \Analyze \Command ;
66
7+ use Entropy \Console \Contract \CommandInterface ;
8+ use Entropy \Console \Enum \ExitCode ;
9+ use Entropy \Console \Output \OutputPrinter ;
710use Rector \Monitor \Analyze \Contract \RuleProcessorInterface ;
811use Rector \Monitor \Analyze \Reporting \ErrorCollector ;
9- use Rector \Monitor \Analyze \RuleProcessor \DisallowedPackagesRuleProcessor ;
10- use Rector \Monitor \Analyze \RuleProcessor \MetafileProcessor \NoPHPStanBaselineMetafileProcessor ;
11- use Rector \Monitor \Analyze \RuleProcessor \MissingPackagesRuleProcessor ;
12- use Rector \Monitor \Analyze \RuleProcessor \TooLowPackagesRulesProcessor ;
1312use Rector \Monitor \Config \ConfigInitializer ;
1413use Rector \Monitor \Config \MonitorConfigProvider ;
1514use Rector \Monitor \Git \RepositoryMetafilesResolver ;
16- use Rector \Monitor \ValueObject \RepositoryCollection ;
17- use Symfony \Component \Console \Command \Command ;
18- use Symfony \Component \Console \Input \InputInterface ;
19- use Symfony \Component \Console \Input \InputOption ;
20- use Symfony \Component \Console \Output \OutputInterface ;
21- use Symfony \Component \Console \Style \SymfonyStyle ;
22-
23- final class AnalyzeCommand extends Command
15+ use Webmozart \Assert \Assert ;
16+
17+ final readonly class AnalyzeCommand implements CommandInterface
2418{
2519 /**
26- * @var RuleProcessorInterface[]
20+ * @param RuleProcessorInterface[] $ruleProcessors
2721 */
28- private array $ ruleProcessors ;
29-
3022 public function __construct (
31- private readonly SymfonyStyle $ symfonyStyle ,
32- private readonly MonitorConfigProvider $ monitorConfigProvider ,
33- private readonly RepositoryMetafilesResolver $ repositoryMetafilesResolver ,
34- private readonly ConfigInitializer $ configInitializer ,
35- DisallowedPackagesRuleProcessor $ disallowedPackagesRuleProcessor ,
36- MissingPackagesRuleProcessor $ missingPackagesRuleProcessor ,
37- TooLowPackagesRulesProcessor $ tooLowPackagesRulesProcessor ,
38- NoPHPStanBaselineMetafileProcessor $ noPHPStanBaselineMetafileProcessor
23+ private OutputPrinter $ outputPrinter ,
24+ private MonitorConfigProvider $ monitorConfigProvider ,
25+ private RepositoryMetafilesResolver $ repositoryMetafilesResolver ,
26+ private ConfigInitializer $ configInitializer ,
27+ private array $ ruleProcessors ,
3928 ) {
40- parent ::__construct ();
41-
42- $ this ->ruleProcessors [] = $ disallowedPackagesRuleProcessor ;
43- $ this ->ruleProcessors [] = $ missingPackagesRuleProcessor ;
44- $ this ->ruleProcessors [] = $ tooLowPackagesRulesProcessor ;
45- $ this ->ruleProcessors [] = $ noPHPStanBaselineMetafileProcessor ;
29+ Assert::notEmpty ($ ruleProcessors );
4630 }
4731
48- protected function configure (): void
32+ public function getName (): string
4933 {
50- $ this ->setName ('analyze ' );
51- $ this ->setAliases (['analyse ' ]);
52-
53- $ this ->setDescription (
54- 'Check repositories composer.json files, assess "require" section, root files, tooling setup and min PHP version with defined quality control rules '
55- );
34+ return 'analyze ' ;
35+ }
5636
57- $ this ->addOption (
58- 'clear-cache ' ,
59- null ,
60- InputOption::VALUE_NONE ,
61- 'Remove cached repositoryCollection composer.json files '
62- );
37+ public function getDescription (): string
38+ {
39+ return 'Analyze multiple repositories for required packages, root files, min PHP version etc. against defined standard ' ;
6340 }
6441
65- protected function execute (InputInterface $ input , OutputInterface $ output ): int
42+ /**
43+ * @param bool $clearCache Remove cached repositoryCollection composer.json files
44+ * @return ExitCode::*
45+ */
46+ public function run (bool $ clearCache = false , bool $ debug = false ): int
6647 {
6748 if ($ this ->configInitializer ->createConfigIfMissing (getcwd ())) {
68- return self ::SUCCESS ;
49+ return ExitCode ::SUCCESS ;
6950 }
7051
7152 $ monitorConfig = $ this ->monitorConfigProvider ->provide ();
72- $ shouldClearCache = (bool ) $ input ->getOption ('clear-cache ' );
7353
7454 $ this ->repositoryMetafilesResolver ->decorateRepositories (
7555 $ monitorConfig ->getRepositoryCollection (),
76- $ shouldClearCache
56+ $ clearCache ,
57+ $ debug
7758 );
7859
79- $ this ->symfonyStyle ->title ('Repository analysis report ' );
60+ $ this ->outputPrinter ->title ('Repository analysis report ' );
8061
8162 $ erroredRepositoryCount = 0 ;
8263
@@ -85,42 +66,45 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8566
8667 $ composerJson = $ repository ->getComposerJson ();
8768
88- $ this ->symfonyStyle ->writeln (
89- sprintf ('<fg=yellow;options=bold >%d) %s</> ' , $ key + 1 , $ composerJson ->getRepositoryName ())
69+ $ this ->outputPrinter ->writeln (
70+ sprintf ('<fg=yellow>%d) %s</> ' , $ key + 1 , $ composerJson ->getRepositoryName ())
9071 );
9172 foreach ($ this ->ruleProcessors as $ ruleProcessor ) {
9273 $ ruleProcessor ->process ($ monitorConfig , $ composerJson , $ errorCollector );
9374 }
9475
9576 foreach ($ errorCollector ->getErrorMessages () as $ errorMessage ) {
96- $ this ->symfonyStyle ->writeln ($ errorMessage );
77+ $ this ->outputPrinter ->writeln ($ errorMessage );
9778 }
9879
9980 if ($ errorCollector ->hasErrors () === false ) {
100- $ this ->symfonyStyle -> writeln ( ' * <fg=green> Package perfect ✔</> ' );
81+ $ this ->outputPrinter -> greenBackground ( ' Package perfect ✔ ' );
10182 } else {
10283 ++$ erroredRepositoryCount ;
10384 }
10485
105- $ this ->symfonyStyle ->newLine ();
86+ $ this ->outputPrinter ->newLine ();
10687 }
10788
108- $ this ->printAnalysisSummary ($ monitorConfig -> getRepositoryCollection (), $ erroredRepositoryCount );
89+ $ this ->printAnalysisSummary ($ erroredRepositoryCount );
10990
110- return self ::SUCCESS ;
91+ return ExitCode ::SUCCESS ;
11192 }
11293
113- private function printAnalysisSummary (RepositoryCollection $ repositoryCollection , int $ erroredRepositoryCount ): void
94+ private function printAnalysisSummary (int $ erroredRepositoryCount ): void
11495 {
115- $ this ->symfonyStyle ->newLine ();
116-
117- $ this ->symfonyStyle ->writeln (sprintf (
118- ' Summary: %d %s analyzed, %d with issues ' ,
119- $ repositoryCollection ->count (),
120- $ repositoryCollection ->count () === 1 ? 'repository ' : 'repositories ' ,
121- $ erroredRepositoryCount
122- ));
96+ $ this ->outputPrinter ->newLine ();
97+
98+ if ($ erroredRepositoryCount > 0 ) {
99+ $ this ->outputPrinter ->redBackground (sprintf (
100+ 'Found %d %s ' ,
101+ $ erroredRepositoryCount ,
102+ $ erroredRepositoryCount === 1 ? 'issue ' : 'issues '
103+ ));
104+ } else {
105+ $ this ->outputPrinter ->greenBackground ('All repositories are perfect ✔ ' );
106+ }
123107
124- $ this ->symfonyStyle ->newLine ();
108+ $ this ->outputPrinter ->newLine ();
125109 }
126110}
0 commit comments