8000 [VarDumper] Fixed dumping of terminated generator · symfony/symfony@3ce2bf1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ce2bf1

Browse files
committed
[VarDumper] Fixed dumping of terminated generator
1 parent 6ac1223 commit 3ce2bf1

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,18 @@ public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
7676

7777
public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested)
7878
{
79-
return class_exists('ReflectionGenerator', false) ? self::castReflectionGenerator(new \ReflectionGenerator($c), $a, $stub, $isNested) : $a;
79+
if (!class_exists('ReflectionGenerator', false)) {
80+
return $a;
81+
}
82+
83+
// Cannot create ReflectionGenerator based on a terminated Generator
84+
try {
85+
$reflectionGenerator = new \ReflectionGenerator($c);
86+
} catch (\Exception $e) {
87+
return $a;
88+
}
89+
90+
return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested);
8091
}
8192

8293
public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
@@ -99,28 +110,28 @@ public static function castReflectionGenerator(\ReflectionGenerator $c, array $a
99110
if ($c->getThis()) {
100111
$a[$prefix.'this'] = new CutStub($c->getThis());
101112
}
102-
$x = $c->getFunction();
113+
$function = $c->getFunction();
103114
$frame = array(
104-
'class' => isset($x->class) ? $x->class : null,
105-
'type' => isset($x->class) ? ($x->isStatic() ? '::' : '->') : null,
106-
'function' => $x->name,
115+
'class' => isset($function->class) ? $function->class : null,
116+
'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null,
117+
'function' => $function->name,
107118
'file' => $c->getExecutingFile(),
108119
'line' => $c->getExecutingLine(),
109120
);
110121
if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
111-
$x = new \ReflectionGenerator($c->getExecutingGenerator());
122+
$function = new \ReflectionGenerator($c->getExecutingGenerator());
112123
array_unshift($trace, array(
113124
'function' => 'yield',
114-
'file' => $x->getExecutingFile(),
115-
'line' => $x->getExecutingLine() - 1,
125+
'file' => $function->getExecutingFile(),
126+
'line' => $function->getExecutingLine() - 1,
116127
));
117128
$trace[] = $frame;
118129
$a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
119130
} else {
120-
$x = new FrameStub($frame, false, true);
121-
$x = ExceptionCaster::castFrameStub($x, array(), $x, true);
131+
$function = new FrameStub($frame, false, true);
132+
$function = ExceptionCaster::castFrameStub($function, array(), $function, true);
122133
$a[$prefix.'executing'] = new EnumStub(array(
123-
$frame['class'].$frame['type'].$frame['function'].'()' => $x[$prefix.'src'],
134+
$frame['class'].$frame['type'].$frame['function'].'()' => $function[$prefix.'src'],
124135
));
125136
}
126137

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,10 @@ public function testGenerator()
149149
$this->markTestSkipped('xdebug is active');
150150
}
151151

152-
$g = new GeneratorDemo();
153-
$g = $g->baz();
154-
$r = new \ReflectionGenerator($g);
152+
$generator = new GeneratorDemo();
153+
$generator = $generator->baz();
155154

156-
$xDump = <<<'EODUMP'
155+
$expectedDump = <<<'EODUMP'
157156
Generator {
158157
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
159158
executing: {
@@ -168,13 +167,13 @@ public function testGenerator()
168167
}
169168
EODUMP;
170169

171-
$this->assertDumpMatchesFormat($xDump, $g);
170+
$this->assertDumpMatchesFormat($expectedDump, $generator);
172171

173-
foreach ($g as $v) {
172+
foreach ($generator as $v) {
174173
break;
175174
}
176175

177-
$xDump = <<<'EODUMP'
176+
$expectedDump = <<<'EODUMP'
178177
array:2 [
179178
0 => ReflectionGenerator {
180179
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
@@ -222,7 +221,16 @@ public function testGenerator()
222221
]
223222
EODUMP;
224223

225-
$this->assertDumpMatchesFormat($xDump, array($r, $r->getExecutingGenerator()));
224+
$r = new \ReflectionGenerator($generator);
225+
$this->assertDumpMatchesFormat($expectedDump, array($r, $r->getExecutingGenerator()));
226+
227+
foreach ($generator as $v) {
228+
}
229+
230+
$expectedDump = <<<'EODUMP'
231+
Generator {}
232+
EODUMP;
233+
$this->assertDumpMatchesFormat($expectedDump, $generator);
226234
}
227235
}
228236

0 commit comments

Comments
 (0)
0