@@ -23,13 +23,16 @@ final class Dumper
2323 public int $ wrapLength = 120 ;
2424 public string $ indentation = "\t" ;
2525 public bool $ customObjects = true ;
26+ public bool $ ref = false ;
27+ private array $ references ;
2628
2729
2830 /**
2931 * Returns a PHP representation of a variable.
3032 */
3133 public function dump (mixed $ var , int $ column = 0 ): string
3234 {
35+ $ this ->references = [];
3336 return $ this ->dumpVar ($ var , [], 0 , $ column );
3437 }
3538
@@ -122,7 +125,8 @@ private function dumpArray(array $var, array $parents, int $level, int $column):
122125 $ res .= $ this ->dumpVar ($ v , $ parents , $ level + 1 , $ column + strlen ($ res ));
123126
124127 if (
125- str_contains ($ res , "\n" )
128+ ($ this ->ref && (\ReflectionReference::fromArrayElement ($ var , $ k ))?->getId())
129+ || str_contains ($ res , "\n" )
126130 || $ level * self ::IndentLength + $ column + strlen ($ res ) + 3 > $ this ->wrapLength // 3 = [],
127131 ) { // wrap
128132 $ space = str_repeat ($ this ->indentation , $ level );
@@ -131,10 +135,19 @@ private function dumpArray(array $var, array $parents, int $level, int $column):
131135 $ keyPart = $ hideKeys && ($ k !== $ keys [0 ] || $ k === 0 )
132136 ? ''
133137 : $ this ->dumpVar ($ k ) . ' => ' ;
134- $ res .= $ this ->indentation
135- . $ keyPart
136- . $ this ->dumpVar ($ v , $ parents , $ level + 1 , strlen ($ keyPart ))
137- . ", \n$ space " ;
138+ $ res .= $ this ->indentation . $ keyPart ;
139+ $ ref = $ this ->ref
140+ ? (\ReflectionReference::fromArrayElement ($ var , $ k ))?->getId()
141+ : null ;
142+ if ($ ref !== null ) {
143+ if (isset ($ this ->references [$ ref ])) {
144+ $ res .= '&$__ref[ ' . $ this ->references [$ ref ] . "], \n$ space " ;
145+ continue ;
146+ } else {
147+ $ res .= '$__ref[ ' . ($ this ->references [$ ref ] = count ($ this ->references ) + 1 ) . '] = ' ;
148+ }
149+ }
150+ $ res .= $ this ->dumpVar ($ v , $ parents , $ level + 1 , strlen ($ keyPart )) . ", \n$ space " ;
138151 }
139152 break ;
140153 }
0 commit comments