bug #16338 [VarDumper] Fix anonymous class dumping (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[VarDumper] Fix anonymous class dumping

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

Follows a comment from @stof

Commits
-------

65da4d5 [VarDumper] Fix anonymous class dumping
This commit is contained in:
Fabien Potencier 2015-10-27 08:06:34 -07:00
commit f042197e10
3 changed files with 16 additions and 7 deletions

View File

@ -55,7 +55,7 @@ class Caster
if (!isset($k[0]) || ("\0" !== $k[0] && !$reflector->hasProperty($k))) {
$p[$i] = self::PREFIX_DYNAMIC.$k;
} elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) {
$p[$i] = "\0anonymous-".$reflector->name.strrchr($k, "\0");
$p[$i] = "\0".$reflector->getParentClass().'@anonymous'.strrchr($k, "\0");
}
}
$a = array_combine($p, $a);

View File

@ -209,14 +209,12 @@ abstract class AbstractCloner implements ClonerInterface
$class = $stub->class;
if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) {
$class = get_parent_class($class);
$stub->class = 'anonymous-'.$class;
$stub->class = get_parent_class($class).'@anonymous';
}
if (isset($this->classInfo[$class])) {
$classInfo = $this->classInfo[$class];
} else {
$classInfo = array(
$class,
new \ReflectionClass($class),
array_reverse(array($class => $class) + class_parents($class) + class_implements($class) + array('*' => '*')),
);
@ -224,9 +222,9 @@ abstract class AbstractCloner implements ClonerInterface
$this->classInfo[$class] = $classInfo;
}
$a = $this->callCaster('Symfony\Component\VarDumper\Caster\Caster::castObject', $obj, $classInfo[1], null, $isNested);
$a = $this->callCaster('Symfony\Component\VarDumper\Caster\Caster::castObject', $obj, $classInfo[0], null, $isNested);
foreach ($classInfo[2] as $p) {
foreach ($classInfo[1] as $p) {
if (!empty($this->casters[$p = strtolower($p)])) {
foreach ($this->casters[$p] as $p) {
$a = $this->callCaster($p, $obj, $a, $stub, $isNested);

View File

@ -157,7 +157,18 @@ class CasterTest extends VarDumperTestCase
$this->assertDumpMatchesFormat(
<<<'EOTXT'
anonymous-stdClass {
stdClass@anonymous {
-foo: "foo"
}
EOTXT
, $c
);
$c = eval('return new class { private $foo = "foo"; };');
$this->assertDumpMatchesFormat(
<<<'EOTXT'
@anonymous {
-foo: "foo"
}
EOTXT