Skip to content

Commit

Permalink
added cycle classe for better cohesion
Browse files Browse the repository at this point in the history
  • Loading branch information
DeGraciaMathieu committed Dec 9, 2024
1 parent e64202e commit 9df62d5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
7 changes: 4 additions & 3 deletions app/Application/Cyclic/CyclicResponseMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
}
3 changes: 2 additions & 1 deletion app/Domain/Aggregators/DependencyAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Domain\Aggregators;

use App\Domain\Services\Cycle;
use App\Domain\Services\CyclicDependency;
use App\Domain\Entities\ClassDependencies;

Expand Down Expand Up @@ -69,7 +70,7 @@ public function calculateAbstractness(): void
}
}

public function detectCycles(): array
public function detectCycles(): Cycle
{
return $this->cyclicDependency->detect($this->classes);
}
Expand Down
28 changes: 28 additions & 0 deletions app/Domain/Services/Cycle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Domain\Services;

class Cycle
{
public array $cycles = [];

public function add(array $cycle): void
{
$this->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;
}
}
17 changes: 9 additions & 8 deletions app/Domain/Services/CyclicDependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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);
}
}

Expand Down
8 changes: 6 additions & 2 deletions tests/Unit/Domain/Services/CyclicDependencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
]);
});
Expand All @@ -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'],
]);
Expand Down

0 comments on commit 9df62d5

Please sign in to comment.