52 lines
1.8 KiB
PHP
52 lines
1.8 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Support\Architecture\ArchitectureScanner;
|
|
use Illuminate\Console\Command;
|
|
|
|
final class ArchitectureScanCommand extends Command
|
|
{
|
|
protected $signature = 'app:architecture-scan {--format=console : console or json} {--fail-on=error : error, warning, or none}';
|
|
protected $description = 'Scan the codebase for modular architecture boundary violations.';
|
|
|
|
public function handle(ArchitectureScanner $scanner): int
|
|
{
|
|
$violations = $scanner->scan();
|
|
|
|
if ($this->option('format') === 'json') {
|
|
$this->line(json_encode(array_map(fn ($v) => $v->toArray(), $violations), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
|
} else {
|
|
if ($violations === []) {
|
|
$this->info('No architecture violations found. Suspiciously civilized.');
|
|
}
|
|
foreach ($violations as $violation) {
|
|
$this->line(sprintf(
|
|
'[%s] %s %s:%d %s',
|
|
strtoupper($violation->severity),
|
|
$violation->ruleId,
|
|
$violation->file,
|
|
$violation->line,
|
|
$violation->message
|
|
));
|
|
if ($violation->suggestedFix) {
|
|
$this->line(' Fix: '.$violation->suggestedFix);
|
|
}
|
|
}
|
|
}
|
|
|
|
$failOn = (string) $this->option('fail-on');
|
|
if ($failOn === 'none') {
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
foreach ($violations as $violation) {
|
|
if ($violation->severity === 'error' || ($failOn === 'warning' && $violation->severity === 'warning')) {
|
|
return self::FAILURE;
|
|
}
|
|
}
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|