diff --git a/src/CLI/Runner.php b/src/CLI/Runner.php index 5a870baf..47858ff1 100644 --- a/src/CLI/Runner.php +++ b/src/CLI/Runner.php @@ -10,21 +10,23 @@ use Arkitect\Analyzer\Parser; use Arkitect\ClassSetRules; use Arkitect\CLI\Progress\Progress; +use Arkitect\Exceptions\FailOnFirstViolationException; use Arkitect\Rules\ParsingErrors; use Arkitect\Rules\Violations; use Symfony\Component\Finder\SplFileInfo; class Runner { - /** @var Violations */ - private $violations; + private Violations $violations; - /** @var ParsingErrors */ - private $parsingErrors; + private ParsingErrors $parsingErrors; + + private bool $stopOnFailure; public function __construct(bool $stopOnFailure = false) { - $this->violations = new Violations($stopOnFailure); + $this->stopOnFailure = $stopOnFailure; + $this->violations = new Violations(); $this->parsingErrors = new ParsingErrors(); } @@ -57,6 +59,8 @@ public function check( ): void { /** @var SplFileInfo $file */ foreach ($classSetRule->getClassSet() as $file) { + $fileViolations = new Violations(); + if (!$onlyErrors) { $progress->startParsingFile($file->getRelativePathname()); } @@ -71,9 +75,18 @@ public function check( /** @var ClassDescription $classDescription */ foreach ($fileParser->getClassDescriptions() as $classDescription) { foreach ($classSetRule->getRules() as $rule) { - $rule->check($classDescription, $violations); + $rule->check($classDescription, $fileViolations); + + if ($this->stopOnFailure && $fileViolations->count() > 0) { + $violations->merge($fileViolations); + + throw new FailOnFirstViolationException(); + } } } + + $violations->merge($fileViolations); + if (!$onlyErrors) { $progress->endParsingFile($file->getRelativePathname()); } diff --git a/src/Rules/Violation.php b/src/Rules/Violation.php index dd1bfb8f..3f149390 100644 --- a/src/Rules/Violation.php +++ b/src/Rules/Violation.php @@ -6,14 +6,11 @@ class Violation implements \JsonSerializable { - /** @var string */ - private $fqcn; + private string $fqcn; - /** @var int|null */ - private $line; + private ?int $line; - /** @var string */ - private $error; + private string $error; public function __construct(string $fqcn, string $error, ?int $line = null) { diff --git a/src/Rules/Violations.php b/src/Rules/Violations.php index 42105b2d..6289565f 100644 --- a/src/Rules/Violations.php +++ b/src/Rules/Violations.php @@ -5,7 +5,6 @@ namespace Arkitect\Rules; use Arkitect\CLI\Printer\PrinterFactory; -use Arkitect\Exceptions\FailOnFirstViolationException; use Arkitect\Exceptions\IndexNotFoundException; /** @@ -16,24 +15,18 @@ class Violations implements \IteratorAggregate, \Countable, \JsonSerializable /** * @var Violation[] */ - private $violations; + private array $violations; - /** - * @var bool - */ - private $stopOnFailure; - - public function __construct(bool $stopOnFailure = false) + public function __construct() { $this->violations = []; - $this->stopOnFailure = $stopOnFailure; } public static function fromJson(string $json): self { $json = json_decode($json, true); - $instance = new self($json['stopOnFailure']); + $instance = new self(); $instance->violations = array_map(function (array $json): Violation { return Violation::fromJson($json); @@ -45,9 +38,11 @@ public static function fromJson(string $json): self public function add(Violation $violation): void { $this->violations[] = $violation; - if ($this->stopOnFailure) { - throw new FailOnFirstViolationException(); - } + } + + public function merge(self $other): void + { + $this->violations = array_merge($this->violations, $other->toArray()); } public function get(int $index): Violation