diff --git a/src/CLI/Baseline.php b/src/CLI/Baseline.php new file mode 100644 index 00000000..f4480060 --- /dev/null +++ b/src/CLI/Baseline.php @@ -0,0 +1,73 @@ +violations = $violations; + $this->filename = $filename; + } + + public function getFilename(): string + { + return $this->filename; + } + + public function applyTo(Violations $violations, bool $ignoreBaselineLinenumbers): void + { + $violations->remove($this->violations, $ignoreBaselineLinenumbers); + } + + public static function empty(): self + { + return new self(new Violations(), ''); + } + + /** + * @psalm-suppress RiskyTruthyFalsyComparison + */ + public static function create(bool $skipBaseline, ?string $baselineFilePath, string $defaultFilePath): self + { + if ($skipBaseline) { + return self::empty(); + } + + if (!$baselineFilePath && file_exists($defaultFilePath)) { + $baselineFilePath = $defaultFilePath; + } + + return $baselineFilePath ? self::loadFromFile($baselineFilePath) : self::empty(); + } + + public static function loadFromFile(string $filename): self + { + if (!file_exists($filename)) { + throw new \RuntimeException("Baseline file '$filename' not found."); + } + + return new self( + Violations::fromJson(file_get_contents($filename)), + $filename + ); + } + + public static function save(?string $filename, string $defaultFilePath, Violations $violations): string + { + if (null === $filename) { + $filename = $defaultFilePath; + } + + file_put_contents($filename, json_encode($violations, \JSON_PRETTY_PRINT)); + + return $filename; + } +} diff --git a/src/CLI/Command/Check.php b/src/CLI/Command/Check.php index 22601e80..a46db73b 100644 --- a/src/CLI/Command/Check.php +++ b/src/CLI/Command/Check.php @@ -4,6 +4,7 @@ namespace Arkitect\CLI\Command; +use Arkitect\CLI\Baseline; use Arkitect\CLI\Config; use Arkitect\CLI\Printer\PrinterFactory; use Arkitect\CLI\Progress\DebugProgress; @@ -123,19 +124,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int $progress = $verbose ? new DebugProgress($output) : new ProgressBarProgress($output); - $this->printHeadingLine($output); - - if (true !== $skipBaseline && !$useBaseline && file_exists(self::DEFAULT_BASELINE_FILENAME)) { - $useBaseline = self::DEFAULT_BASELINE_FILENAME; - } + $baseline = Baseline::create($skipBaseline, $useBaseline, self::DEFAULT_BASELINE_FILENAME); - if ($useBaseline && !file_exists($useBaseline)) { - $output->writeln("❌ Baseline file '$useBaseline' not found."); + $printer = (new PrinterFactory())->create($format); - return self::ERROR_CODE; - } + $this->printHeadingLine($output); - $output->writeln("Baseline file '$useBaseline' found"); + $baseline->getFilename() && $output->writeln("Baseline file '{$baseline->getFilename()}' found"); $rulesFilename = $this->getConfigFilename($input); @@ -151,29 +146,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int $violations = $result->getViolations(); if (false !== $generateBaseline) { - if (null === $generateBaseline) { - $generateBaseline = self::DEFAULT_BASELINE_FILENAME; - } - $this->saveBaseline($generateBaseline, $violations); + $baselineFilePath = Baseline::save($generateBaseline, self::DEFAULT_BASELINE_FILENAME, $violations); - $output->writeln("ℹ️ Baseline file '$generateBaseline' created!"); + $output->writeln("ℹ️ Baseline file '$baselineFilePath' created!"); return self::SUCCESS_CODE; } - if ($useBaseline) { - $baseline = $this->loadBaseline($useBaseline); - - $violations->remove($baseline, $ignoreBaselineLinenumbers); - } - - $printer = (new PrinterFactory())->create($format); + $baseline->applyTo($violations, $ignoreBaselineLinenumbers); // we always print this so we do not have to do additional ifs later $stdOut->writeln($printer->print($violations->groupedByFqcn())); if ($violations->count() > 0) { - $output->writeln(\sprintf('⚠️ %s violations detected!', \count($violations))); + $output->writeln("⚠️ {$violations->count()} violations detected!"); } if ($result->hasParsingErrors()) { @@ -222,16 +208,6 @@ protected function printExecutionTime(OutputInterface $output, float $startTime) $output->writeln("⏱️ Execution time: $executionTime\n"); } - private function loadBaseline(string $filename): Violations - { - return Violations::fromJson(file_get_contents($filename)); - } - - private function saveBaseline(string $filename, Violations $violations): void - { - file_put_contents($filename, json_encode($violations, \JSON_PRETTY_PRINT)); - } - private function getConfigFilename(InputInterface $input): string { $filename = $input->getOption(self::CONFIG_FILENAME_PARAM);