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

Skip to content

Commit c346f2a

Browse files
bug #21542 [VarDumper] Fixed dumping of terminated generator (lyrixx)
This PR was merged into the 2.8 branch. Discussion ---------- [VarDumper] Fixed dumping of terminated generator | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- c5094a0 [VarDumper] Fixed dumping of terminated generator
2 parents 013e405 + c5094a0 commit c346f2a

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,20 @@ 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+
$a[Caster::PREFIX_VIRTUAL.'closed'] = true;
88+
89+
return $a;
90+
}
91+
92+
return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested);
8093
}
8194

8295
public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
@@ -99,31 +112,33 @@ public static function castReflectionGenerator(\ReflectionGenerator $c, array $a
99112
if ($c->getThis()) {
100113
$a[$prefix.'this'] = new CutStub($c->getThis());
101114
}
102-
$x = $c->getFunction();
115+
$function = $c->getFunction();
103116
$frame = array(
104-
'class' => isset($x->class) ? $x->class : null,
105-
'type' => isset($x->class) ? ($x->isStatic() ? '::' : '->') : null,
106-
'function' => $x->name,
117+
'class' => isset($function->class) ? $function->class : null,
118+
'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null,
119+
'function' => $function->name,
107120
'file' => $c->getExecutingFile(),
108121
'line' => $c->getExecutingLine(),
109122
);
110123
if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
111-
$x = new \ReflectionGenerator($c->getExecutingGenerator());
124+
$function = new \ReflectionGenerator($c->getExecutingGenerator());
112125
array_unshift($trace, array(
113126
'function' => 'yield',
114-
'file' => $x->getExecutingFile(),
115-
'line' => $x->getExecutingLine() - 1,
127+
'file' => $function->getExecutingFile(),
128+
'line' => $function->getExecutingLine() - 1,
116129
));
117130
$trace[] = $frame;
118131
$a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
119132
} else {
120-
$x = new FrameStub($frame, false, true);
121-
$x = ExceptionCaster::castFrameStub($x, array(), $x, true);
133+
$function = new FrameStub($frame, false, true);
134+
$function = ExceptionCaster::castFrameStub($function, array(), $function, true);
122135
$a[$prefix.'executing'] = new EnumStub(array(
123-
$frame['class'].$frame['type'].$frame['function'].'()' => $x[$prefix.'src'],
136+
$frame['class'].$frame['type'].$frame['function'].'()' => $function[$prefix.'src'],
124137
));
125138
}
126139

140+
$a[Caster::PREFIX_VIRTUAL.'closed'] = false;
141+
127142
return $a;
128143
}
129144

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

Lines changed: 21 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: {
@@ -165,16 +164,17 @@ public function testGenerator()
165164
"""
166165
}
167166
}
167+
closed: false
168168
}
169169
EODUMP;
170170

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

173-
foreach ($g as $v) {
173+
foreach ($generator as $v) {
174174
break;
175175
}
176176

177-
$xDump = <<<'EODUMP'
177+
$expectedDump = <<<'EODUMP'
178178
array:2 [
179179
0 => ReflectionGenerator {
180180
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
@@ -207,6 +207,7 @@ public function testGenerator()
207207
}
208208
}
209209
}
210+
closed: false
210211
}
211212
1 => Generator {
212213
executing: {
@@ -218,11 +219,23 @@ public function testGenerator()
218219
"""
219220
}
220221
}
222+
closed: false
221223
}
222224
]
223225
EODUMP;
224226

225-
$this->assertDumpMatchesFormat($xDump, array($r, $r->getExecutingGenerator()));
227+
$r = new \ReflectionGenerator($generator);
228+
$this->assertDumpMatchesFormat($expectedDump, array($r, $r->getExecutingGenerator()));
229+
230+
foreach ($generator as $v) {
231+
}
232+
233+
$expectedDump = <<<'EODUMP'
234+
Generator {
235+
closed: true
236+
}
237+
EODUMP;
238+
$this->assertDumpMatchesFormat($expectedDump, $generator);
226239
}
227240
}
228241

0 commit comments

Comments
 (0)