Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[VarDumper] Fixed dumping of terminated generator #21542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions 37 src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,20 @@ public static function castClosure(\Closure $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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closed: true/false virtual prop?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I know?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call ->valid()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can not because it alters the generator. That's why I used the try catch method and not ->valid().

}

// Cannot create ReflectionGenerator based on a terminated Generator
try {
$reflectionGenerator = new \ReflectionGenerator($c);
} catch (\Exception $e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm that an Exception is thrown here, even-though I'd personally have expected a ReflectionException
ping @jpauli.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been fixed as part of PHP 7.2 in php/php-src@ed4216c

$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)
Expand All @@ -99,31 +112,33 @@ public static function castReflectionGenerator(\ReflectionGenerator $c, array $a
if ($c->getThis()) {
$a[$prefix.'this'] = new CutStub($c->getThis());
}
$x = $c->getFunction();
$function = $c->getFunction();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff irrelevant for review

$frame = array(
'class' => isset($x->class) ? $x->class : null,
'type' => isset($x->class) ? ($x->isStatic() ? '::' : '->') : null,
'function' => $x->name,
'class' => isset($function->class) ? $function->class : null,
'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null,
'function' => $function->name,
'file' => $c->getExecutingFile(),
'line' => $c->getExecutingLine(),
);
if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
$x = new \ReflectionGenerator($c->getExecutingGenerator());
$function = new \ReflectionGenerator($c->getExecutingGenerator());
array_unshift($trace, array(
'function' => 'yield',
'file' => $x->getExecutingFile(),
'line' => $x->getExecutingLine() - 1,
'file' => $function->getExecutingFile(),
'line' => $function->getExecutingLine() - 1,
));
$trace[] = $frame;
$a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
} else {
$x = new FrameStub($frame, false, true);
$x = ExceptionCaster::castFrameStub($x, array(), $x, true);
$function = new FrameStub($frame, false, true);
$function = ExceptionCaster::castFrameStub($function, array(), $function, true);
$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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,10 @@ public function testGenerator()
$this->markTestSkipped('xdebug is active');
}

$g = new GeneratorDemo();
$g = $g->baz();
$r = new \ReflectionGenerator($g);
$generator = new GeneratorDemo();
$generator = $generator->baz();

$xDump = <<<'EODUMP'
$expectedDump = <<<'EODUMP'
Generator {
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
executing: {
Expand All @@ -165,16 +164,17 @@ public function testGenerator()
"""
}
}
closed: false
}
EODUMP;

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

foreach ($g as $v) {
foreach ($generator as $v) {
break;
}

$xDump = <<<'EODUMP'
$expectedDump = <<<'EODUMP'
array:2 [
0 => ReflectionGenerator {
this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …}
Expand Down Expand Up @@ -207,6 +207,7 @@ public function testGenerator()
}
}
}
closed: false
}
1 => Generator {
executing: {
Expand All @@ -218,11 +219,23 @@ public function testGenerator()
"""
}
}
closed: false
}
]
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);
}
}

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.