Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/CLI/Baseline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);

namespace Arkitect\CLI;

use Arkitect\Rules\Violations;

class Baseline
{
private Violations $violations;

private string $filename;

private function __construct(Violations $violations, string $filename)
{
$this->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.");

Check warning on line 54 in src/CLI/Baseline.php

View check run for this annotation

Codecov / codecov/patch

src/CLI/Baseline.php#L54

Added line #L54 was not covered by tests
}

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;
}
}
42 changes: 9 additions & 33 deletions src/CLI/Command/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down