Lines 93.10% 27 / 29
Methods 91.66% 11 / 12
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 configure 100.00% 2 / 2 100.00% 1 / 1 1
 execute 80.00% 8 / 10 0.00% 0 / 1 4.13
 findGeneratedManifest 100.00% 1 / 1 100.00% 1 / 1 2
 writeReport 100.00% 2 / 2 100.00% 1 / 1 2
 [Stewart\Runtime\Console\StewartCommand] findConfigOption 100.00% 1 / 1 100.00% 1 / 1 1
 [Stewart\Runtime\Console\StewartCommand] readAppSelectionOption 100.00% 1 / 1 100.00% 1 / 1 1
 [Stewart\Runtime\Console\StewartCommand] findAppIdsOption 100.00% 4 / 4 100.00% 1 / 1 4
 [Stewart\Runtime\Console\StewartCommand] nonEmptyStringOption 100.00% 2 / 2 100.00% 1 / 1 3
 [Stewart\Runtime\Console\StewartCommand] writeMissingConfigFileNotice 100.00% 2 / 2 100.00% 1 / 1 2
 [Stewart\Runtime\Console\StewartCommand] writeNotice 100.00% 1 / 1 100.00% 1 / 1 2
 [Stewart\Runtime\Console\StewartCommand] readStringArgument 100.00% 2 / 2 100.00% 1 / 1 2
22final class ReloadCheckCommand extends StewartCommand
23{
24    public const string NAME = 'reload:check';
25
26    public function __construct(
27        private readonly ConfigLoader $config,
28        private readonly ProjectCheck $projects,
29        private readonly ReloadCheckReportCodec $reports,
30    ) {
31        parent::__construct();
32    }
33
34    protected function configure(): void
35    {
36        parent::configure();
37        $this->addOption('only', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Check only this automation, even if disabled; repeat for more');
38    }
39
40    protected function execute(InputInterface $input, OutputInterface $output): int
41    {
42        $configFile = $this->findConfigOption($input);
43        $fingerprint = $this->config->fingerprintConfigFile($configFile);
44
45        try {
46            $result = $this->projects->checkProject($configFile, $this->readAppSelectionOption($input));
47        } catch (IdentifierException $e) {
48            return $this->writeReport($output, ReloadCheckReport::failed([$e->getMessage()], $fingerprint));
49        }
50
51        if (!$result->hasPassed()) {
52            return $this->writeReport($output, ReloadCheckReport::failed($result->errors, $fingerprint));
53        }
54
55        // A reload keeps the running listeners, but must not accept a config the next start refuses.
56        if ($result->httpListenerError !== null) {
57            return $this->writeReport($output, ReloadCheckReport::failed([$result->httpListenerError], $fingerprint));
58        }
59
60        return $this->writeReport($output, ReloadCheckReport::fromDiscovery($result->discovered, $fingerprint, $this->findGeneratedManifest($result)));
61    }
62
63    private function findGeneratedManifest(ProjectCheckResult $result): ?GeneratedManifest
64    {
65        return $result->codegen === null ? null : GeneratedManifest::findForGeneratedCode($result->codegen);
66    }
67
68    private function writeReport(OutputInterface $output, ReloadCheckReport $report): int
69    {
70        $output->writeln($this->reports->encodeReport($report), OutputInterface::OUTPUT_RAW);
71
72        return $report->findFirstError() === null ? Command::SUCCESS : Command::FAILURE;
73    }
74}

Inherited from Stewart\Runtime\Console\StewartCommand

24    protected function findConfigOption(InputInterface $input): ?string
25    {
26        return $this->nonEmptyStringOption($input, 'config');
27    }
30    protected function readAppSelectionOption(InputInterface $input): AppSelection
31    {
32        return new AppSelection($this->findAppIdsOption($input, 'only'), $this->findAppIdsOption($input, 'trace'));
33    }
36    private function findAppIdsOption(InputInterface $input, string $name): ?AppIdCollection
37    {
38        $ids = $input->hasOption($name) ? $input->getOption($name) : null;
39
40        if (!\is_array($ids) || $ids === []) {
41            return null;
42        }
43
44        return AppIdCollection::fromList(array_map(static fn(string $id): AppId => new AppId($id), array_values(array_filter($ids, \is_string(...)))));
45    }
47    protected function nonEmptyStringOption(InputInterface $input, string $name): ?string
48    {
49        $value = $input->getOption($name);
50
51        return \is_string($value) && $value !== '' ? $value : null;
52    }
54    protected function writeMissingConfigFileNotice(OutputInterface $output, ?string $missingConfigFile): void
55    {
56        if ($missingConfigFile !== null) {
57            $this->writeNotice($output, \sprintf('No config file at %s; using the environment only.', $missingConfigFile));
58        }
59    }
61    protected function writeNotice(OutputInterface $output, string $notice): void
62    {
63        ($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output)->writeln('<comment>' . $notice . '</comment>');
64    }
66    protected function readStringArgument(InputInterface $input, string $name): string
67    {
68        $value = $input->getArgument($name);
69
70        return \is_string($value) ? $value : '';
71    }