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

Skip to content

Commit 985a11a

Browse files
[VarDumper] Dont use Stub objects for arrays
1 parent 0763898 commit 985a11a

File tree

4 files changed

+125
-198
lines changed

4 files changed

+125
-198
lines changed

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

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function getValue($recursive = false)
7272
if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {
7373
$item = $item->value;
7474
}
75-
if (!$item instanceof Stub) {
75+
if (!($item = $this->getStub($item)) instanceof Stub) {
7676
return $item;
7777
}
7878
if (Stub::TYPE_STRING === $item->type) {
@@ -82,20 +82,20 @@ public function getValue($recursive = false)
8282
$children = $item->position ? $this->data[$item->position] : array();
8383

8484
foreach ($children as $k => $v) {
85-
if ($recursive && !$v instanceof Stub) {
85+
if ($recursive && !($v = $this->getStub($v)) instanceof Stub) {
8686
continue;
8787
}
8888
$children[$k] = clone $this;
8989
$children[$k]->key = $k;
9090
$children[$k]->position = $item->position;
9191

9292
if ($recursive) {
93-
if ($v instanceof Stub && Stub::TYPE_REF === $v->type && $v->value instanceof Stub) {
93+
if (Stub::TYPE_REF === $v->type && ($v = $this->getStub($v->value)) instanceof Stub) {
9494
$recursive = (array) $recursive;
95-
if (isset($recursive[$v->value->position])) {
95+
if (isset($recursive[$v->position])) {
9696
continue;
9797
}
98-
$recursive[$v->value->position] = true;
98+
$recursive[$v->position] = true;
9999
}
100100
$children[$k] = $children[$k]->getValue($recursive);
101101
}
@@ -123,7 +123,7 @@ public function getIterator()
123123
public function __get($key)
124124
{
125125
if (null !== $data = $this->seek($key)) {
126-
$item = $data->data[$data->position][$data->key];
126+
$item = $this->getStub($data->data[$data->position][$data->key]);
127127

128128
return $item instanceof Stub || array() === $item ? $data : $item;
129129
}
@@ -236,7 +236,7 @@ public function seek($key)
236236
if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {
237237
$item = $item->value;
238238
}
239-
if (!$item instanceof Stub || !$item->position) {
239+
if (!($item = $this->getStub($item)) instanceof Stub || !$item->position) {
240240
return;
241241
}
242242
$keys = array($key);
@@ -288,13 +288,7 @@ public function serialize()
288288
foreach ($data as $i => $values) {
289289
foreach ($values as $k => $v) {
290290
if ($v instanceof Stub) {
291-
if (Stub::TYPE_ARRAY === $v->type) {
292-
$v = self::mapStubConsts($v, false);
293-
$data[$i][$k] = array($v->class, $v->position, $v->cut);
294-
} else {
295-
$v = self::mapStubConsts($v, false);
296-
$data[$i][$k] = array($v->class, $v->position, $v->cut, $v->type, $v->value, $v->handle, $v->refCount, $v->attr);
297-
}
291+
$data[$i][$k] = array($v->class, $v->position, $v->cut, $v->type, $v->value, $v->handle, $v->refCount, $v->attr);
298292
}
299293
}
300294
}
@@ -311,17 +305,10 @@ public function unserialize($serialized)
311305

312306
foreach ($data as $i => $values) {
313307
foreach ($values as $k => $v) {
314-
if ($v && is_array($v)) {
308+
if ($v && is_array($v) && isset($v[7])) {
315309
$s = new Stub();
316-
if (3 === count($v)) {
317-
$s->type = Stub::TYPE_ARRAY;
318-
$s = self::mapStubConsts($s, false);
319-
list($s->class, $s->position, $s->cut) = $v;
320-
$s->value = $s->cut + count($data[$s->position]);
321-
} else {
322-
list($s->class, $s->position, $s->cut, $s->type, $s->value, $s->handle, $s->refCount, $s->attr) = $v;
323-
}
324-
$data[$i][$k] = self::mapStubConsts($s, true);
310+
list($s->class, $s->position, $s->cut, $s->type, $s->value, $s->handle, $s->refCount, $s->attr) = $v;
311+
$data[$i][$k] = $s;
325312
}
326313
}
327314
}
@@ -347,6 +334,9 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
347334
if (!$item instanceof Stub) {
348335
$cursor->attr = array();
349336
$type = gettype($item);
337+
if ($item && 'array' === $type) {
338+
$item = $this->getStub($item);
339+
}
350340
} elseif (Stub::TYPE_REF === $item->type) {
351341
if ($item->handle) {
352342
if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) {
@@ -360,7 +350,7 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
360350
}
361351
$cursor->attr = $item->attr;
362352
$type = $item->class ?: gettype($item->value);
363-
$item = $item->value;
353+
$item = $this->getStub($item->value);
364354
}
365355
if ($item instanceof Stub) {
366356
if ($item->refCount) {
@@ -458,21 +448,19 @@ private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCu
458448
return $hashCut;
459449
}
460450

461-
private static function mapStubConsts(Stub $stub, $resolve)
451+
private function getStub($item)
462452
{
463-
static $stubConstIndexes, $stubConstValues;
464-
465-
if (null === $stubConstIndexes) {
466-
$r = new \ReflectionClass(Stub::class);
467-
$stubConstIndexes = array_flip(array_values($r->getConstants()));
468-
$stubConstValues = array_flip($stubConstIndexes);
453+
if (!$item || $item instanceof Stub || !is_array($item)) {
454+
return $item;
469455
}
470456

471-
$map = $resolve ? $stubConstValues : $stubConstIndexes;
472-
473-
$stub = clone $stub;
474-
$stub->type = $map[$stub->type];
475-
$stub->class = isset($map[$stub->class]) ? $map[$stub->class] : $stub->class;
457+
$stub = new Stub();
458+
$stub->type = Stub::TYPE_ARRAY;
459+
list($stub->class, $stub->position) = $item;
460+
if (isset($item[2])) {
461+
$stub->cut = $item[2];
462+
}
463+
$stub->value = $stub->cut + count($this->data[$stub->position]);
476464

477465
return $stub;
478466
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
*/
1919
class Stub
2020
{
21-
const TYPE_REF = 'ref';
22-
const TYPE_STRING = 'string';
23-
const TYPE_ARRAY = 'array';
24-
const TYPE_OBJECT = 'object';
25-
const TYPE_RESOURCE = 'resource';
21+
const TYPE_REF = 1;
22+
const TYPE_STRING = 2;
23+
const TYPE_ARRAY = 3;
24+
const TYPE_OBJECT = 4;
25+
const TYPE_RESOURCE = 5;
2626

27-
const STRING_BINARY = 'bin';
28-
const STRING_UTF8 = 'utf8';
27+
const STRING_BINARY = 0;
28+
const STRING_UTF8 = 1;
2929

30-
const ARRAY_ASSOC = 'assoc';
31-
const ARRAY_INDEXED = 'indexed';
30+
const ARRAY_ASSOC = 0;
31+
const ARRAY_INDEXED = 1;
3232

3333
public $type = self::TYPE_REF;
3434
public $class = '';

0 commit comments

Comments
 (0)