bug #13785 [VarDumper] Workaround stringy numeric keys (nicolas-grekas)

This PR was merged into the 2.6 branch.

Discussion
----------

[VarDumper] Workaround stringy numeric keys

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13784
| License       | MIT
| Doc PR        | -

Commits
-------

46bea3a [VarDumper] Workaround stringy numeric keys
This commit is contained in:
Fabien Potencier 2015-02-25 09:36:14 +01:00
commit 70ba475156
2 changed files with 100 additions and 4 deletions

View File

@ -199,15 +199,17 @@ abstract class AbstractCloner implements ClonerInterface
}
if ($classInfo[1]) {
$a = $this->callCaster(function ($obj) {return $obj->__debugInfo();}, $obj, array(), null, $isNested);
$p = $this->callCaster(function ($obj) {return $obj->__debugInfo();}, $obj, array(), null, $isNested);
} else {
$a = (array) $obj;
$p = (array) $obj;
}
foreach ($a as $k => $p) {
$a = array();
foreach ($p as $k => $p) {
if (!isset($k[0]) || ("\0" !== $k[0] && !$classInfo[2]->hasProperty($k))) {
unset($a[$k]);
$a["\0+\0".$k] = $p;
} else {
$a[$k] = $p;
}
}

View File

@ -0,0 +1,94 @@
<?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\Tests;
use Symfony\Component\VarDumper\Cloner\VarCloner;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class VarClonerTest extends \PHPUnit_Framework_TestCase
{
public function testClone()
{
$json = json_decode('{"1":{"var":"val"},"2":{"var":"val"}}');
$cloner = new VarCloner();
$clone = $cloner->cloneVar($json);
$expected = <<<EOTXT
Symfony\Component\VarDumper\Cloner\Data Object
(
[data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
(
[0] => Array
(
[0] => Symfony\Component\VarDumper\Cloner\Stub Object
(
[type] => object
[class] => stdClass
[value] =>
[cut] => 0
[handle] => %d
[refCount] => 0
[position] => 1
)
)
[1] => Array
(
[\000+\0001] => Symfony\Component\VarDumper\Cloner\Stub Object
(
[type] => object
[class] => stdClass
[value] =>
[cut] => 0
[handle] => %d
[refCount] => 0
[position] => 2
)
[\000+\0002] => Symfony\Component\VarDumper\Cloner\Stub Object
(
[type] => object
[class] => stdClass
[value] =>
[cut] => 0
[handle] => %d
[refCount] => 0
[position] => 3
)
)
[2] => Array
(
[\000+\000var] => val
)
[3] => Array
(
[\000+\000var] => val
)
)
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
)
EOTXT;
$this->assertStringMatchesFormat($expected, print_r($clone, true));
}
}