bug #14114 [VarDumper] Fix dumping references as properties (nicolas-grekas)

This PR was merged into the 2.6 branch.

Discussion
----------

[VarDumper] Fix dumping references as properties

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Just discovered this while reviewing an other PR.
Using `array_combine()` preserves references now.

Commits
-------

6c6560e [VarDumper] Fix dumping references as properties
This commit is contained in:
Fabien Potencier 2015-03-31 10:15:34 +02:00
commit 29e153ebdf
2 changed files with 37 additions and 8 deletions

View File

@ -199,18 +199,19 @@ abstract class AbstractCloner implements ClonerInterface
} }
if ($classInfo[1]) { if ($classInfo[1]) {
$p = $this->callCaster(function ($obj) {return $obj->__debugInfo();}, $obj, array(), null, $isNested); $a = $this->callCaster(function ($obj) {return $obj->__debugInfo();}, $obj, array(), null, $isNested);
} else { } else {
$p = (array) $obj; $a = (array) $obj;
} }
$a = array(); if ($a) {
foreach ($p as $k => $p) { $p = array_keys($a);
if (!isset($k[0]) || ("\0" !== $k[0] && !$classInfo[2]->hasProperty($k))) { foreach ($p as $i => $k) {
$a["\0+\0".$k] = $p; if (!isset($k[0]) || ("\0" !== $k[0] && !$classInfo[2]->hasProperty($k))) {
} else { $p[$i] = "\0+\0".$k;
$a[$k] = $p; }
} }
$a = array_combine($p, $a);
} }
foreach ($classInfo[3] as $p) { foreach ($classInfo[3] as $p) {

View File

@ -154,6 +154,34 @@ EOTXT
} }
} }
EOTXT
,
$out
);
}
public function testRefsInProperties()
{
$var = (object) array('foo' => 'foo');
$var->bar =& $var->foo;
$dumper = new CliDumper();
$dumper->setColors(false);
$cloner = new VarCloner();
$out = fopen('php://memory', 'r+b');
$data = $cloner->cloneVar($var);
$dumper->dump($data, $out);
rewind($out);
$out = stream_get_contents($out);
$this->assertStringMatchesFormat(
<<<EOTXT
{#%d
+"foo": &1 "foo"
+"bar": &1 "foo"
}
EOTXT EOTXT
, ,
$out $out