Skip to content

Commit 184f35e

Browse files
committed
Get rid of constants
1 parent 7cf6c19 commit 184f35e

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/Symfony/Component/Messenger/Command/StatsCommand.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@
2929
#[AsCommand(name: 'messenger:stats', description: 'Show the message count for one or more transports')]
3030
class StatsCommand extends Command
3131
{
32-
public const OUTPUT_FORMAT_TEXT = 'text';
33-
34-
public const OUTPUT_FORMAT_JSON = 'json';
35-
36-
public const ALL_OUTPUT_FORMATS = [self::OUTPUT_FORMAT_TEXT, self::OUTPUT_FORMAT_JSON];
37-
3832
public function __construct(
3933
private ContainerInterface $transportLocator,
4034
private array $transportNames = [],
@@ -44,10 +38,10 @@ public function __construct(
4438

4539
protected function configure(): void
4640
{
47-
$outputFormats = implode(', ', self::ALL_OUTPUT_FORMATS);
41+
$outputFormats = implode(', ', $this->getAvailableFormatOptions());
4842
$this
4943
->addArgument('transport_names', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'List of transports\' names')
50-
->addOption('format', '', InputOption::VALUE_REQUIRED, 'The output format, e.g.: '.$outputFormats, self::OUTPUT_FORMAT_TEXT, self::ALL_OUTPUT_FORMATS)
44+
->addOption('format', '', InputOption::VALUE_REQUIRED, 'The output format, e.g.: '.$outputFormats, 'text', $this->getAvailableFormatOptions())
5145
->setHelp(<<<EOF
5246
The <info>%command.name%</info> command counts the messages for all the transports:
5347
@@ -70,11 +64,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7064
{
7165
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
7266

73-
$format = match ($input->getOption('format')) {
74-
self::OUTPUT_FORMAT_TEXT,
75-
self::OUTPUT_FORMAT_JSON => $input->getOption('format'),
76-
default => throw new InvalidArgumentException('Invalid output format.'),
77-
};
67+
$format = $input->getOption('format');
68+
if (!\in_array($format, $this->getAvailableFormatOptions(), true)) {
69+
throw new InvalidArgumentException('Invalid output format.');
70+
}
7871

7972
$transportNames = $this->transportNames;
8073
if ($input->getArgument('transport_names')) {
@@ -101,8 +94,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
10194
}
10295

10396
match ($format) {
104-
self::OUTPUT_FORMAT_TEXT => $this->outputText($io, $outputTable, $uncountableTransports),
105-
self::OUTPUT_FORMAT_JSON => $this->outputJson($io, $outputTable, $uncountableTransports),
97+
'text' => $this->outputText($io, $outputTable, $uncountableTransports),
98+
'json' => $this->outputJson($io, $outputTable, $uncountableTransports),
10699
};
107100

108101
return 0;
@@ -134,8 +127,14 @@ private function outputJson(SymfonyStyle $io, array $outputTable, array $uncount
134127
private function formatSupportsWarnings(string $format): bool
135128
{
136129
return match ($format) {
137-
self::OUTPUT_FORMAT_TEXT => true,
138-
self::OUTPUT_FORMAT_JSON => false,
130+
'text' => true,
131+
'json' => false,
139132
};
140133
}
134+
135+
/** @return string[] */
136+
private function getAvailableFormatOptions(): array
137+
{
138+
return ['text', 'json'];
139+
}
141140
}

0 commit comments

Comments
 (0)