@@ -49,10 +49,10 @@ private function dumpVar(mixed &$var, array $parents = [], int $level = 0, int $
49
49
return $ this ->dumpLiteral ($ var , $ level );
50
50
51
51
} elseif (is_object ($ var )) {
52
- return $ this ->dumpObject ($ var , $ parents , $ level );
52
+ return $ this ->dumpObject ($ var , $ parents , $ level, $ column );
53
53
54
54
} elseif (is_resource ($ var )) {
55
- throw new Nette \InvalidArgumentException ('Cannot dump resource. ' );
55
+ throw new Nette \InvalidStateException ('Cannot dump value of type resource. ' );
56
56
57
57
} else {
58
58
return var_export ($ var , return: true );
@@ -106,7 +106,7 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
106
106
return '[] ' ;
107
107
108
108
} 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. ' );
110
110
}
111
111
112
112
$ space = str_repeat ($ this ->indentation , $ level );
@@ -136,12 +136,25 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
136
136
137
137
138
138
/** @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
140
140
{
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
+
141
145
$ class = $ var ::class;
146
+ $ parents [] = $ var ;
142
147
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
+ );
145
158
146
159
} elseif ($ var instanceof \UnitEnum) {
147
160
return '\\' . $ var ::class . ':: ' . $ var ->name ;
@@ -154,15 +167,25 @@ private function dumpObject(object $var, array $parents, int $level): string
154
167
: implode (':: ' , (array ) $ inner ) . '(...) ' ;
155
168
}
156
169
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
+ }
158
176
159
- } elseif ((new \ReflectionObject ($ var ))->isAnonymous ()) {
160
- throw new Nette \InvalidArgumentException ('Cannot dump anonymous class. ' );
161
177
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. ' );
164
183
}
165
184
185
+ $ class = $ var ::class;
186
+ $ space = str_repeat ($ this ->indentation , $ level );
187
+ $ out = "\n" ;
188
+
166
189
if (method_exists ($ var , '__serialize ' )) {
167
190
$ arr = $ var ->__serialize ();
168
191
} else {
@@ -174,10 +197,6 @@ private function dumpObject(object $var, array $parents, int $level): string
174
197
}
175
198
}
176
199
177
- $ space = str_repeat ($ this ->indentation , $ level );
178
- $ out = "\n" ;
179
- $ parents [] = $ var ;
180
-
181
200
foreach ($ arr as $ k => &$ v ) {
182
201
if (!isset ($ props ) || isset ($ props [$ k ])) {
183
202
$ out .= $ space . $ this ->indentation
@@ -187,11 +206,7 @@ private function dumpObject(object $var, array $parents, int $level): string
187
206
}
188
207
}
189
208
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]) " ;
195
210
}
196
211
197
212
0 commit comments