Skip to content

Commit 9d3ceb2

Browse files
committed
Dumper: added support for dumping references
1 parent e3ac591 commit 9d3ceb2

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

src/PhpGenerator/Dumper.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ final class Dumper
2323
public int $wrapLength = 120;
2424
public string $indentation = "\t";
2525
public bool $customObjects = true;
26+
public bool $ref = false;
27+
private array $references;
2628

2729

2830
/**
2931
* Returns a PHP representation of a variable.
3032
*/
3133
public function dump(mixed $var, int $column = 0): string
3234
{
35+
$this->references = [];
3336
return $this->dumpVar($var, [], 0, $column);
3437
}
3538

@@ -122,7 +125,8 @@ private function dumpArray(array $var, array $parents, int $level, int $column):
122125
$res .= $this->dumpVar($v, $parents, $level + 1, $column + strlen($res));
123126

124127
if (
125-
str_contains($res, "\n")
128+
($this->ref && (\ReflectionReference::fromArrayElement($var, $k))?->getId())
129+
|| str_contains($res, "\n")
126130
|| $level * self::IndentLength + $column + strlen($res) + 3 > $this->wrapLength // 3 = [],
127131
) { // wrap
128132
$space = str_repeat($this->indentation, $level);
@@ -131,10 +135,19 @@ private function dumpArray(array $var, array $parents, int $level, int $column):
131135
$keyPart = $hideKeys && ($k !== $keys[0] || $k === 0)
132136
? ''
133137
: $this->dumpVar($k) . ' => ';
134-
$res .= $this->indentation
135-
. $keyPart
136-
. $this->dumpVar($v, $parents, $level + 1, strlen($keyPart))
137-
. ",\n$space";
138+
$res .= $this->indentation . $keyPart;
139+
$ref = $this->ref
140+
? (\ReflectionReference::fromArrayElement($var, $k))?->getId()
141+
: null;
142+
if ($ref !== null) {
143+
if (isset($this->references[$ref])) {
144+
$res .= '&$__ref[' . $this->references[$ref] . "],\n$space";
145+
continue;
146+
} else {
147+
$res .= '$__ref[' . ($this->references[$ref] = count($this->references) + 1) . '] = ';
148+
}
149+
}
150+
$res .= $this->dumpVar($v, $parents, $level + 1, strlen($keyPart)) . ",\n$space";
138151
}
139152
break;
140153
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator\Dumper::dump()
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\PhpGenerator\Dumper;
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
$val = 123;
16+
17+
// no reference
18+
$dumper = new Dumper;
19+
$dumper->ref = false;
20+
Assert::same('[123]', $dumper->dump([&$val]));
21+
22+
23+
// array
24+
$dumper = new Dumper;
25+
$dumper->ref = true;
26+
27+
same('[
28+
$__ref[1] = 123,
29+
]', $dumper->dump([&$val]));
30+
31+
same('[
32+
$__ref[1] = 123,
33+
&$__ref[1],
34+
]', $dumper->dump([&$val, &$val]));
35+
36+
$arr = [1];
37+
$arr[] = &$arr;
38+
same('[
39+
$__ref[1] = 123,
40+
&$__ref[1],
41+
]', $dumper->dump([&$arr]));

0 commit comments

Comments
 (0)