From edd5633374857e31d57c391bea88b8d4d343d55c Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 6 Nov 2015 09:22:03 +0100 Subject: [PATCH] [VarDumper] Fix PHP7 type-hints compat --- .../VarDumper/Caster/ReflectionCaster.php | 6 ++++- .../Tests/Caster/ReflectionCasterTest.php | 25 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php index a302835f082d9..cacd2113d7694 100644 --- a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php @@ -167,7 +167,11 @@ public static function castParameter(\ReflectionParameter $c, array $a, Stub $st )); try { - if ($c->isArray()) { + if (method_exists($c, 'hasType')) { + if ($c->hasType()) { + $a[$prefix.'typeHint'] = $c->getType()->__toString(); + } + } elseif ($c->isArray()) { $a[$prefix.'typeHint'] = 'array'; } elseif (method_exists($c, 'isCallable') && $c->isCallable()) { $a[$prefix.'typeHint'] = 'callable'; diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php index 4120d341daecf..5c306a671085c 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php @@ -78,20 +78,41 @@ public function testReflectionParameter() ); } + /** + * @requires PHP 7.0 + */ + public function testReflectionParameterScalar() + { + $f = eval('return function (int $a) {};'); + $var = new \ReflectionParameter($f, 0); + + $this->assertDumpMatchesFormat( + <<<'EOTXT' +ReflectionParameter { + +name: "a" + position: 0 + typeHint: "int" +} +EOTXT + , $var + ); + } + /** * @requires PHP 7.0 */ public function testReturnType() { $f = eval('return function ():int {};'); + $line = __LINE__ - 1; $this->assertDumpMatchesFormat( - <<<'EOTXT' + <<