Skip to content

Commit

Permalink
[VarDumper] Fix return type and anonymous classes dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Oct 19, 2015
1 parent 80182fa commit b42b03a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/Caster.php
Expand Up @@ -54,6 +54,8 @@ public static function castObject($obj, \ReflectionClass $reflector)
foreach ($p as $i => $k) {
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");
}
}
$a = array_combine($p, $a);
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
Expand Up @@ -114,6 +114,9 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
'this' => 'getClosureThis',
));

if (isset($a[$prefix.'returnType'])) {
$a[$prefix.'returnType'] = (string) $a[$prefix.'returnType'];
}
if (isset($a[$prefix.'this'])) {
$a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Expand Up @@ -208,9 +208,12 @@ protected function castObject(Stub $stub, $isNested)
$obj = $stub->value;
$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;
}
if (isset($this->classInfo[$class])) {
$classInfo = $this->classInfo[$class];
$stub->class = $classInfo[0];
} else {
$classInfo = array(
$class,
Expand Down
24 changes: 22 additions & 2 deletions src/Symfony/Component/VarDumper/Tests/Caster/CasterTest.php
Expand Up @@ -12,11 +12,12 @@
namespace Symfony\Component\VarDumper\Tests\Caster;

use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Test\VarDumperTestCase;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class CasterTest extends \PHPUnit_Framework_TestCase
class CasterTest extends VarDumperTestCase
{
private $referenceArray = array(
'null' => null,
Expand All @@ -28,7 +29,9 @@ class CasterTest extends \PHPUnit_Framework_TestCase
"\0Foo\0private" => 'priv',
);

/** @dataProvider provideFilter */
/**
* @dataProvider provideFilter
*/
public function testFilter($filter, $expectedDiff, $listedProperties = null)
{
if (null === $listedProperties) {
Expand Down Expand Up @@ -144,4 +147,21 @@ public function provideFilter()
),
);
}

/**
* @requires PHP 7.0
*/
public function testAnonymousClass()
{
$c = eval('return new class extends stdClass { private $foo = "foo"; };');

$this->assertDumpMatchesFormat(
<<<'EOTXT'
anonymous-stdClass {
-foo: "foo"
}
EOTXT
, $c
);
}
}
Expand Up @@ -60,4 +60,25 @@ public function testReflectionCaster()
, $var
);
}

/**
* @requires PHP 7.0
*/
public function testReturnType()
{
$f = eval('return function ():int {};');

$this->assertDumpMatchesFormat(
<<<'EOTXT'
Closure {
returnType: "int"
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
file: "%sReflectionCasterTest.php(69) : eval()'d code"
line: "1 to 1"
}
EOTXT
, $f
);
}
}

0 comments on commit b42b03a

Please sign in to comment.