1414
1515use function array_filter ;
1616use function array_key_exists ;
17+ use function array_map ;
1718use function array_reduce ;
1819use function array_values ;
1920use function count ;
2021use function current ;
22+ use function end ;
23+ use function explode ;
2124use function is_array ;
2225use function is_string ;
2326use function Respect \Stringifier \stringify ;
@@ -42,11 +45,7 @@ public function main(Result $result, array $templates, Translator $translator):
4245 $ selectedTemplates = $ this ->selectTemplates ($ result , $ templates );
4346 if (!$ this ->isFinalTemplate ($ result , $ selectedTemplates )) {
4447 foreach ($ this ->extractDeduplicatedChildren ($ result ) as $ child ) {
45- if ($ result ->path !== null && $ child ->path !== null && $ child ->path !== $ result ->path ) {
46- $ child = $ child ->withPath ($ result ->path );
47- } elseif ($ result ->path !== null && $ child ->path === null ) {
48- $ child = $ child ->withPath ($ result ->path );
49- }
48+ $ child = $ this ->resultWithPath ($ result , $ child );
5049
5150 return $ this ->main ($ child , $ selectedTemplates , $ translator );
5251 }
@@ -63,32 +62,38 @@ public function full(
6362 array $ templates ,
6463 Translator $ translator ,
6564 int $ depth = 0 ,
66- ?Result $ parent = null ,
6765 Result ...$ siblings
6866 ): string {
6967 $ selectedTemplates = $ this ->selectTemplates ($ result , $ templates );
7068 $ isFinalTemplate = $ this ->isFinalTemplate ($ result , $ selectedTemplates );
7169
7270 $ rendered = '' ;
7371 if ($ this ->isAlwaysVisible ($ result , ...$ siblings ) || $ isFinalTemplate ) {
72+ $ resultToRender = $ result ;
73+ if ($ depth > 0 && $ result ->path !== null ) {
74+ $ paths = explode ('. ' , (string ) $ result ->path );
75+ $ resultToRender ->path = end ($ paths );
76+ }
7477 $ indentation = str_repeat (' ' , $ depth * 2 );
7578 $ rendered .= sprintf (
7679 '%s- %s ' . PHP_EOL ,
7780 $ indentation ,
78- $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
81+ $ this ->renderer ->render ($ this ->getTemplated ($ resultToRender , $ selectedTemplates ), $ translator ),
7982 );
8083 $ depth ++;
8184 }
8285
8386 if (!$ isFinalTemplate ) {
84- $ results = $ this ->extractDeduplicatedChildren ($ result );
87+ $ results = array_map (
88+ fn (Result $ child ) => $ this ->resultWithPath ($ result , $ child ),
89+ $ this ->extractDeduplicatedChildren ($ result )
90+ );
8591 foreach ($ results as $ child ) {
8692 $ rendered .= $ this ->full (
8793 $ child ,
8894 $ selectedTemplates ,
8995 $ translator ,
9096 $ depth ,
91- $ result ,
9297 ...array_filter ($ results , static fn (Result $ sibling ) => $ sibling !== $ child )
9398 );
9499 $ rendered .= PHP_EOL ;
@@ -142,6 +147,19 @@ public function array(Result $result, array $templates, Translator $translator):
142147 return $ messages ;
143148 }
144149
150+ public function resultWithPath (Result $ parent , Result $ child ): Result
151+ {
152+ if ($ parent ->path !== null && $ child ->path !== null && $ child ->path !== $ parent ->path ) {
153+ return $ child ->withPath ($ parent ->path );
154+ }
155+
156+ if ($ parent ->path !== null && $ child ->path === null ) {
157+ return $ child ->withPath ($ parent ->path );
158+ }
159+
160+ return $ child ;
161+ }
162+
145163 private function isAlwaysVisible (Result $ result , Result ...$ siblings ): bool
146164 {
147165 if ($ result ->isValid ) {
0 commit comments