@@ -49,10 +49,10 @@ private function dumpVar(mixed &$var, array $parents = [], int $level = 0, int $
4949 return $ this ->dumpLiteral ($ var , $ level );
5050
5151 } elseif (is_object ($ var )) {
52- return $ this ->dumpObject ($ var , $ parents , $ level );
52+ return $ this ->dumpObject ($ var , $ parents , $ level, $ column );
5353
5454 } elseif (is_resource ($ var )) {
55- throw new Nette \InvalidArgumentException ('Cannot dump resource. ' );
55+ throw new Nette \InvalidStateException ('Cannot dump value of type resource. ' );
5656
5757 } else {
5858 return var_export ($ var , return: true );
@@ -106,7 +106,7 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
106106 return '[] ' ;
107107
108108 } elseif ($ level > $ this ->maxDepth || in_array ($ var , $ parents , strict: true )) {
109- throw new Nette \InvalidArgumentException ('Nesting level too deep or recursive dependency. ' );
109+ throw new Nette \InvalidStateException ('Nesting level too deep or recursive dependency. ' );
110110 }
111111
112112 $ space = str_repeat ($ this ->indentation , $ level );
@@ -136,12 +136,25 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
136136
137137
138138 /** @param array<mixed[]|object> $parents */
139- private function dumpObject (object $ var , array $ parents , int $ level ): string
139+ private function dumpObject (object $ var , array $ parents , int $ level, int $ column ): string
140140 {
141+ if ($ level > $ this ->maxDepth || in_array ($ var , $ parents , strict: true )) {
142+ throw new Nette \InvalidStateException ('Nesting level too deep or recursive dependency. ' );
143+ }
144+
141145 $ class = $ var ::class;
146+ $ parents [] = $ var ;
142147
143- if (in_array ($ class , [\DateTime::class, \DateTimeImmutable::class], strict: true )) {
144- return $ this ->format ("new \\$ class(?, new \\DateTimeZone(?)) " , $ var ->format ('Y-m-d H:i:s.u ' ), $ var ->getTimeZone ()->getName ());
148+ if ($ class === \stdClass::class) {
149+ $ var = (array ) $ var ;
150+ return '(object) ' . $ this ->dumpArray ($ var , $ parents , $ level , $ column + 10 );
151+
152+ } elseif ($ class === \DateTime::class || $ class === \DateTimeImmutable::class) {
153+ return $ this ->format (
154+ "new \\$ class(?, new \\DateTimeZone(?)) " ,
155+ $ var ->format ('Y-m-d H:i:s.u ' ),
156+ $ var ->getTimeZone ()->getName (),
157+ );
145158
146159 } elseif ($ var instanceof \UnitEnum) {
147160 return '\\' . $ var ::class . ':: ' . $ var ->name ;
@@ -154,15 +167,25 @@ private function dumpObject(object $var, array $parents, int $level): string
154167 : implode (':: ' , (array ) $ inner ) . '(...) ' ;
155168 }
156169
157- throw new Nette \InvalidArgumentException ('Cannot dump closure. ' );
170+ throw new Nette \InvalidStateException ('Cannot dump object of type Closure. ' );
171+
172+ } else {
173+ return $ this ->dumpCustomObject ($ var , $ parents , $ level );
174+ }
175+ }
158176
159- } elseif ((new \ReflectionObject ($ var ))->isAnonymous ()) {
160- throw new Nette \InvalidArgumentException ('Cannot dump anonymous class. ' );
161177
162- } elseif ($ level > $ this ->maxDepth || in_array ($ var , $ parents , strict: true )) {
163- throw new Nette \InvalidArgumentException ('Nesting level too deep or recursive dependency. ' );
178+ /** @param array<mixed[]|object> $parents */
179+ private function dumpCustomObject (object $ var , array $ parents , int $ level ): string
180+ {
181+ if ((new \ReflectionObject ($ var ))->isAnonymous ()) {
182+ throw new Nette \InvalidStateException ('Cannot dump an instance of an anonymous class. ' );
164183 }
165184
185+ $ class = $ var ::class;
186+ $ space = str_repeat ($ this ->indentation , $ level );
187+ $ out = "\n" ;
188+
166189 if (method_exists ($ var , '__serialize ' )) {
167190 $ arr = $ var ->__serialize ();
168191 } else {
@@ -174,10 +197,6 @@ private function dumpObject(object $var, array $parents, int $level): string
174197 }
175198 }
176199
177- $ space = str_repeat ($ this ->indentation , $ level );
178- $ out = "\n" ;
179- $ parents [] = $ var ;
180-
181200 foreach ($ arr as $ k => &$ v ) {
182201 if (!isset ($ props ) || isset ($ props [$ k ])) {
183202 $ out .= $ space . $ this ->indentation
@@ -187,11 +206,7 @@ private function dumpObject(object $var, array $parents, int $level): string
187206 }
188207 }
189208
190- array_pop ($ parents );
191- $ out .= $ space ;
192- return $ class === \stdClass::class
193- ? "(object) [ $ out] "
194- : '\\' . self ::class . "::createObject( \\$ class::class, [ $ out]) " ;
209+ return '\\' . self ::class . "::createObject( \\$ class::class, [ $ out$ space]) " ;
195210 }
196211
197212
0 commit comments