Skip to content

Commit 46c0bd2

Browse files
Create baseline class (#487)
Extracts baseline management logic from Check command to a separate Baseline class
1 parent 4ee55c0 commit 46c0bd2

File tree

2 files changed

+82
-33
lines changed

2 files changed

+82
-33
lines changed

src/CLI/Baseline.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Arkitect\CLI;
5+
6+
use Arkitect\Rules\Violations;
7+
8+
class Baseline
9+
{
10+
private Violations $violations;
11+
12+
private string $filename;
13+
14+
private function __construct(Violations $violations, string $filename)
15+
{
16+
$this->violations = $violations;
17+
$this->filename = $filename;
18+
}
19+
20+
public function getFilename(): string
21+
{
22+
return $this->filename;
23+
}
24+
25+
public function applyTo(Violations $violations, bool $ignoreBaselineLinenumbers): void
26+
{
27+
$violations->remove($this->violations, $ignoreBaselineLinenumbers);
28+
}
29+
30+
public static function empty(): self
31+
{
32+
return new self(new Violations(), '');
33+
}
34+
35+
/**
36+
* @psalm-suppress RiskyTruthyFalsyComparison
37+
*/
38+
public static function create(bool $skipBaseline, ?string $baselineFilePath, string $defaultFilePath): self
39+
{
40+
if ($skipBaseline) {
41+
return self::empty();
42+
}
43+
44+
if (!$baselineFilePath && file_exists($defaultFilePath)) {
45+
$baselineFilePath = $defaultFilePath;
46+
}
47+
48+
return $baselineFilePath ? self::loadFromFile($baselineFilePath) : self::empty();
49+
}
50+
51+
public static function loadFromFile(string $filename): self
52+
{
53+
if (!file_exists($filename)) {
54+
throw new \RuntimeException("Baseline file '$filename' not found.");
55+
}
56+
57+
return new self(
58+
Violations::fromJson(file_get_contents($filename)),
59+
$filename
60+
);
61+
}
62+
63+
public static function save(?string $filename, string $defaultFilePath, Violations $violations): string
64+
{
65+
if (null === $filename) {
66+
$filename = $defaultFilePath;
67+
}
68+
69+
file_put_contents($filename, json_encode($violations, \JSON_PRETTY_PRINT));
70+
71+
return $filename;
72+
}
73+
}

src/CLI/Command/Check.php

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Arkitect\CLI\Command;
66

7+
use Arkitect\CLI\Baseline;
78
use Arkitect\CLI\Config;
89
use Arkitect\CLI\Printer\PrinterFactory;
910
use Arkitect\CLI\Progress\DebugProgress;
@@ -123,19 +124,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
123124

124125
$progress = $verbose ? new DebugProgress($output) : new ProgressBarProgress($output);
125126

126-
$this->printHeadingLine($output);
127-
128-
if (true !== $skipBaseline && !$useBaseline && file_exists(self::DEFAULT_BASELINE_FILENAME)) {
129-
$useBaseline = self::DEFAULT_BASELINE_FILENAME;
130-
}
127+
$baseline = Baseline::create($skipBaseline, $useBaseline, self::DEFAULT_BASELINE_FILENAME);
131128

132-
if ($useBaseline && !file_exists($useBaseline)) {
133-
$output->writeln("❌ Baseline file '$useBaseline' not found.");
129+
$printer = (new PrinterFactory())->create($format);
134130

135-
return self::ERROR_CODE;
136-
}
131+
$this->printHeadingLine($output);
137132

138-
$output->writeln("Baseline file '$useBaseline' found");
133+
$baseline->getFilename() && $output->writeln("Baseline file '{$baseline->getFilename()}' found");
139134

140135
$rulesFilename = $this->getConfigFilename($input);
141136

@@ -151,29 +146,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
151146
$violations = $result->getViolations();
152147

153148
if (false !== $generateBaseline) {
154-
if (null === $generateBaseline) {
155-
$generateBaseline = self::DEFAULT_BASELINE_FILENAME;
156-
}
157-
$this->saveBaseline($generateBaseline, $violations);
149+
$baselineFilePath = Baseline::save($generateBaseline, self::DEFAULT_BASELINE_FILENAME, $violations);
158150

159-
$output->writeln("ℹ️ Baseline file '$generateBaseline' created!");
151+
$output->writeln("ℹ️ Baseline file '$baselineFilePath' created!");
160152

161153
return self::SUCCESS_CODE;
162154
}
163155

164-
if ($useBaseline) {
165-
$baseline = $this->loadBaseline($useBaseline);
166-
167-
$violations->remove($baseline, $ignoreBaselineLinenumbers);
168-
}
169-
170-
$printer = (new PrinterFactory())->create($format);
156+
$baseline->applyTo($violations, $ignoreBaselineLinenumbers);
171157

172158
// we always print this so we do not have to do additional ifs later
173159
$stdOut->writeln($printer->print($violations->groupedByFqcn()));
174160

175161
if ($violations->count() > 0) {
176-
$output->writeln(\sprintf('⚠️ %s violations detected!', \count($violations)));
162+
$output->writeln("⚠️ {$violations->count()} violations detected!");
177163
}
178164

179165
if ($result->hasParsingErrors()) {
@@ -222,16 +208,6 @@ protected function printExecutionTime(OutputInterface $output, float $startTime)
222208
$output->writeln("⏱️ Execution time: $executionTime\n");
223209
}
224210

225-
private function loadBaseline(string $filename): Violations
226-
{
227-
return Violations::fromJson(file_get_contents($filename));
228-
}
229-
230-
private function saveBaseline(string $filename, Violations $violations): void
231-
{
232-
file_put_contents($filename, json_encode($violations, \JSON_PRETTY_PRINT));
233-
}
234-
235211
private function getConfigFilename(InputInterface $input): string
236212
{
237213
$filename = $input->getOption(self::CONFIG_FILENAME_PARAM);

0 commit comments

Comments
 (0)