Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29e153e

Browse files
committed
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
2 parents 2c4b5e5 + 6c6560e commit 29e153e

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,19 @@ protected function castObject(Stub $stub, $isNested)
199199
}
200200

201201
if ($classInfo[1]) {
202-
$p = $this->callCaster(function ($obj) {return $obj->__debugInfo();}, $obj, array(), null, $isNested);
202+
$a = $this->callCaster(function ($obj) {return $obj->__debugInfo();}, $obj, array(), null, $isNested);
203203
} else {
204-
$p = (array) $obj;
204+
$a = (array) $obj;
205205
}
206206

207-
$a = array();
208-
foreach ($p as $k => $p) {
209-
if (!isset($k[0]) || ("\0" !== $k[0] && !$classInfo[2]->hasProperty($k))) {
210-
$a["\0+\0".$k] = $p;
211-
} else {
212-
$a[$k] = $p;
207+
if ($a) {
208+
$p = array_keys($a);
209+
foreach ($p as $i => $k) {
210+
if (!isset($k[0]) || ("\0" !== $k[0] && !$classInfo[2]->hasProperty($k))) {
211+
$p[$i] = "\0+\0".$k;
212+
}
213213
}
214+
$a = array_combine($p, $a);
214215
}
215216

216217
foreach ($classInfo[3] as $p) {

src/Symfony/Component/VarDumper/Tests/CliDumperTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ public function testThrowingCaster()
154154
}
155155
}
156156
157+
EOTXT
158+
,
159+
$out
160+
);
161+
}
162+
163+
public function testRefsInProperties()
164+
{
165+
$var = (object) array('foo' => 'foo');
166+
$var->bar =& $var->foo;
167+
168+
$dumper = new CliDumper();
169+
$dumper->setColors(false);
170+
$cloner = new VarCloner();
171+
172+
$out = fopen('php://memory', 'r+b');
173+
$data = $cloner->cloneVar($var);
174+
$dumper->dump($data, $out);
175+
rewind($out);
176+
$out = stream_get_contents($out);
177+
178+
$this->assertStringMatchesFormat(
179+
<<<EOTXT
180+
{#%d
181+
+"foo": &1 "foo"
182+
+"bar": &1 "foo"
183+
}
184+
157185
EOTXT
158186
,
159187
$out

0 commit comments

Comments
 (0)