Skip to content

Commit 5bbeb1c

Browse files
committed
Dumper: detection of recursive objects is done via non-static argument $parents
1 parent 15be6e8 commit 5bbeb1c

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/PhpGenerator/Dumper.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function dump($var): string
3535
}
3636

3737

38-
private function dumpVar(&$var, int $level = 0): string
38+
private function dumpVar(&$var, array $parents = [], int $level = 0): string
3939
{
4040
if ($var instanceof PhpLiteral) {
4141
return (string) $var;
@@ -47,10 +47,10 @@ private function dumpVar(&$var, int $level = 0): string
4747
return $this->dumpString($var);
4848

4949
} elseif (is_array($var)) {
50-
return $this->dumpArray($var, $level);
50+
return $this->dumpArray($var, $parents, $level);
5151

5252
} elseif (is_object($var)) {
53-
return $this->dumpObject($var, $level);
53+
return $this->dumpObject($var, $parents, $level);
5454

5555
} elseif (is_resource($var)) {
5656
throw new Nette\InvalidArgumentException('Cannot dump resource.');
@@ -83,7 +83,7 @@ private function dumpString(string $var): string
8383
}
8484

8585

86-
private function dumpArray(array &$var, int $level): string
86+
private function dumpArray(array &$var, array $parents, int $level): string
8787
{
8888
static $marker;
8989
if ($marker === null) {
@@ -104,7 +104,7 @@ private function dumpArray(array &$var, int $level): string
104104

105105
foreach ($var as $k => &$v) {
106106
if ($k !== $marker) {
107-
$item = ($k === $counter ? '' : $this->dumpVar($k, $level + 1) . ' => ') . $this->dumpVar($v, $level + 1);
107+
$item = ($k === $counter ? '' : $this->dumpVar($k) . ' => ') . $this->dumpVar($v, $parents, $level + 1);
108108
$counter = is_int($k) ? max($k + 1, $counter) : $counter;
109109
$outInline .= ($outInline === '' ? '' : ', ') . $item;
110110
$outWrapped .= "\t$item,\n$space";
@@ -117,7 +117,7 @@ private function dumpArray(array &$var, int $level): string
117117
}
118118

119119

120-
private function dumpObject(&$var, int $level): string
120+
private function dumpObject(&$var, array $parents, int $level): string
121121
{
122122
if ($var instanceof \Serializable) {
123123
return 'unserialize(' . $this->dumpString(serialize($var)) . ')';
@@ -137,13 +137,12 @@ private function dumpObject(&$var, int $level): string
137137
$arr = (array) $var;
138138
$space = str_repeat("\t", $level);
139139

140-
static $list = [];
141-
if ($level > $this->maxDepth || in_array($var, $list, true)) {
140+
if ($level > $this->maxDepth || in_array($var, $parents ?? [], true)) {
142141
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
143142
}
144143

145144
$out = "\n";
146-
$list[] = $var;
145+
$parents[] = $var;
147146
if (method_exists($var, '__sleep')) {
148147
foreach ($var->__sleep() as $v) {
149148
$props[$v] = $props["\x00*\x00$v"] = $props["\x00$class\x00$v"] = true;
@@ -152,11 +151,11 @@ private function dumpObject(&$var, int $level): string
152151

153152
foreach ($arr as $k => &$v) {
154153
if (!isset($props) || isset($props[$k])) {
155-
$out .= "$space\t" . $this->dumpVar($k, $level + 1) . ' => ' . $this->dumpVar($v, $level + 1) . ",\n";
154+
$out .= "$space\t" . $this->dumpVar($k) . ' => ' . $this->dumpVar($v, $parents, $level + 1) . ",\n";
156155
}
157156
}
158157

159-
array_pop($list);
158+
array_pop($parents);
160159
$out .= $space;
161160
return $class === 'stdClass'
162161
? "(object) [$out]"

0 commit comments

Comments
 (0)