13
13
14
14
15
15
/**
16
- * PHP code generator utils .
16
+ * Generates a PHP representation of a variable .
17
17
*/
18
18
final class Dumper
19
19
{
@@ -35,7 +35,7 @@ public function dump(mixed $var, int $column = 0): string
35
35
36
36
37
37
/** @param array<mixed[]|object> $parents */
38
- private function dumpVar (mixed & $ var , array $ parents = [], int $ level = 0 , int $ column = 0 ): string
38
+ private function dumpVar (mixed $ var , array $ parents = [], int $ level = 0 , int $ column = 0 ): string
39
39
{
40
40
if ($ var === null ) {
41
41
return 'null ' ;
@@ -101,7 +101,7 @@ private static function utf8Ord(string $c): int
101
101
* @param mixed[] $var
102
102
* @param array<mixed[]|object> $parents
103
103
*/
104
- private function dumpArray (array & $ var , array $ parents , int $ level , int $ column ): string
104
+ private function dumpArray (array $ var , array $ parents , int $ level , int $ column ): string
105
105
{
106
106
if (empty ($ var )) {
107
107
return '[] ' ;
@@ -114,7 +114,7 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
114
114
$ hideKeys = is_int (($ keys = array_keys ($ var ))[0 ]) && $ keys === range ($ keys [0 ], $ keys [0 ] + count ($ var ) - 1 );
115
115
$ pairs = [];
116
116
117
- foreach ($ var as $ k => & $ v ) {
117
+ foreach ($ var as $ k => $ v ) {
118
118
$ keyPart = $ hideKeys && ($ k !== $ keys [0 ] || $ k === 0 )
119
119
? ''
120
120
: $ this ->dumpVar ($ k ) . ' => ' ;
@@ -134,6 +134,8 @@ private function dumpObject(object $var, array $parents, int $level, int $column
134
134
{
135
135
if ($ level > $ this ->maxDepth || in_array ($ var , $ parents , strict: true )) {
136
136
throw new Nette \InvalidStateException ('Nesting level too deep or recursive dependency. ' );
137
+ } elseif ((new \ReflectionObject ($ var ))->isAnonymous ()) {
138
+ throw new Nette \InvalidStateException ('Cannot dump an instance of an anonymous class. ' );
137
139
}
138
140
139
141
$ class = $ var ::class;
@@ -175,10 +177,6 @@ private function dumpObject(object $var, array $parents, int $level, int $column
175
177
/** @param array<mixed[]|object> $parents */
176
178
private function dumpCustomObject (object $ var , array $ parents , int $ level ): string
177
179
{
178
- if ((new \ReflectionObject ($ var ))->isAnonymous ()) {
179
- throw new Nette \InvalidStateException ('Cannot dump an instance of an anonymous class. ' );
180
- }
181
-
182
180
$ class = $ var ::class;
183
181
$ space = str_repeat ($ this ->indentation , $ level );
184
182
$ out = "\n" ;
@@ -194,7 +192,7 @@ private function dumpCustomObject(object $var, array $parents, int $level): stri
194
192
}
195
193
}
196
194
197
- foreach ($ arr as $ k => & $ v ) {
195
+ foreach ($ arr as $ k => $ v ) {
198
196
if (!isset ($ props ) || isset ($ props [$ k ])) {
199
197
$ out .= $ space . $ this ->indentation
200
198
. ($ keyPart = $ this ->dumpVar ($ k ) . ' => ' )
0 commit comments