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

Skip to content

Commit 1a41265

Browse files
[VarDumper] Fix dumping jsons casted as arrays
1 parent d303b9d commit 1a41265

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ protected function doClone($var)
3535
$objRefs = array(); // Map of original object handles to their stub object couterpart
3636
$resRefs = array(); // Map of original resource handles to their stub object couterpart
3737
$values = array(); // Map of stub objects' hashes to original values
38+
$jsonKeys = array();
3839
$maxItems = $this->maxItems;
3940
$maxString = $this->maxString;
4041
$cookie = (object) array(); // Unique object used to detect hard references
4142
$gid = uniqid(mt_rand(), true); // Unique string used to detect the special $GLOBALS variable
4243
$a = null; // Array cast for nested structures
4344
$stub = null; // Stub capturing the main properties of an original item value
4445
// or null if the original value is used directly
45-
$zval = array( // Main properties of the current value
46+
$zvalProto = $zval = array( // Main properties of the current value
4647
'type' => null,
4748
'zval_isref' => null,
4849
'zval_hash' => null,
@@ -67,7 +68,11 @@ protected function doClone($var)
6768
if ($indexed && $k !== ++$j) {
6869
$indexed = false;
6970
}
70-
if ($useExt) {
71+
if (null === $v ? !array_key_exists($k, $step) : !isset($step[$k])) {
72+
$jsonKeys[$k] = $k;
73+
$zval = $zvalProto;
74+
$zval['type'] = gettype($v);
75+
} elseif ($useExt) {
7176
$zval = symfony_zval_info($k, $step);
7277
} else {
7378
$step[$k] = $cookie;
@@ -253,7 +258,23 @@ protected function doClone($var)
253258
$values[$h] = $v;
254259
}
255260
$queue[$i][$k]->handle = ++$refs;
261+
} else {
262+
unset($jsonKeys[$k]);
263+
}
264+
}
265+
266+
if ($jsonKeys) {
267+
foreach ($queue[$i] as $k => &$v) {
268+
if (isset($jsonKeys[$k])) {
269+
if ($k === $jsonKeys[$k]) {
270+
$v = $queue[$i][$k];
271+
} else {
272+
unset($queue[$i][$k]);
273+
}
274+
}
256275
}
276+
$jsonKeys = array();
277+
unset($v);
257278
}
258279

259280
if (isset($arrayRefs[$i])) {

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,72 @@ public function testClone()
135135
$this->assertStringMatchesFormat($expected, print_r($clone, true));
136136
}
137137

138+
public function testJsonCast()
139+
{
140+
$data = (array) json_decode('{"1":{}}');
141+
142+
$cloner = new VarCloner();
143+
$clone = $cloner->cloneVar($data);
144+
145+
$expected = <<<'EOTXT'
146+
object(Symfony\Component\VarDumper\Cloner\Data)#%i (4) {
147+
["data":"Symfony\Component\VarDumper\Cloner\Data":private]=>
148+
array(2) {
149+
[0]=>
150+
array(1) {
151+
[0]=>
152+
object(Symfony\Component\VarDumper\Cloner\Stub)#%i (7) {
153+
["type"]=>
154+
string(5) "array"
155+
["class"]=>
156+
string(5) "assoc"
157+
["value"]=>
158+
int(1)
159+
["cut"]=>
160+
int(0)
161+
["handle"]=>
162+
int(0)
163+
["refCount"]=>
164+
int(0)
165+
["position"]=>
166+
int(1)
167+
}
168+
}
169+
[1]=>
170+
array(1) {
171+
["1"]=>
172+
object(Symfony\Component\VarDumper\Cloner\Stub)#%i (7) {
173+
["type"]=>
174+
string(6) "object"
175+
["class"]=>
176+
string(8) "stdClass"
177+
["value"]=>
178+
NULL
179+
["cut"]=>
180+
int(0)
181+
["handle"]=>
182+
int(%i)
183+
["refCount"]=>
184+
int(0)
185+
["position"]=>
186+
int(0)
187+
}
188+
}
189+
}
190+
["maxDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
191+
int(20)
192+
["maxItemsPerDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
193+
int(-1)
194+
["useRefHandles":"Symfony\Component\VarDumper\Cloner\Data":private]=>
195+
int(-1)
196+
}
197+
198+
EOTXT;
199+
ob_start();
200+
var_dump($clone);
201+
$this->assertStringMatchesFormat($expected, ob_get_clean());
202+
}
203+
138204
public function testCaster()
139205
{
140206
$cloner = new VarCloner(array(

0 commit comments

Comments
 (0)