Skip to content

Commit 17bd93e

Browse files
[VarDumper] Casters for Generator, ReflectionGenerator and ReflectionType
1 parent 87f83f7 commit 17bd93e

File tree

9 files changed

+185
-32
lines changed

9 files changed

+185
-32
lines changed

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
9090

9191
$a = array();
9292
$j = count($frames);
93-
if (0 > $i = $trace->offset) {
93+
if (0 > $i = $trace->sliceOffset) {
9494
$i = max(0, $j + $i);
9595
}
9696
if (!isset($trace->value[$i])) {
9797
return array();
9898
}
9999
$lastCall = isset($frames[$i]['function']) ? ' ==> '.(isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
100100

101-
for ($j -= $i++; isset($frames[$i]); ++$i, --$j) {
101+
for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
102102
$call = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[$i]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '???';
103103

104104
$a[Caster::PREFIX_VIRTUAL.$j.'. '.$call.$lastCall] = new FrameStub(
@@ -126,8 +126,8 @@ public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $is
126126
$trace->keepArgs,
127127
true
128128
);
129-
if (null !== $trace->length) {
130-
$a = array_slice($a, 0, $trace->length, true);
129+
if (null !== $trace->sliceLength) {
130+
$a = array_slice($a, 0, $trace->sliceLength, true);
131131
}
132132

133133
return $a;
@@ -247,15 +247,29 @@ private static function filterExceptionArray($xClass, array $a, $xPrefix, $filte
247247

248248
private static function extractSource(array $srcArray, $line, $srcContext)
249249
{
250-
$src = '';
250+
$src = array();
251251

252252
for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
253-
$src .= (isset($srcArray[$i]) ? $srcArray[$i] : '')."\n";
253+
$src[] = (isset($srcArray[$i]) ? $srcArray[$i] : '')."\n";
254254
}
255-
if (!$srcContext) {
256-
$src = trim($src);
255+
256+
$ltrim = 0;
257+
while (' ' === $src[0][$ltrim] || "\t" === $src[0][$ltrim]) {
258+
$i = $srcContext << 1;
259+
while ($i > 0 && $src[0][$ltrim] === $src[$i][$ltrim]) {
260+
--$i;
261+
}
262+
if ($i) {
263+
break;
264+
}
265+
++$ltrim;
266+
}
267+
if ($ltrim) {
268+
foreach ($src as $i => $line) {
269+
$src[$i] = substr($line, $ltrim);
270+
}
257271
}
258272

259-
return $src;
273+
return implode('', $src);
260274
}
261275
}

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,51 @@ public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
7474
return $a;
7575
}
7676

77+
public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested)
78+
{
79+
return self::castReflectionGenerator(new \ReflectionGenerator($c), $a, $stub, $isNested);
80+
}
81+
82+
public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
83+
{
84+
$prefix = Caster::PREFIX_VIRTUAL;
85+
86+
$a += array(
87+
$prefix.'type' => $c->__toString(),
88+
$prefix.'allowsNull' => $c->allowsNull(),
89+
$prefix.'isBuiltin' => $c->isBuiltin(),
90+
);
91+
92+
return $a;
93+
}
94+
95+
public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
96+
{
97+
$prefix = Caster::PREFIX_VIRTUAL;
98+
99+
if ($c->getThis()) {
100+
$a[$prefix.'this'] = new CutStub($c->getThis());
101+
}
102+
$trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS);
103+
$x = new \ReflectionGenerator($c->getExecutingGenerator());
104+
array_unshift($trace, array(
105+
'function' => 'yield',
106+
'file' => $x->getExecutingFile(),
107+
'line' => $x->getExecutingLine() - 1,
108+
));
109+
$f = $c->getFunction();
110+
$trace[] = array(
111+
'class' => isset($f->class) ? $f->class : null,
112+
'type' => isset($f->class) ? ($f->isStatic() ? '::' : '->') : null,
113+
'function' => $f->name,
114+
'file' => $c->getExecutingFile(),
115+
'line' => $c->getExecutingLine(),
116+
);
117+
$a[$prefix.'trace'] = new TraceStub($trace, 1, false, 0, -1, -1);
118+
119+
return $a;
120+
}
121+
77122
public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
78123
{
79124
$prefix = Caster::PREFIX_VIRTUAL;
@@ -241,7 +286,7 @@ private static function addExtra(&$a, \Reflector $c)
241286
}
242287
}
243288

244-
private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
289+
private static function addMap(&$a, $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
245290
{
246291
foreach ($map as $k => $m) {
247292
if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static function castEnum(EnumStub $c, array $a, Stub $stub, $isNested)
5454
if ($isNested) {
5555
$stub->class = '';
5656
$stub->handle = 0;
57+
$stub->value = null;
5758

5859
$a = array();
5960

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ class TraceStub extends Stub
2222
{
2323
public $srcContext;
2424
public $keepArgs;
25-
public $offset;
26-
public $length;
25+
public $sliceOffset;
26+
public $sliceLength;
27+
public $numberingOffset;
2728

28-
public function __construct(array $trace, $srcContext = 1, $keepArgs = true, $offset = 0, $length = null)
29+
public function __construct(array $trace, $srcContext = 1, $keepArgs = true, $sliceOffset = 0, $sliceLength = null, $numberingOffset = 0)
2930
{
3031
$this->value = $trace;
3132
$this->srcContext = $srcContext;
3233
$this->keepArgs = $keepArgs;
33-
$this->offset = $offset;
34-
$this->length = $length;
34+
$this->sliceOffset = $sliceOffset;
35+
$this->sliceLength = $sliceLength;
36+
$this->numberingOffset = $numberingOffset;
3537
}
3638
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ abstract class AbstractCloner implements ClonerInterface
2828
'Symfony\Component\VarDumper\Caster\EnumStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castEnum',
2929

3030
'Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure',
31+
'Generator' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castGenerator',
32+
'ReflectionType' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castType',
33+
'ReflectionGenerator' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castReflectionGenerator',
3134
'ReflectionClass' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClass',
3235
'ReflectionFunctionAbstract' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castFunctionAbstract',
3336
'ReflectionMethod' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castMethod',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ protected function doClone($var)
149149
$stub->handle = $h;
150150
$a = $this->castObject($stub, 0 < $i);
151151
if ($v !== $stub->value) {
152-
if (Stub::TYPE_OBJECT !== $stub->type) {
152+
if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
153153
break;
154154
}
155155
if ($useExt) {

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\VarDumper\Tests\Caster;
1313

1414
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
15+
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
1516

1617
/**
1718
* @author Nicolas Grekas <p@tchwork.com>
@@ -72,10 +73,76 @@ public function testClosureCaster()
7273
\$b: & 123
7374
}
7475
file: "%sReflectionCasterTest.php"
75-
line: "62 to 62"
76+
line: "63 to 63"
7677
}
7778
EOTXT
7879
, $var
7980
);
8081
}
82+
83+
/**
84+
* @requires PHP 7.0
85+
*/
86+
public function testGenerator()
87+
{
88+
$g = new GeneratorDemo();
89+
$g = $g->baz();
90+
$r = new \ReflectionGenerator($g);
91+
92+
foreach ($g as $v) {
93+
break;
94+
}
95+
96+
$xDump = <<<'EODUMP'
97+
array:2 [
98+
0 => ReflectionGenerator {
99+
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
100+
trace: {
101+
3. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo() ==> yield(): {
102+
src: {
103+
%sGeneratorDemo.php:9: """
104+
{\n
105+
yield 1;\n
106+
}\n
107+
"""
108+
}
109+
}
110+
2. Symfony\Component\VarDumper\Tests\Fixtures\bar() ==> Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo(): {
111+
src: {
112+
%sGeneratorDemo.php:20: """
113+
{\n
114+
yield from GeneratorDemo::foo();\n
115+
}\n
116+
"""
117+
}
118+
}
119+
1. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo->baz() ==> Symfony\Component\VarDumper\Tests\Fixtures\bar(): {
120+
src: {
121+
%sGeneratorDemo.php:14: """
122+
{\n
123+
yield from bar();\n
124+
}\n
125+
"""
126+
}
127+
}
128+
}
129+
}
130+
1 => Generator {
131+
trace: {
132+
1. Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo() ==> yield(): {
133+
src: {
134+
%sGeneratorDemo.php:9: """
135+
{\n
136+
yield 1;\n
137+
}\n
138+
"""
139+
}
140+
}
141+
}
142+
}
143+
]
144+
EODUMP;
145+
146+
$this->assertDumpMatchesFormat($xDump, array($r, $r->getExecutingGenerator()));
147+
}
81148
}

src/Symfony/Component/VarDumper/Tests/CliDumperTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,45 +232,45 @@ public function testThrowingCaster()
232232
%d. __TwigTemplate_VarDumperFixture_u75a09->doDisplay() ==> new Exception(): {
233233
src: {
234234
%sTwig.php:19: """
235-
// line 2\\n
236-
throw new \Exception('Foobar');\\n
237-
}\\n
235+
// line 2\\n
236+
throw new \Exception('Foobar');\\n
237+
}\\n
238238
"""
239239
{$twig} }
240240
}
241241
%d. Twig_Template->displayWithErrorHandling() ==> __TwigTemplate_VarDumperFixture_u75a09->doDisplay(): {
242242
src: {
243243
%sTemplate.php:%d: """
244-
try {\\n
245-
\$this->doDisplay(\$context, \$blocks);\\n
246-
} catch (Twig_Error \$e) {\\n
244+
try {\\n
245+
\$this->doDisplay(\$context, \$blocks);\\n
246+
} catch (Twig_Error \$e) {\\n
247247
"""
248248
}
249249
}
250250
%d. Twig_Template->display() ==> Twig_Template->displayWithErrorHandling(): {
251251
src: {
252252
%sTemplate.php:%d: """
253-
{\\n
254-
\$this->displayWithErrorHandling(\$this->env->mergeGlobals(\$context), array_merge(\$this->blocks, \$blocks));\\n
255-
}\\n
253+
{\\n
254+
\$this->displayWithErrorHandling(\$this->env->mergeGlobals(\$context), array_merge(\$this->blocks, \$blocks));\\n
255+
}\\n
256256
"""
257257
}
258258
}
259259
%d. Twig_Template->render() ==> Twig_Template->display(): {
260260
src: {
261261
%sTemplate.php:%d: """
262-
try {\\n
263-
\$this->display(\$context);\\n
264-
} catch (Exception \$e) {\\n
262+
try {\\n
263+
\$this->display(\$context);\\n
264+
} catch (Exception \$e) {\\n
265265
"""
266266
}
267267
}
268268
%d. %slosure%s() ==> Twig_Template->render(): {
269269
src: {
270270
%sCliDumperTest.php:{$line}: """
271-
}\\n
272-
};'),\\n
273-
));\\n
271+
}\\n
272+
};'),\\n
273+
));\\n
274274
"""
275275
}
276276
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
4+
5+
class GeneratorDemo
6+
{
7+
public static function foo()
8+
{
9+
yield 1;
10+
}
11+
12+
public function baz()
13+
{
14+
yield from bar();
15+
}
16+
}
17+
18+
function bar()
19+
{
20+
yield from GeneratorDemo::foo();
21+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy