diff --git a/app/Application/Cyclic/CyclicResponseMapper.php b/app/Application/Cyclic/CyclicResponseMapper.php index 6d2c32b..0b1e1e8 100644 --- a/app/Application/Cyclic/CyclicResponseMapper.php +++ b/app/Application/Cyclic/CyclicResponseMapper.php @@ -2,15 +2,16 @@ namespace App\Application\Cyclic; +use App\Domain\Services\Cycle; use App\Application\Cyclic\CyclicResponse; class CyclicResponseMapper { - public function from(array $cycles): CyclicResponse + public function from(Cycle $cycles): CyclicResponse { return new CyclicResponse( - count: count($cycles), - cycles: $cycles, + count: $cycles->count(), + cycles: $cycles->all(), ); } } diff --git a/app/Domain/Aggregators/DependencyAggregator.php b/app/Domain/Aggregators/DependencyAggregator.php index 77aad5a..0e688e7 100644 --- a/app/Domain/Aggregators/DependencyAggregator.php +++ b/app/Domain/Aggregators/DependencyAggregator.php @@ -2,6 +2,7 @@ namespace App\Domain\Aggregators; +use App\Domain\Services\Cycle; use App\Domain\Services\CyclicDependency; use App\Domain\Entities\ClassDependencies; @@ -69,7 +70,7 @@ public function calculateAbstractness(): void } } - public function detectCycles(): array + public function detectCycles(): Cycle { return $this->cyclicDependency->detect($this->classes); } diff --git a/app/Domain/Services/Cycle.php b/app/Domain/Services/Cycle.php new file mode 100644 index 0000000..791d2d8 --- /dev/null +++ b/app/Domain/Services/Cycle.php @@ -0,0 +1,28 @@ +cycles[] = $cycle; + } + + public function isEmpty(): bool + { + return empty($this->cycles); + } + + public function count(): int + { + return count($this->cycles); + } + + public function all(): array + { + return $this->cycles; + } +} diff --git a/app/Domain/Services/CyclicDependency.php b/app/Domain/Services/CyclicDependency.php index dc904ba..60c1281 100644 --- a/app/Domain/Services/CyclicDependency.php +++ b/app/Domain/Services/CyclicDependency.php @@ -3,29 +3,30 @@ namespace App\Domain\Services; use App\Domain\Services\Visited; +use App\Domain\Services\Cycle; +use App\Domain\Services\Stack; class CyclicDependency { public function __construct( private Visited $visited, private Stack $stack, + private Cycle $cycle, ) {} - public function detect(array $classes): array + public function detect(array $classes): Cycle { - $cycles = []; - foreach ($classes as $givenClass => $_) { if ($this->visited->unknown($givenClass)) { - $this->deepDive($givenClass, $classes, $cycles); + $this->deepDive($givenClass, $classes); } } - return $cycles; + return $this->cycle; } - private function deepDive(string $class, array $classes, array &$cycles): void + private function deepDive(string $class, array $classes): void { /** * Duplicate class can exist in the array. @@ -44,7 +45,7 @@ private function deepDive(string $class, array $classes, array &$cycles): void $cycle = $this->stack->extractCycle($class); - $cycles[] = $cycle; + $this->cycle->add($cycle); /** * Stop the recursion, we can switch to another class. @@ -58,7 +59,7 @@ private function deepDive(string $class, array $classes, array &$cycles): void foreach ($classes[$class]->getDependencies() as $dependency) { if (isset($classes[$dependency])) { - $this->deepDive($dependency, $classes, $cycles); + $this->deepDive($dependency, $classes); } } diff --git a/tests/Unit/Domain/Services/CyclicDependencyTest.php b/tests/Unit/Domain/Services/CyclicDependencyTest.php index 2ff28af..e784df8 100644 --- a/tests/Unit/Domain/Services/CyclicDependencyTest.php +++ b/tests/Unit/Domain/Services/CyclicDependencyTest.php @@ -14,7 +14,9 @@ $cycles = app(CyclicDependency::class)->detect($dependencyAggregator->classes()); - expect($cycles)->toBe([ + $this->assertFalse($cycles->isEmpty()); + + expect($cycles->all())->toBe([ ['A', 'B', 'C'], ]); }); @@ -31,7 +33,9 @@ $cycles = app(CyclicDependency::class)->detect($dependencyAggregator->classes()); - expect($cycles)->toBe([ + $this->assertFalse($cycles->isEmpty()); + + expect($cycles->all())->toBe([ ['A', 'B'], ['A', 'C'], ]);