Skip to content

Commit

Permalink
bug #30349 [VarDumper] fix dumping Ds maps and pairs (nicolas-grekas)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Feb 23, 2019
2 parents e9a2c3d + 45869ac commit 2e8bf33
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
50 changes: 34 additions & 16 deletions src/Symfony/Component/VarDumper/Caster/DsCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@

namespace Symfony\Component\VarDumper\Caster;

use Ds\Deque;
use Ds\Collection;
use Ds\Map;
use Ds\PriorityQueue;
use Ds\Queue;
use Ds\Set;
use Ds\Stack;
use Ds\Vector;
use Ds\Pair;
use Symfony\Component\VarDumper\Cloner\Stub;

/**
Expand All @@ -27,23 +23,45 @@
*/
class DsCaster
{
/**
* @param Set|Deque|Vector|Stack|Queue|PriorityQueue $c
*/
public static function castDs($c, array $a, Stub $stub, bool $isNested): array
public static function castCollection(Collection $c, array $a, Stub $stub, bool $isNested): array
{
$prefix = Caster::PREFIX_VIRTUAL;
$a = $c->toArray();
$a[$prefix.'capacity'] = $c->capacity();
$a[Caster::PREFIX_VIRTUAL.'count'] = $c->count();
$a[Caster::PREFIX_VIRTUAL.'capacity'] = $c->capacity();

if (!$c instanceof Map) {
$a += $c->toArray();
}

return $a;
}

public static function castMap(Map $c, array $a, Stub $stub, bool $isNested): array
{
$prefix = Caster::PREFIX_VIRTUAL;
$a = $c->pairs()->toArray();
$a[$prefix.'capacity'] = $c->capacity();
foreach ($c as $k => $v) {
$a[] = new DsPairStub($k, $v);
}

return $a;
}

public static function castPair(Pair $c, array $a, Stub $stub, bool $isNested): array
{
foreach ($c->toArray() as $k => $v) {
$a[Caster::PREFIX_VIRTUAL.$k] = $v;
}

return $a;
}

public static function castPairStub(DsPairStub $c, array $a, Stub $stub, bool $isNested): array
{
if ($isNested) {
$stub->class = Pair::class;
$stub->value = null;
$stub->handle = 0;

$a = $c->value;
}

return $a;
}
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/DsPairStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Caster;

use Symfony\Component\VarDumper\Cloner\Stub;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class DsPairStub extends Stub
{
public function __construct($key, $value)
{
$this->value = [
Caster::PREFIX_VIRTUAL.'key' => $key,
Caster::PREFIX_VIRTUAL.'value' => $value,
];
}
}
9 changes: 3 additions & 6 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,10 @@ abstract class AbstractCloner implements ClonerInterface

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

'Ds\Set' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
'Ds\Vector' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
'Ds\Deque' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
'Ds\Stack' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
'Ds\Queue' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
'Ds\PriorityQueue' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castDs'],
'Ds\Collection' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castCollection'],
'Ds\Map' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castMap'],
'Ds\Pair' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPair'],
'Symfony\Component\VarDumper\Caster\DsPairStub' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPairStub'],

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

0 comments on commit 2e8bf33

Please sign in to comment.