Skip to content

Commit 3db4deb

Browse files
wip
1 parent 45bd46f commit 3db4deb

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

src/CLI/Command/Check.php

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
use Arkitect\CLI\Baseline;
88
use Arkitect\CLI\ConfigBuilder;
9+
use Arkitect\CLI\Printer\Printer;
910
use Arkitect\CLI\Printer\PrinterFactory;
1011
use Arkitect\CLI\Progress\DebugProgress;
12+
use Arkitect\CLI\Progress\Progress;
1113
use Arkitect\CLI\Progress\ProgressBarProgress;
1214
use Arkitect\CLI\Runner;
1315
use Arkitect\CLI\TargetPhpVersion;
@@ -16,6 +18,7 @@
1618
use Symfony\Component\Console\Input\InputOption;
1719
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1820
use Symfony\Component\Console\Output\OutputInterface;
21+
use Webmozart\Assert\Assert;
1922

2023
class Check extends Command
2124
{
@@ -26,6 +29,7 @@ class Check extends Command
2629
private const SKIP_BASELINE_PARAM = 'skip-baseline';
2730
private const IGNORE_BASELINE_LINENUMBERS_PARAM = 'ignore-baseline-linenumbers';
2831
private const FORMAT_PARAM = 'format';
32+
private const AUTOLOAD_PARAM = 'autoload';
2933

3034
private const GENERATE_BASELINE_PARAM = 'generate-baseline';
3135
private const DEFAULT_RULES_FILENAME = 'phparkitect.php';
@@ -95,6 +99,12 @@ protected function configure(): void
9599
InputOption::VALUE_OPTIONAL,
96100
'Output format: text (default), json, gitlab',
97101
'text'
102+
)
103+
->addOption(
104+
self::AUTOLOAD_PARAM,
105+
'a',
106+
InputOption::VALUE_REQUIRED,
107+
'Specify an autoload file to use',
98108
);
99109
}
100110

@@ -123,20 +133,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
123133
$this->printHeadingLine($output);
124134

125135
$config = ConfigBuilder::loadFromFile($rulesFilename)
136+
->autoloadFilePath($input->getOption(self::AUTOLOAD_PARAM))
126137
->stopOnFailure($stopOnFailure)
127138
->targetPhpVersion(TargetPhpVersion::create($phpVersion))
128139
->baselineFilePath(Baseline::resolveFilePath($useBaseline, self::DEFAULT_BASELINE_FILENAME))
129140
->ignoreBaselineLinenumbers($ignoreBaselineLinenumbers)
130141
->skipBaseline($skipBaseline)
131142
->format($format);
132143

133-
$printer = PrinterFactory::create($config->getFormat());
134-
135-
$progress = $verbose ? new DebugProgress($output) : new ProgressBarProgress($output);
144+
$this->requireAutoload($config->getAutoloadFilePath(), $output);
145+
$printer = $this->createPrinter($config->getFormat(), $output);
146+
$progress = $this->createProgress($verbose, $output);
147+
$baseline = $this->createBaseline($config->isSkipBaseline(), $config->getBaselineFilePath(), $output);
136148

137-
$baseline = Baseline::create($config->isSkipBaseline(), $config->getBaselineFilePath());
138-
139-
$baseline->getFilename() && $output->writeln("Baseline file '{$baseline->getFilename()}' found");
140149
$output->writeln("Config file '$rulesFilename' found\n");
141150

142151
$runner = new Runner();
@@ -177,6 +186,45 @@ protected function execute(InputInterface $input, OutputInterface $output): int
177186
}
178187
}
179188

189+
/**
190+
* @psalm-suppress UnresolvableInclude
191+
*/
192+
protected function requireAutoload(?string $filePath, OutputInterface $output): void
193+
{
194+
if (null === $filePath) {
195+
return;
196+
}
197+
198+
Assert::file($filePath, "Cannot find '$filePath'");
199+
200+
require_once $filePath;
201+
202+
$output->writeln("Autoload file '$filePath' added");
203+
}
204+
205+
protected function createPrinter(string $format, OutputInterface $output): Printer
206+
{
207+
$output->writeln("Output format: $format");
208+
209+
return PrinterFactory::create($format);
210+
}
211+
212+
protected function createProgress(bool $verbose, OutputInterface $output): Progress
213+
{
214+
$output->writeln('Progress: '.($verbose ? 'debug' : 'bar'));
215+
216+
return $verbose ? new DebugProgress($output) : new ProgressBarProgress($output);
217+
}
218+
219+
protected function createBaseline(bool $skipBaseline, ?string $baselineFilePath, OutputInterface $output): Baseline
220+
{
221+
$baseline = Baseline::create($skipBaseline, $baselineFilePath);
222+
223+
$baseline->getFilename() && $output->writeln("Baseline file '{$baseline->getFilename()}' found");
224+
225+
return $baseline;
226+
}
227+
180228
protected function printHeadingLine(OutputInterface $output): void
181229
{
182230
$app = $this->getApplication();

src/CLI/Config.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Config
2727

2828
private string $format;
2929

30+
private ?string $autoloadFilePath;
31+
3032
private TargetPhpVersion $targetPhpVersion;
3133

3234
public function __construct()
@@ -39,6 +41,7 @@ public function __construct()
3941
$this->baselineFilePath = null;
4042
$this->ignoreBaselineLinenumbers = false;
4143
$this->format = PrinterFactory::default();
44+
$this->autoloadFilePath = null;
4245
$this->targetPhpVersion = TargetPhpVersion::latest();
4346
}
4447

@@ -152,4 +155,16 @@ public function isSkipBaseline(): bool
152155
{
153156
return $this->skipBaseline;
154157
}
158+
159+
public function autoloadFilePath(?string $autoloadFilePath): self
160+
{
161+
$this->autoloadFilePath = $autoloadFilePath;
162+
163+
return $this;
164+
}
165+
166+
public function getAutoloadFilePath(): ?string
167+
{
168+
return $this->autoloadFilePath;
169+
}
155170
}

0 commit comments

Comments
 (0)