Skip to content

Commit 07606ab

Browse files
Added CycleHelper
1 parent e5e0e30 commit 07606ab

File tree

5 files changed

+68
-7
lines changed

5 files changed

+68
-7
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Presenter\Commands\Cyclic\Summary;
4+
5+
use App\Presenter\Commands\Shared\NameFormatter;
6+
7+
class CycleHelper
8+
{
9+
public static function through(array $classNames): string
10+
{
11+
$readableNames = self::convertToReadableNames($classNames);
12+
13+
$path = self::joinWithArrows($readableNames);
14+
15+
return self::completeCircularPath($path, $classNames);
16+
}
17+
18+
private static function convertToReadableNames(array $classNames): array
19+
{
20+
return array_map(function ($className) {
21+
return NameFormatter::humanReadable($className);
22+
}, $classNames);
23+
}
24+
25+
private static function joinWithArrows(array $names): string
26+
{
27+
return implode(' -> ', $names);
28+
}
29+
30+
private static function completeCircularPath(string $path, array $names): string
31+
{
32+
return $path . ' -> ' . NameFormatter::humanReadable($names[0]);
33+
}
34+
}

app/Presenter/Commands/Cyclic/Summary/CyclicPresenterMapper.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ public static function from(array $cycles): array
1010
{
1111
return array_map(function (array $cycle) {
1212
return [
13-
'start' => NameFormatter::humanReadable($cycle[0]),
14-
'end' => NameFormatter::humanReadable(end($cycle)),
15-
'through' => implode(' -> ', array_map(function($item) {
16-
return NameFormatter::className($item);
17-
}, $cycle)) . ' -> ' . NameFormatter::className($cycle[0]),
13+
'start' => self::humanReadable($cycle[0]),
14+
'end' => self::humanReadable(end($cycle)),
15+
'through' => CycleHelper::through($cycle),
1816
];
1917
}, $cycles);
2018
}
19+
20+
private static function humanReadable(string $name): string
21+
{
22+
return NameFormatter::humanReadable($name);
23+
}
2124
}

app/Presenter/Commands/Shared/NameFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function humanReadable(string $className): string
2222

2323
$exploded = self::keepLastTwoNamespaces($exploded);
2424

25-
return '...\\' . $exploded->implode('\\');
25+
return '\\' . $exploded->implode('\\');
2626
}
2727

2828
private static function isShortName(Collection $exploded): bool

tests/Unit/Formatters/NameFormatterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
});
88

99
test('it formats human readable names', function () {
10-
expect(NameFormatter::humanReadable('App\Domain\Services\CyclicDependency'))->toBe('...\\Services\\CyclicDependency');
10+
expect(NameFormatter::humanReadable('App\Domain\Services\CyclicDependency'))->toBe('\\Services\\CyclicDependency');
1111
});
1212

1313
test('it keeps short names', function () {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use App\Presenter\Commands\Cyclic\Summary\CycleHelper;
4+
5+
test('it formats a cycle', function () {
6+
7+
$cycle = ['A', 'B', 'C'];
8+
9+
$formatted = CycleHelper::through($cycle);
10+
11+
expect($formatted)->toBe('A -> B -> C -> A');
12+
});
13+
14+
test('it formats a cycle with readable names', function () {
15+
16+
$cycle = [
17+
'App\Domain\Services\BarService',
18+
'App\Domain\Services\FooService',
19+
];
20+
21+
$formatted = CycleHelper::through($cycle);
22+
23+
expect($formatted)->toBe('\Services\BarService -> \Services\FooService -> \Services\BarService');
24+
});

0 commit comments

Comments
 (0)