From e335c87e025279af10dde66fd62f4d06edd00b89 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Mon, 27 May 2024 08:57:07 +0200 Subject: [PATCH] [VarDumper] Fix generator dump on PHP 8.4 --- Caster/ReflectionCaster.php | 4 +- Tests/Caster/ReflectionCasterTest.php | 82 +++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/Caster/ReflectionCaster.php b/Caster/ReflectionCaster.php index 35fd1e8..87e5ffc 100644 --- a/Caster/ReflectionCaster.php +++ b/Caster/ReflectionCaster.php @@ -83,13 +83,13 @@ public static function castGenerator(\Generator $c, array $a, Stub $stub, bool $ // Cannot create ReflectionGenerator based on a terminated Generator try { $reflectionGenerator = new \ReflectionGenerator($c); + + return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested); } 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, bool $isNested) diff --git a/Tests/Caster/ReflectionCasterTest.php b/Tests/Caster/ReflectionCasterTest.php index e579991..a87fa55 100644 --- a/Tests/Caster/ReflectionCasterTest.php +++ b/Tests/Caster/ReflectionCasterTest.php @@ -500,6 +500,84 @@ class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest" ); } + /** + * @requires PHP < 8.4 + */ + public function testGeneratorPriorTo84() + { + if (\extension_loaded('xdebug')) { + $this->markTestSkipped('xdebug is active'); + } + + $generator = new GeneratorDemo(); + $generator = $generator->baz(); + + $expectedDump = <<<'EODUMP' +Generator { + this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …} + %s: { + %sGeneratorDemo.php:14 { + Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo->baz() + › { + › yield from bar(); + › } + } +%A} + closed: false +} +EODUMP; + + $this->assertDumpMatchesFormat($expectedDump, $generator); + + foreach ($generator as $v) { + break; + } + + $expectedDump = <<<'EODUMP' +array:2 [ + 0 => ReflectionGenerator { + this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …} + %s: { + %s%eTests%eFixtures%eGeneratorDemo.php:%d { + Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo() +%A › yield 1; +%A } + %s%eTests%eFixtures%eGeneratorDemo.php:20 { …} + %s%eTests%eFixtures%eGeneratorDemo.php:14 { …} +%A } + closed: false + } + 1 => Generator { + %s: { + %s%eTests%eFixtures%eGeneratorDemo.php:%d { + Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo() + › yield 1; + › } + › + } +%A } + closed: false + } +] +EODUMP; + + $r = new \ReflectionGenerator($generator); + $this->assertDumpMatchesFormat($expectedDump, [$r, $r->getExecutingGenerator()]); + + foreach ($generator as $v) { + } + + $expectedDump = <<<'EODUMP' +Generator { + closed: true +} +EODUMP; + $this->assertDumpMatchesFormat($expectedDump, $generator); + } + + /** + * @requires PHP 8.4 + */ public function testGenerator() { if (\extension_loaded('xdebug')) { @@ -511,6 +589,7 @@ public function testGenerator() $expectedDump = <<<'EODUMP' Generator { + function: "Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::baz" this: Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo { …} %s: { %sGeneratorDemo.php:14 { @@ -519,6 +598,7 @@ public function testGenerator() › yield from bar(); › } } + Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo->baz() {} %A} closed: false } @@ -545,6 +625,7 @@ public function testGenerator() closed: false } 1 => Generator { + function: "Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo" %s: { %s%eTests%eFixtures%eGeneratorDemo.php:%d { Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::foo() @@ -566,6 +647,7 @@ public function testGenerator() $expectedDump = <<<'EODUMP' Generator { + function: "Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo::baz" closed: true } EODUMP;