Lines
100.00%
26 / 26
Methods
100.00%
10 / 10
Classes
100.00%
1 / 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 | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 6 | ||
| [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 | ||
| 17 | final class DoctorCommand extends StewartCommand | |
| 18 | { | |
| 19 | public const string NAME = 'doctor'; | |
| 20 | ||
| 21 | private const string OFFLINE_OPTION = 'offline'; | |
| 22 | ||
| 23 | public function __construct( | |
| 24 | private readonly ContainerChecks $containerChecks, | |
| 25 | private readonly ConnectionChecks $connectionChecks, | |
| 26 | ) { | |
| 27 | parent::__construct(); | |
| 28 | } | |
| 29 | ||
| 30 | protected function configure(): void | |
| 31 | { | |
| 32 | parent::configure(); | |
| 33 | $this->addOption(self::OFFLINE_OPTION, null, InputOption::VALUE_NONE, 'Check only this container, not the settings and connections'); | |
| 34 | } | |
| 35 | ||
| 36 | protected function execute(InputInterface $input, OutputInterface $output): int | |
| 37 | { | |
| 38 | // Composer's platform check already refused an older PHP before this command could load. | |
| 39 | $output->writeln(\sprintf('PHP %s (%s)', \PHP_VERSION, \PHP_BINARY)); | |
| 40 | $findings = $this->containerChecks->runContainerChecks(); | |
| 41 | ||
| 42 | if ($input->getOption(self::OFFLINE_OPTION) !== true) { | |
| 43 | array_push($findings, ...$this->connectionChecks->runConnectionChecks($this->findConfigOption($input))); | |
| 44 | } | |
| 45 | ||
| 46 | $healthy = true; | |
| 47 | ||
| 48 | foreach ($findings as $finding) { | |
| 49 | $output->writeln(rtrim(\sprintf(' %-12s %s %s', $finding->label, $finding->outcome->getMark(), $finding->detail))); | |
| 50 | $healthy = $healthy && $finding->outcome !== DoctorOutcome::Fail; | |
| 51 | } | |
| 52 | ||
| 53 | $output->writeln($healthy ? 'All good.' : 'Problems found.'); | |
| 54 | ||
| 55 | return $healthy ? Command::SUCCESS : Command::FAILURE; | |
| 56 | } | |
| 57 | } |
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 | } |