Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b42b03a

Browse files
[VarDumper] Fix return type and anonymous classes dumping
1 parent 80182fa commit b42b03a

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public static function castObject($obj, \ReflectionClass $reflector)
5454
foreach ($p as $i => $k) {
5555
if (!isset($k[0]) || ("\0" !== $k[0] && !$reflector->hasProperty($k))) {
5656
$p[$i] = self::PREFIX_DYNAMIC.$k;
57+
} elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) {
58+
$p[$i] = "\0anonymous-".$reflector->name.strrchr($k, "\0");
5759
}
5860
}
5961
$a = array_combine($p, $a);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
114114
'this' => 'getClosureThis',
115115
));
116116

117+
if (isset($a[$prefix.'returnType'])) {
118+
$a[$prefix.'returnType'] = (string) $a[$prefix.'returnType'];
119+
}
117120
if (isset($a[$prefix.'this'])) {
118121
$a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
119122
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,12 @@ protected function castObject(Stub $stub, $isNested)
208208
$obj = $stub->value;
209209
$class = $stub->class;
210210

211+
if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) {
212+
$class = get_parent_class($class);
213+
$stub->class = 'anonymous-'.$class;
214+
}
211215
if (isset($this->classInfo[$class])) {
212216
$classInfo = $this->classInfo[$class];
213-
$stub->class = $classInfo[0];
214217
} else {
215218
$classInfo = array(
216219
$class,

src/Symfony/Component/VarDumper/Tests/Caster/CasterTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
namespace Symfony\Component\VarDumper\Tests\Caster;
1313

1414
use Symfony\Component\VarDumper\Caster\Caster;
15+
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
1516

1617
/**
1718
* @author Nicolas Grekas <[email protected]>
1819
*/
19-
class CasterTest extends \PHPUnit_Framework_TestCase
20+
class CasterTest extends VarDumperTestCase
2021
{
2122
private $referenceArray = array(
2223
'null' => null,
@@ -28,7 +29,9 @@ class CasterTest extends \PHPUnit_Framework_TestCase
2829
"\0Foo\0private" => 'priv',
2930
);
3031

31-
/** @dataProvider provideFilter */
32+
/**
33+
* @dataProvider provideFilter
34+
*/
3235
public function testFilter($filter, $expectedDiff, $listedProperties = null)
3336
{
3437
if (null === $listedProperties) {
@@ -144,4 +147,21 @@ public function provideFilter()
144147
),
145148
);
146149
}
150+
151+
/**
152+
* @requires PHP 7.0
153+
*/
154+
public function testAnonymousClass()
155+
{
156+
$c = eval('return new class extends stdClass { private $foo = "foo"; };');
157+
158+
$this->assertDumpMatchesFormat(
159+
<<<'EOTXT'
160+
anonymous-stdClass {
161+
-foo: "foo"
162+
}
163+
EOTXT
164+
, $c
165+
);
166+
}
147167
}

src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,25 @@ public function testReflectionCaster()
6060
, $var
6161
);
6262
}
63+
64+
/**
65+
* @requires PHP 7.0
66+
*/
67+
public function testReturnType()
68+
{
69+
$f = eval('return function ():int {};');
70+
71+
$this->assertDumpMatchesFormat(
72+
<<<'EOTXT'
73+
Closure {
74+
returnType: "int"
75+
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
76+
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
77+
file: "%sReflectionCasterTest.php(69) : eval()'d code"
78+
line: "1 to 1"
79+
}
80+
EOTXT
81+
, $f
82+
);
83+
}
6384
}

0 commit comments

Comments
 (0)