Skip to content

Commit 1b017d1

Browse files
Refactor CheckCommand to utilize OutputFormatterFactory for output format handling, improving code organization and maintainability. Introduced OutputFormatterFactory class to centralize formatter creation and support format retrieval.
1 parent e15c63c commit 1b017d1

2 files changed

Lines changed: 80 additions & 42 deletions

File tree

src/Command/CheckCommand.php

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,8 @@
2222
use Phauthentic\BcCheck\Config\ConfigurationException;
2323
use Phauthentic\BcCheck\Config\ConfigurationLoader;
2424
use Phauthentic\BcCheck\DependencyInjection\ContainerFactory;
25+
use Phauthentic\BcCheck\Factory\OutputFormatterFactory;
2526
use Phauthentic\BcCheck\Git\GitException;
26-
use Phauthentic\BcCheck\Output\CheckstyleOutputFormatter;
27-
use Phauthentic\BcCheck\Output\GithubActionsFormatter;
28-
use Phauthentic\BcCheck\Output\GitlabCodeQualityOutputFormatter;
29-
use Phauthentic\BcCheck\Output\JsonOutputFormatter;
30-
use Phauthentic\BcCheck\Output\JunitOutputFormatter;
31-
use Phauthentic\BcCheck\Output\MarkdownOutputFormatter;
32-
use Phauthentic\BcCheck\Output\OutputFormatterInterface;
33-
use Phauthentic\BcCheck\Output\SarifOutputFormatter;
34-
use Phauthentic\BcCheck\Output\TextOutputFormatter;
3527
use Symfony\Component\Console\Attribute\AsCommand;
3628
use Symfony\Component\Console\Command\Command;
3729
use Symfony\Component\Console\Input\InputArgument;
@@ -45,14 +37,11 @@
4537
)]
4638
final class CheckCommand extends Command
4739
{
48-
private const FORMAT_TEXT = 'text';
49-
private const FORMAT_JSON = 'json';
50-
private const FORMAT_MARKDOWN = 'markdown';
51-
private const FORMAT_GITHUB = 'github-actions';
52-
private const FORMAT_SARIF = 'sarif';
53-
private const FORMAT_CHECKSTYLE = 'checkstyle';
54-
private const FORMAT_JUNIT = 'junit';
55-
private const FORMAT_GITLAB = 'gitlab';
40+
public function __construct(
41+
private readonly OutputFormatterFactory $formatterFactory = new OutputFormatterFactory(),
42+
) {
43+
parent::__construct();
44+
}
5645

5746
protected function configure(): void
5847
{
@@ -83,17 +72,10 @@ protected function configure(): void
8372
'f',
8473
InputOption::VALUE_REQUIRED,
8574
sprintf(
86-
'Output format (%s, %s, %s, %s, %s, %s, %s, %s)',
87-
self::FORMAT_TEXT,
88-
self::FORMAT_JSON,
89-
self::FORMAT_MARKDOWN,
90-
self::FORMAT_GITHUB,
91-
self::FORMAT_SARIF,
92-
self::FORMAT_CHECKSTYLE,
93-
self::FORMAT_JUNIT,
94-
self::FORMAT_GITLAB,
75+
'Output format (%s)',
76+
implode(', ', $this->formatterFactory->getSupportedFormats()),
9577
),
96-
self::FORMAT_TEXT,
78+
OutputFormatterFactory::FORMAT_TEXT,
9779
)
9880
->addOption(
9981
'show-files',
@@ -140,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
140122
}
141123

142124
// Get formatter
143-
$formatter = $this->getFormatter($format);
125+
$formatter = $this->formatterFactory->create($format);
144126

145127
// Build container with services
146128
try {
@@ -205,18 +187,4 @@ private function loadConfiguration(?string $configPath): Configuration
205187

206188
return $loader->createDefault();
207189
}
208-
209-
private function getFormatter(string $format): OutputFormatterInterface
210-
{
211-
return match ($format) {
212-
self::FORMAT_JSON => new JsonOutputFormatter(),
213-
self::FORMAT_MARKDOWN => new MarkdownOutputFormatter(),
214-
self::FORMAT_GITHUB => new GithubActionsFormatter(),
215-
self::FORMAT_SARIF => new SarifOutputFormatter(),
216-
self::FORMAT_CHECKSTYLE => new CheckstyleOutputFormatter(),
217-
self::FORMAT_JUNIT => new JunitOutputFormatter(),
218-
self::FORMAT_GITLAB => new GitlabCodeQualityOutputFormatter(),
219-
default => new TextOutputFormatter(),
220-
};
221-
}
222190
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Florian Krämer (https://florian-kraemer.net)
7+
* Licensed under The MIT License
8+
* For full copyright and license information, please see the LICENSE.txt
9+
* Redistributions of files must retain the above copyright notice.
10+
*
11+
* @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
12+
* @author Florian Krämer
13+
* @link https://github.com/Phauthentic
14+
* @license https://opensource.org/licenses/GPL-3.0 GPL License
15+
*/
16+
17+
namespace Phauthentic\BcCheck\Factory;
18+
19+
use Phauthentic\BcCheck\Output\CheckstyleOutputFormatter;
20+
use Phauthentic\BcCheck\Output\GithubActionsFormatter;
21+
use Phauthentic\BcCheck\Output\GitlabCodeQualityOutputFormatter;
22+
use Phauthentic\BcCheck\Output\JsonOutputFormatter;
23+
use Phauthentic\BcCheck\Output\JunitOutputFormatter;
24+
use Phauthentic\BcCheck\Output\MarkdownOutputFormatter;
25+
use Phauthentic\BcCheck\Output\OutputFormatterInterface;
26+
use Phauthentic\BcCheck\Output\SarifOutputFormatter;
27+
use Phauthentic\BcCheck\Output\TextOutputFormatter;
28+
29+
final readonly class OutputFormatterFactory
30+
{
31+
public const FORMAT_TEXT = 'text';
32+
public const FORMAT_JSON = 'json';
33+
public const FORMAT_MARKDOWN = 'markdown';
34+
public const FORMAT_GITHUB = 'github-actions';
35+
public const FORMAT_SARIF = 'sarif';
36+
public const FORMAT_CHECKSTYLE = 'checkstyle';
37+
public const FORMAT_JUNIT = 'junit';
38+
public const FORMAT_GITLAB = 'gitlab';
39+
40+
public function create(string $format): OutputFormatterInterface
41+
{
42+
return match ($format) {
43+
self::FORMAT_JSON => new JsonOutputFormatter(),
44+
self::FORMAT_MARKDOWN => new MarkdownOutputFormatter(),
45+
self::FORMAT_GITHUB => new GithubActionsFormatter(),
46+
self::FORMAT_SARIF => new SarifOutputFormatter(),
47+
self::FORMAT_CHECKSTYLE => new CheckstyleOutputFormatter(),
48+
self::FORMAT_JUNIT => new JunitOutputFormatter(),
49+
self::FORMAT_GITLAB => new GitlabCodeQualityOutputFormatter(),
50+
default => new TextOutputFormatter(),
51+
};
52+
}
53+
54+
/**
55+
* @return list<string>
56+
*/
57+
public function getSupportedFormats(): array
58+
{
59+
return [
60+
self::FORMAT_TEXT,
61+
self::FORMAT_JSON,
62+
self::FORMAT_MARKDOWN,
63+
self::FORMAT_GITHUB,
64+
self::FORMAT_SARIF,
65+
self::FORMAT_CHECKSTYLE,
66+
self::FORMAT_JUNIT,
67+
self::FORMAT_GITLAB,
68+
];
69+
}
70+
}

0 commit comments

Comments
 (0)