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
2 changes: 1 addition & 1 deletion docs/assets/no-violation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/assets/structarmed-showoff.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 69 additions & 40 deletions src/Cli/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,69 +183,54 @@ public function run(array $arguments, string $basePath): int
$analysisResultCache->store($cacheKey, $metadata, $ruleViolationCollection);
}

$elapsed = microtime(true) - $start;
$baseline = new Baseline();
$elapsed = microtime(true) - $start;
$baseline = new Baseline();
$unfilteredRuleViolationCollection = $ruleViolationCollection;
$shouldGenerateBaseline = isset($options['generate-baseline']);
$fixedCount = 0;

if (isset($options['generate-baseline'])) {
if (isset($options['fix'])) {
try {
$baseline->generate($ruleViolationCollection, $options['generate-baseline'], $basePath);
$ruleViolationCollection = $this->resolveRuleViolationCollection(
$unfilteredRuleViolationCollection,
$architecture,
$basePath,
$shouldGenerateBaseline
);
} catch (RuntimeException $runtimeException) {
echo 'Error: ' . $runtimeException->getMessage() . PHP_EOL;

return 1;
}

echo sprintf(
"Generated baseline [%s] with %d violation(s).\n",
$options['generate-baseline'],
$ruleViolationCollection->count()
);

return 0;
}

try {
$ruleViolationCollection = (new BaselineFilter())->apply(
$ruleViolationCollection,
$architecture,
$basePath
);
} catch (RuntimeException $runtimeException) {
echo 'Error: ' . $runtimeException->getMessage() . PHP_EOL;

return 1;
}

$fixedCount = 0;

if (isset($options['fix'])) {
$fixedCount = $this->fixViolations($architecture, $ruleViolationCollection);

if ($fixedCount > 0) {
$analysisResultCache->clear();

$files = $analyser->filesForAnalysis($architecture, $scanPaths);
$metadata = $analysisCacheMetadataFactory->metadata(
$files = $analyser->filesForAnalysis($architecture, $scanPaths);
$metadata = $analysisCacheMetadataFactory->metadata(
$basePath,
$configFile,
$scanPaths,
$files
);
$cacheKey = $analysisCacheMetadataFactory->key($metadata);
$ruleViolationCollection = $analyser->analyse(
$cacheKey = $analysisCacheMetadataFactory->key($metadata);
$unfilteredRuleViolationCollection = $analyser->analyse(
$architecture,
$scanPaths,
null,
$analyserOptions,
$files
);
$analysisResultCache->store($cacheKey, $metadata, $ruleViolationCollection);
$analysisResultCache->store($cacheKey, $metadata, $unfilteredRuleViolationCollection);

try {
$ruleViolationCollection = (new BaselineFilter())->apply(
$ruleViolationCollection,
$ruleViolationCollection = $this->resolveRuleViolationCollection(
$unfilteredRuleViolationCollection,
$architecture,
$basePath
$basePath,
$shouldGenerateBaseline
);
} catch (RuntimeException $runtimeException) {
echo 'Error: ' . $runtimeException->getMessage() . PHP_EOL;
Expand All @@ -255,22 +240,66 @@ public function run(array $arguments, string $basePath): int

$elapsed = microtime(true) - $start;
}
} else {
try {
$ruleViolationCollection = $this->resolveRuleViolationCollection(
$unfilteredRuleViolationCollection,
$architecture,
$basePath,
$shouldGenerateBaseline
);
} catch (RuntimeException $runtimeException) {
echo 'Error: ' . $runtimeException->getMessage() . PHP_EOL;

return 1;
}
}

if ($reportType === 'console' && $fixedCount > 0) {
echo PHP_EOL . $this->fixedViolationMessage($fixedCount) . PHP_EOL;
}

if ($shouldGenerateBaseline) {
try {
$baseline->generate($unfilteredRuleViolationCollection, $options['generate-baseline'], $basePath);
} catch (RuntimeException $runtimeException) {
echo 'Error: ' . $runtimeException->getMessage() . PHP_EOL;

return 1;
}

echo sprintf(
"Generated baseline [%s] with %d violation(s).\n",
$options['generate-baseline'],
$unfilteredRuleViolationCollection->count()
);

return 0;
}

$report = match ($reportType) {
'json' => (new JsonReport())->render($ruleViolationCollection, $elapsed),
default => (new ConsoleReport())->render($ruleViolationCollection, $elapsed),
};

if ($reportType === 'console' && $fixedCount > 0) {
echo PHP_EOL . $this->fixedViolationMessage($fixedCount) . PHP_EOL;
}

echo $report;

return $ruleViolationCollection->hasViolations() ? 1 : 0;
}

private function resolveRuleViolationCollection(
RuleViolationCollection $unfilteredRuleViolationCollection,
Architecture $architecture,
string $basePath,
bool $shouldGenerateBaseline
): RuleViolationCollection {
if ($shouldGenerateBaseline) {
return $unfilteredRuleViolationCollection;
}

return (new BaselineFilter())->apply($unfilteredRuleViolationCollection, $architecture, $basePath);
}

private function fixViolations(Architecture $architecture, RuleViolationCollection $ruleViolationCollection): int
{
$rules = $architecture->getRules();
Expand Down
35 changes: 35 additions & 0 deletions tests/Cli/AnalyseCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Boundwize\StructArmed\Tests\Cli;

use Boundwize\StructArmed\Cli\AnalyseCommand;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

#[CoversClass(AnalyseCommand::class)]
final class AnalyseCommandTest extends TestCase
{
/**
* @return iterable<string, array{int, string}>
*/
public static function fixedViolationMessageProvider(): iterable
{
yield 'singular' => [1, '1 violation has been fixed.'];
yield 'plural' => [2, '2 violations have been fixed.'];
}

#[DataProvider('fixedViolationMessageProvider')]
public function testFormatsFixedViolationMessage(int $fixedCount, string $expectedMessage): void
{
$reflectionMethod = new ReflectionMethod(AnalyseCommand::class, 'fixedViolationMessage');

$message = $reflectionMethod->invoke(new AnalyseCommand(), $fixedCount);

$this->assertIsString($message);
$this->assertStringContainsString($expectedMessage, $message);
}
}
Loading
Loading