[VarDumper] Fixed dumping of terminated generator

This commit is contained in:
Grégoire Pineau 2017-02-06 13:22:16 +01:00
parent 013e4053f7
commit c5094a05a4
2 changed files with 47 additions and 19 deletions

View File

@ -76,7 +76,20 @@ class ReflectionCaster
public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested) public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested)
{ {
return class_exists('ReflectionGenerator', false) ? self::castReflectionGenerator(new \ReflectionGenerator($c), $a, $stub, $isNested) : $a; if (!class_exists('ReflectionGenerator', false)) {
return $a;
}
// Cannot create ReflectionGenerator based on a terminated Generator
try {
$reflectionGenerator = new \ReflectionGenerator($c);
} catch (\Exception $e) {
$a[Caster::PREFIX_VIRTUAL.'closed'] = true;
return $a;
}
return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested);
} }
public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested) public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
@ -99,31 +112,33 @@ class ReflectionCaster
if ($c->getThis()) { if ($c->getThis()) {
$a[$prefix.'this'] = new CutStub($c->getThis()); $a[$prefix.'this'] = new CutStub($c->getThis());
} }
$x = $c->getFunction(); $function = $c->getFunction();
$frame = array( $frame = array(
'class' => isset($x->class) ? $x->class : null, 'class' => isset($function->class) ? $function->class : null,
'type' => isset($x->class) ? ($x->isStatic() ? '::' : '->') : null, 'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null,
'function' => $x->name, 'function' => $function->name,
'file' => $c->getExecutingFile(), 'file' => $c->getExecutingFile(),
'line' => $c->getExecutingLine(), 'line' => $c->getExecutingLine(),
); );
if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) { if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
$x = new \ReflectionGenerator($c->getExecutingGenerator()); $function = new \ReflectionGenerator($c->getExecutingGenerator());
array_unshift($trace, array( array_unshift($trace, array(
'function' => 'yield', 'function' => 'yield',
'file' => $x->getExecutingFile(), 'file' => $function->getExecutingFile(),
'line' => $x->getExecutingLine() - 1, 'line' => $function->getExecutingLine() - 1,
)); ));
$trace[] = $frame; $trace[] = $frame;
$a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1); $a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
} else { } else {
$x = new FrameStub($frame, false, true); $function = new FrameStub($frame, false, true);
$x = ExceptionCaster::castFrameStub($x, array(), $x, true); $function = ExceptionCaster::castFrameStub($function, array(), $function, true);
$a[$prefix.'executing'] = new EnumStub(array( $a[$prefix.'executing'] = new EnumStub(array(
$frame['class'].$frame['type'].$frame['function'].'()' => $x[$prefix.'src'], $frame['class'].$frame['type'].$frame['function'].'()' => $function[$prefix.'src'],
)); ));
} }
$a[Caster::PREFIX_VIRTUAL.'closed'] = false;
return $a; return $a;
} }

View File

@ -149,11 +149,10 @@ EOTXT
$this->markTestSkipped('xdebug is active'); $this->markTestSkipped('xdebug is active');
} }
$g = new GeneratorDemo(); $generator = new GeneratorDemo();
$g = $g->baz(); $generator = $generator->baz();
$r = new \ReflectionGenerator($g);
$xDump = <<<'EODUMP' $expectedDump = <<<'EODUMP'
Generator { Generator {
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { } this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { }
executing: { executing: {
@ -165,16 +164,17 @@ Generator {
""" """
} }
} }
closed: false
} }
EODUMP; EODUMP;
$this->assertDumpMatchesFormat($xDump, $g); $this->assertDumpMatchesFormat($expectedDump, $generator);
foreach ($g as $v) { foreach ($generator as $v) {
break; break;
} }
$xDump = <<<'EODUMP' $expectedDump = <<<'EODUMP'
array:2 [ array:2 [
0 => ReflectionGenerator { 0 => ReflectionGenerator {
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { } this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { }
@ -207,6 +207,7 @@ array:2 [
} }
} }
} }
closed: false
} }
1 => Generator { 1 => Generator {
executing: { executing: {
@ -218,11 +219,23 @@ array:2 [
""" """
} }
} }
closed: false
} }
] ]
EODUMP; EODUMP;
$this->assertDumpMatchesFormat($xDump, array($r, $r->getExecutingGenerator())); $r = new \ReflectionGenerator($generator);
$this->assertDumpMatchesFormat($expectedDump, array($r, $r->getExecutingGenerator()));
foreach ($generator as $v) {
}
$expectedDump = <<<'EODUMP'
Generator {
closed: true
}
EODUMP;
$this->assertDumpMatchesFormat($expectedDump, $generator);
} }
} }