8000 bug #30349 [VarDumper] fix dumping Ds maps and pairs (nicolas-grekas) · symfony/symfony@2e8bf33 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e8bf33

Browse files
committed
bug #30349 [VarDumper] fix dumping Ds maps and pairs (nicolas-grekas)
This PR was merged into the 4.3-dev branch. Discussion ---------- [VarDumper] fix dumping Ds maps and pairs | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Fixes #30311 (comment) ![image](https://user-images.githubusercontent.com/243674/53267273-41260e80-36e3-11e9-8723-a73bf0690a01.png) Commits ------- 45869ac [VarDumper] fix dumping Ds maps and pairs
2 parents e9a2c3d + 45869ac commit 2e8bf33

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

src/Symfony/Component/VarDumper/Caster/DsCaster.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111

1212
namespace Symfony\Component\VarDumper\Caster;
1313

14-
use Ds\Deque;
14+
use Ds\Collection;
1515
use Ds\Map;
16-
use Ds\PriorityQueue;
17-
use Ds\Queue;
18-
use Ds\Set;
19-
use Ds\Stack;
20-
use Ds\Vector;
16+
use Ds\Pair;
2117
use Symfony\Component\VarDumper\Cloner\Stub;
2218

2319
/**
@@ -27,23 +23,45 @@
2723
*/
2824
class DsCaster
2925
{
30-
/**
31-
* @param Set|Deque|Vector|Stack|Queue|PriorityQueue $c
32-
*/
33-
public static function castDs($c, array $a, Stub $stub, bool $isNested): array
26+
public static function castCollection(Collection $c, array $a, Stub $stub, bool $isNested): array
3427
{
35-
$prefix = Caster::PREFIX_VIRTUAL;
36-
$a = $c->toArray();
37-
$a[$prefix.'capacity'] = $c->capacity();
28+
$a[Caster::PREFIX_VIRTUAL.'count'] = $c->count();
29+
$a[Caster::PREFIX_VIRTUAL.'capacity'] = $c->capacity();
30+
31+
if (!$c instanceof Map) {
32+
$a += $c->toArray();
33+
}
3834

3935
return $a;
4036
}
4137

4238
public static function castMap(Map $c, array $a, Stub $stub, bool $isNested): array
4339
{
44-
$prefix = Caster::PREFIX_VIRTUAL;
45-
$a = $c->pairs()->toArray();
46-
$a[$prefix.'capacity'] = $c->capacity();
40+
foreach ($c as $k => $v) {
41+
$a[] = new DsPairStub($k, $v);
42+
}
43+
44+
return $a;
45+
}
46+
47+
public static function castPair(Pair $c, array $a, Stub $stub, bool $isNested): array
48+
{
49+
foreach ($c->toArray() as $k => $v) {
50+
$a[Caster::PREFIX_VIRTUAL.$k] = $v;
51+
}
52+
53+
return $a;
54+
}
55+
56+
public static function castPairStub(DsPairStub $c, array $a, Stub $stub, bool $isNested): array
57+
{
58+
if ($isNested) {
59+
$stub->class = Pair::class;
60+
$stub->value = null;
61+
$stub->handle = 0;
62+
63+
$a = $c->value;
64+
}
4765

4866
return $a;
4967
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
use Symfony\Component\VarDumper\Cloner\Stub;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class DsPairStub extends Stub
20+
{
21+
public function __construct($key, $value)
22+
{
23+
$this->value = [
24+
Caster::PREFIX_VIRTUAL.'key' => $key,
25+
Caster::PREFIX_VIRTUAL.'value' => $value,
26+
];
27+
}
28+
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,10 @@ abstract class AbstractCloner implements ClonerInterface
125125

126126
'Memcached' => ['Symfony\Component\VarDumper\Caster\MemcachedCaster', 'castMemcached'],
127127

128-
'Ds\Set' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
129-
'Ds\Vector' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
130-
'Ds\Deque' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
131-
'Ds\Stack' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
132-
'Ds\Queue' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
133-
'Ds\PriorityQueue' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
128+
'Ds\Collection' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castCollection'],
134129
'Ds\Map' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castMap'],
130+
'Ds\Pair' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPair'],
131+
'Symfony\Component\VarDumper\Caster\DsPairStub' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPairStub'],
135132

136133
':curl' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'],
137134
':dba' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'],

0 commit comments

Comments
 (0)
0