File tree Expand file tree Collapse file tree 5 files changed +68
-7
lines changed
Expand file tree Collapse file tree 5 files changed +68
-7
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 77});
88
99test ('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
1313test ('it keeps short names ' , function () {
Original file line number Diff line number Diff line change 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+ });
You can’t perform that action at this time.
0 commit comments