Skip to content

Commit de4473a

Browse files
wip
1 parent 88f96b2 commit de4473a

File tree

5 files changed

+111
-44
lines changed

5 files changed

+111
-44
lines changed

src/CLI/Baseline.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,30 @@ public function applyTo(Violations $violations, bool $ignoreBaselineLinenumbers)
2727
$violations->remove($this->violations, $ignoreBaselineLinenumbers);
2828
}
2929

30+
/**
31+
* @psalm-suppress RiskyTruthyFalsyComparison
32+
*/
33+
public static function resolveFilePath(?string $filePath, string $defaultFilePath): ?string
34+
{
35+
if (!$filePath && file_exists($defaultFilePath)) {
36+
$filePath = $defaultFilePath;
37+
}
38+
39+
return $filePath ?: null;
40+
}
41+
3042
public static function empty(): self
3143
{
3244
return new self(new Violations(), '');
3345
}
3446

35-
/**
36-
* @psalm-suppress RiskyTruthyFalsyComparison
37-
*/
38-
public static function create(bool $skipBaseline, ?string $baselineFilePath, string $defaultFilePath): self
47+
public static function create(bool $skipBaseline, ?string $baselineFilePath): self
3948
{
40-
if ($skipBaseline) {
49+
if ($skipBaseline || null === $baselineFilePath) {
4150
return self::empty();
4251
}
4352

44-
if (!$baselineFilePath && file_exists($defaultFilePath)) {
45-
$baselineFilePath = $defaultFilePath;
46-
}
47-
48-
return $baselineFilePath ? self::loadFromFile($baselineFilePath) : self::empty();
53+
return self::loadFromFile($baselineFilePath);
4954
}
5055

5156
public static function loadFromFile(string $filename): self

src/CLI/Command/Check.php

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Arkitect\CLI\Command;
66

77
use Arkitect\CLI\Baseline;
8-
use Arkitect\CLI\Config;
8+
use Arkitect\CLI\ConfigBuilder;
99
use Arkitect\CLI\Printer\PrinterFactory;
1010
use Arkitect\CLI\Progress\DebugProgress;
1111
use Arkitect\CLI\Progress\ProgressBarProgress;
@@ -16,7 +16,6 @@
1616
use Symfony\Component\Console\Input\InputOption;
1717
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1818
use Symfony\Component\Console\Output\OutputInterface;
19-
use Webmozart\Assert\Assert;
2019

2120
class Check extends Command
2221
{
@@ -123,18 +122,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
123122

124123
$this->printHeadingLine($output);
125124

126-
$printer = (new PrinterFactory())->create($format);
125+
$config = ConfigBuilder::loadFromFile($rulesFilename)
126+
->stopOnFailure($stopOnFailure)
127+
->targetPhpVersion(TargetPhpVersion::create($phpVersion))
128+
->baselineFilePath(Baseline::resolveFilePath($useBaseline, self::DEFAULT_BASELINE_FILENAME))
129+
->ignoreBaselineLinenumbers($ignoreBaselineLinenumbers)
130+
->skipBaseline($skipBaseline)
131+
->format($format);
127132

128-
$progress = $verbose ? new DebugProgress($output) : new ProgressBarProgress($output);
133+
$printer = PrinterFactory::create($config->getFormat());
129134

130-
$baseline = Baseline::create($skipBaseline, $useBaseline, self::DEFAULT_BASELINE_FILENAME);
135+
$progress = $verbose ? new DebugProgress($output) : new ProgressBarProgress($output);
131136

132-
$config = ConfigBuilder::loadFromFile($rulesFilename);
133-
$config->stopOnFailure($stopOnFailure);
134-
$config->targetPhpVersion(TargetPhpVersion::create($phpVersion));
135-
$config->ignoreBaselineLinenumbers($ignoreBaselineLinenumbers);
137+
$baseline = Baseline::create($config->isSkipBaseline(), $config->getBaselineFilePath());
136138

137-
$baseline->getFilename() && $output->writeln("Baseline file '{$baseline->getFilename()}' found");
139+
null !== $config->getBaselineFilePath() && $output->writeln("Baseline file '{$config->getBaselineFilePath()}' found");
138140
$output->writeln("Config file '$rulesFilename' found\n");
139141

140142
$runner = new Runner();
@@ -192,24 +194,3 @@ protected function printExecutionTime(OutputInterface $output, float $startTime)
192194
$output->writeln("⏱️ Execution time: $executionTime\n");
193195
}
194196
}
195-
196-
class ConfigBuilder
197-
{
198-
public static function loadFromFile(string $filePath): Config
199-
{
200-
Assert::file($filePath, "Config file '$filePath' not found");
201-
202-
$config = new Config();
203-
204-
\Closure::fromCallable(function () use ($config, $filePath): ?bool {
205-
/** @psalm-suppress UnresolvableInclude $config */
206-
$configFunction = require $filePath;
207-
208-
Assert::isCallable($configFunction);
209-
210-
return $configFunction($config);
211-
})();
212-
213-
return $config;
214-
}
215-
}

src/CLI/Config.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Arkitect\ClassSet;
77
use Arkitect\ClassSetRules;
8+
use Arkitect\CLI\Printer\PrinterFactory;
89
use Arkitect\Rules\DSL\ArchRule;
910

1011
class Config
@@ -18,8 +19,14 @@ class Config
1819

1920
private bool $stopOnFailure;
2021

22+
private bool $skipBaseline;
23+
24+
private ?string $baselineFilePath;
25+
2126
private bool $ignoreBaselineLinenumbers;
2227

28+
private string $format;
29+
2330
private TargetPhpVersion $targetPhpVersion;
2431

2532
public function __construct()
@@ -28,7 +35,10 @@ public function __construct()
2835
$this->runOnlyARule = false;
2936
$this->parseCustomAnnotations = true;
3037
$this->stopOnFailure = false;
38+
$this->skipBaseline = false;
39+
$this->baselineFilePath = null;
3140
$this->ignoreBaselineLinenumbers = false;
41+
$this->format = PrinterFactory::default();
3242
$this->targetPhpVersion = TargetPhpVersion::latest();
3343
}
3444

@@ -83,16 +93,30 @@ public function getTargetPhpVersion(): TargetPhpVersion
8393
return $this->targetPhpVersion;
8494
}
8595

86-
public function stopOnFailure(bool $stopOnFailure): bool
96+
public function stopOnFailure(bool $stopOnFailure): self
8797
{
88-
return $this->stopOnFailure = $stopOnFailure;
98+
$this->stopOnFailure = $stopOnFailure;
99+
100+
return $this;
89101
}
90102

91103
public function isStopOnFailure(): bool
92104
{
93105
return $this->stopOnFailure;
94106
}
95107

108+
public function baselineFilePath(?string $baselineFilePath): self
109+
{
110+
$this->baselineFilePath = $baselineFilePath;
111+
112+
return $this;
113+
}
114+
115+
public function getBaselineFilePath(): ?string
116+
{
117+
return $this->baselineFilePath;
118+
}
119+
96120
public function ignoreBaselineLinenumbers(bool $ignoreBaselineLinenumbers): self
97121
{
98122
$this->ignoreBaselineLinenumbers = $ignoreBaselineLinenumbers;
@@ -104,4 +128,28 @@ public function isIgnoreBaselineLinenumbers(): bool
104128
{
105129
return $this->ignoreBaselineLinenumbers;
106130
}
131+
132+
public function format(string $format): self
133+
{
134+
$this->format = $format;
135+
136+
return $this;
137+
}
138+
139+
public function getFormat(): string
140+
{
141+
return $this->format;
142+
}
143+
144+
public function skipBaseline(bool $skipBaseline): self
145+
{
146+
$this->skipBaseline = $skipBaseline;
147+
148+
return $this;
149+
}
150+
151+
public function isSkipBaseline(): bool
152+
{
153+
return $this->skipBaseline;
154+
}
107155
}

src/CLI/ConfigBuilder.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Arkitect\CLI;
6+
7+
use Webmozart\Assert\Assert;
8+
9+
class ConfigBuilder
10+
{
11+
public static function loadFromFile(string $filePath): Config
12+
{
13+
Assert::file($filePath, "Config file '$filePath' not found");
14+
15+
$config = new Config();
16+
17+
\Closure::fromCallable(function () use ($config, $filePath): ?bool {
18+
/** @psalm-suppress UnresolvableInclude $config */
19+
$configFunction = require $filePath;
20+
21+
Assert::isCallable($configFunction);
22+
23+
return $configFunction($config);
24+
})();
25+
26+
return $config;
27+
}
28+
}

src/CLI/Printer/PrinterFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66
final class PrinterFactory
77
{
8-
public function create(string $format): Printer
8+
public static function default(): string
9+
{
10+
return Printer::FORMAT_TEXT;
11+
}
12+
13+
public static function create(string $format): Printer
914
{
1015
switch ($format) {
1116
case Printer::FORMAT_GITLAB:

0 commit comments

Comments
 (0)