Lines 100.00% 54 / 54
Methods 100.00% 15 / 15
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 execute 100.00% 17 / 17 100.00% 1 / 1 4
 writeHttpListenerNotice 100.00% 2 / 2 100.00% 1 / 1 2
 writeUnmarkedAppNotices 100.00% 2 / 2 100.00% 1 / 1 2
 writeUnusedTemplateNotices 100.00% 2 / 2 100.00% 1 / 1 2
 writeUnmatchedOverridesNotice 100.00% 6 / 6 100.00% 1 / 1 3
 writeAppTable 100.00% 10 / 10 100.00% 1 / 1 5
 [Stewart\Runtime\Console\StewartCommand] configure 100.00% 1 / 1 100.00% 1 / 1 1
 [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 CheckCommand extends StewartCommand
23{
24    public const string NAME = 'check';
25
26    public function __construct(
27        private readonly ConfigLoader $config,
28        private readonly ProjectCheck $projects,
29    ) {
30        parent::__construct();
31    }
32
33    protected function execute(InputInterface $input, OutputInterface $output): int
34    {
35        $configFile = $this->findConfigOption($input);
36        $result = $this->projects->checkProject($configFile);
37
38        if (!$result->hasPassed()) {
39            $errors = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
40
41            foreach ($result->errors as $error) {
42                $errors->writeln('<error>' . $error . '</error>');
43            }
44
45            return Command::FAILURE;
46        }
47
48        $discovered = $result->discovered;
49        $apps = $result->catalog;
50
51        $this->writeMissingConfigFileNotice($output, $this->config->findMissingDefaultFile($configFile));
52        $this->writeHttpListenerNotice($output, $result->httpListenerError);
53        $this->writeUnmatchedOverridesNotice($output, $apps);
54        $this->writeUnmarkedAppNotices($output, $discovered->unmarked);
55        $this->writeUnusedTemplateNotices($output, $discovered->listUnusedTemplateClasses());
56        $this->writeAppTable($output, $discovered, $apps, $result->workerSlots);
57        $output->writeln(\sprintf('Configuration and %d automations load; %d enabled.', $discovered->apps->count(), $apps->enabled->count()));
58
59        return Command::SUCCESS;
60    }
61
62    private function writeHttpListenerNotice(OutputInterface $output, ?string $error): void
63    {
64        if ($error !== null) {
65            $this->writeNotice($output, $error . ' The daemon refuses to start until it is fixed.');
66        }
67    }
68
69    private function writeUnmarkedAppNotices(OutputInterface $output, UnmarkedAppCollection $unmarked): void
70    {
71        foreach ($unmarked as $app) {
72            $this->writeNotice($output, \sprintf('%s implements App but is not marked #[Automation] or #[AppTemplate], so it will not run.', $app->class));
73        }
74    }
75
76    /** @param list<class-string> $templates */
77    private function writeUnusedTemplateNotices(OutputInterface $output, array $templates): void
78    {
79        foreach ($templates as $template) {
80            $this->writeNotice($output, \sprintf('%s is an #[AppTemplate], but no apps.<id>.class names it, so it does not run.', $template));
81        }
82    }
83
84    private function writeUnmatchedOverridesNotice(OutputInterface $output, AppCatalog $apps): void
85    {
86        if (!$apps->unmatchedOverrideIds->isEmpty()) {
87            $this->writeNotice($output, \sprintf(
88                'stewart.yaml or the environment configures automations that were not found, so their settings are ignored: %s. Known automations: %s.',
89                implode(', ', $apps->unmatchedOverrideIds->toStrings()),
90                $apps->knownIds->isEmpty() ? 'none' : implode(', ', $apps->knownIds->toStrings()),
91            ));
92        }
93    }
94
95    // Sorted by class, so the entries that run one class, and a template nothing runs, sit together.
96    private function writeAppTable(OutputInterface $output, DiscoveryResult $discovered, AppCatalog $apps, WorkerSlotCollection $slots): void
97    {
98        $rows = $discovered->apps->mapToList(static function (DiscoveredApp $app) use ($apps, $slots): array {
99            $definition = $apps->enabled->find($app->id);
100            $state = $definition === null ? 'disabled' : ($definition->startsPaused ? 'paused' : 'enabled');
101
102            return [$app->id->value, $app->class, (string) ($slots->findWorkerOfApp($app->id) ?? '-'), $state];
103        });
104
105        foreach ($discovered->listUnusedTemplateClasses() as $template) {
106            $rows[] = ['-', $template, '-', 'no entry'];
107        }
108
109        usort($rows, static fn(array $a, array $b): int => [$a[1], $a[0]] <=> [$b[1], $b[0]]);
110
111        if ($rows !== []) {
112            new Table($output)->setStyle('compact')->setHeaders(['app', 'class', 'worker', 'state'])->setRows($rows)->render();
113        }
114    }
115}

Inherited from Stewart\Runtime\Console\StewartCommand

19    protected function configure(): void
20    {
21        $this->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Path to stewart.yaml; defaults to stewart.yaml in the project root');
22    }
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    }