Skip to content

Commit dec0711

Browse files
committed
Dumper: refactoring
1 parent d281b08 commit dec0711

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/PhpGenerator/Dumper.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
/**
16-
* PHP code generator utils.
16+
* Generates a PHP representation of a variable.
1717
*/
1818
final class Dumper
1919
{
@@ -35,7 +35,7 @@ public function dump(mixed $var, int $column = 0): string
3535

3636

3737
/** @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
3939
{
4040
if ($var === null) {
4141
return 'null';
@@ -101,7 +101,7 @@ private static function utf8Ord(string $c): int
101101
* @param mixed[] $var
102102
* @param array<mixed[]|object> $parents
103103
*/
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
105105
{
106106
if (empty($var)) {
107107
return '[]';
@@ -114,7 +114,7 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
114114
$hideKeys = is_int(($keys = array_keys($var))[0]) && $keys === range($keys[0], $keys[0] + count($var) - 1);
115115
$pairs = [];
116116

117-
foreach ($var as $k => &$v) {
117+
foreach ($var as $k => $v) {
118118
$keyPart = $hideKeys && ($k !== $keys[0] || $k === 0)
119119
? ''
120120
: $this->dumpVar($k) . ' => ';
@@ -134,6 +134,8 @@ private function dumpObject(object $var, array $parents, int $level, int $column
134134
{
135135
if ($level > $this->maxDepth || in_array($var, $parents, strict: true)) {
136136
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.');
137139
}
138140

139141
$class = $var::class;
@@ -175,10 +177,6 @@ private function dumpObject(object $var, array $parents, int $level, int $column
175177
/** @param array<mixed[]|object> $parents */
176178
private function dumpCustomObject(object $var, array $parents, int $level): string
177179
{
178-
if ((new \ReflectionObject($var))->isAnonymous()) {
179-
throw new Nette\InvalidStateException('Cannot dump an instance of an anonymous class.');
180-
}
181-
182180
$class = $var::class;
183181
$space = str_repeat($this->indentation, $level);
184182
$out = "\n";
@@ -194,7 +192,7 @@ private function dumpCustomObject(object $var, array $parents, int $level): stri
194192
}
195193
}
196194

197-
foreach ($arr as $k => &$v) {
195+
foreach ($arr as $k => $v) {
198196
if (!isset($props) || isset($props[$k])) {
199197
$out .= $space . $this->indentation
200198
. ($keyPart = $this->dumpVar($k) . ' => ')

0 commit comments

Comments
 (0)