60 lines
2.0 KiB
PHP
60 lines
2.0 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Support\Architecture;
|
|
|
|
final readonly class ArchitectureScanner
|
|
{
|
|
public function __construct(
|
|
private ArchitectureRuleRegistry $registry,
|
|
private ArchitectureFileFinder $finder,
|
|
) {}
|
|
|
|
/** @return ArchitectureViolation[] */
|
|
public function scan(): array
|
|
{
|
|
$violations = [];
|
|
foreach ($this->registry->rules() as $rule) {
|
|
foreach ($this->finder->phpFiles($rule->paths) as $file) {
|
|
$relative = str_replace(base_path().'/', '', $file);
|
|
if ($this->isExcluded($relative, $rule->excludePaths)) {
|
|
continue;
|
|
}
|
|
$lines = file($file, FILE_IGNORE_NEW_LINES) ?: [];
|
|
foreach ($lines as $index => $line) {
|
|
foreach ($rule->patterns as $pattern) {
|
|
if (@preg_match($pattern, '') === false) {
|
|
if (! str_contains($line, $pattern)) {
|
|
continue;
|
|
}
|
|
} elseif (! preg_match($pattern, $line)) {
|
|
continue;
|
|
}
|
|
|
|
$violations[] = new ArchitectureViolation(
|
|
ruleId: $rule->id,
|
|
severity: $rule->severity,
|
|
file: $relative,
|
|
line: $index + 1,
|
|
message: $rule->message,
|
|
suggestedFix: $rule->suggestedFix,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $violations;
|
|
}
|
|
|
|
/** @param string[] $excludePaths */
|
|
private function isExcluded(string $relativePath, array $excludePaths): bool
|
|
{
|
|
foreach ($excludePaths as $exclude) {
|
|
if (str_contains($relativePath, $exclude)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|