[VarDumper] Fix display of nullable union return types.
This commit is contained in:
parent
018e33ab2d
commit
efeb2dcf70
@ -183,7 +183,7 @@ class ReflectionCaster
|
|||||||
if (isset($a[$prefix.'returnType'])) {
|
if (isset($a[$prefix.'returnType'])) {
|
||||||
$v = $a[$prefix.'returnType'];
|
$v = $a[$prefix.'returnType'];
|
||||||
$v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
|
$v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
|
||||||
$a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType']->allowsNull() && 'mixed' !== $v ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
|
$a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType'] instanceof \ReflectionNamedType && $a[$prefix.'returnType']->allowsNull() && 'mixed' !== $v ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
|
||||||
}
|
}
|
||||||
if (isset($a[$prefix.'class'])) {
|
if (isset($a[$prefix.'class'])) {
|
||||||
$a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
|
$a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
|
||||||
|
@ -149,6 +149,68 @@ EOTXT
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 8
|
||||||
|
*/
|
||||||
|
public function testReflectionParameterMixed()
|
||||||
|
{
|
||||||
|
$f = eval('return function (mixed $a) {};');
|
||||||
|
$var = new \ReflectionParameter($f, 0);
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat(
|
||||||
|
<<<'EOTXT'
|
||||||
|
ReflectionParameter {
|
||||||
|
+name: "a"
|
||||||
|
position: 0
|
||||||
|
allowsNull: true
|
||||||
|
typeHint: "mixed"
|
||||||
|
}
|
||||||
|
EOTXT
|
||||||
|
, $var
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 8
|
||||||
|
*/
|
||||||
|
public function testReflectionParameterUnion()
|
||||||
|
{
|
||||||
|
$f = eval('return function (int|float $a) {};');
|
||||||
|
$var = new \ReflectionParameter($f, 0);
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat(
|
||||||
|
<<<'EOTXT'
|
||||||
|
ReflectionParameter {
|
||||||
|
+name: "a"
|
||||||
|
position: 0
|
||||||
|
typeHint: "int|float"
|
||||||
|
}
|
||||||
|
EOTXT
|
||||||
|
, $var
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 8
|
||||||
|
*/
|
||||||
|
public function testReflectionParameterNullableUnion()
|
||||||
|
{
|
||||||
|
$f = eval('return function (int|float|null $a) {};');
|
||||||
|
$var = new \ReflectionParameter($f, 0);
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat(
|
||||||
|
<<<'EOTXT'
|
||||||
|
ReflectionParameter {
|
||||||
|
+name: "a"
|
||||||
|
position: 0
|
||||||
|
allowsNull: true
|
||||||
|
typeHint: "int|float|null"
|
||||||
|
}
|
||||||
|
EOTXT
|
||||||
|
, $var
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testReturnType()
|
public function testReturnType()
|
||||||
{
|
{
|
||||||
$f = eval('return function ():int {};');
|
$f = eval('return function ():int {};');
|
||||||
@ -168,6 +230,72 @@ EOTXT
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 8
|
||||||
|
*/
|
||||||
|
public function testMixedReturnType()
|
||||||
|
{
|
||||||
|
$f = eval('return function (): mixed {};');
|
||||||
|
$line = __LINE__ - 1;
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat(
|
||||||
|
<<<EOTXT
|
||||||
|
Closure(): mixed {
|
||||||
|
returnType: "mixed"
|
||||||
|
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
|
||||||
|
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
|
||||||
|
file: "%sReflectionCasterTest.php($line) : eval()'d code"
|
||||||
|
line: "1 to 1"
|
||||||
|
}
|
||||||
|
EOTXT
|
||||||
|
, $f
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 8
|
||||||
|
*/
|
||||||
|
public function testUnionReturnType()
|
||||||
|
{
|
||||||
|
$f = eval('return function (): int|float {};');
|
||||||
|
$line = __LINE__ - 1;
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat(
|
||||||
|
<<<EOTXT
|
||||||
|
Closure(): int|float {
|
||||||
|
returnType: "int|float"
|
||||||
|
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
|
||||||
|
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
|
||||||
|
file: "%sReflectionCasterTest.php($line) : eval()'d code"
|
||||||
|
line: "1 to 1"
|
||||||
|
}
|
||||||
|
EOTXT
|
||||||
|
, $f
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 8
|
||||||
|
*/
|
||||||
|
public function testNullableUnionReturnType()
|
||||||
|
{
|
||||||
|
$f = eval('return function (): int|float|null {};');
|
||||||
|
$line = __LINE__ - 1;
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat(
|
||||||
|
<<<EOTXT
|
||||||
|
Closure(): int|float|null {
|
||||||
|
returnType: "int|float|null"
|
||||||
|
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
|
||||||
|
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
|
||||||
|
file: "%sReflectionCasterTest.php($line) : eval()'d code"
|
||||||
|
line: "1 to 1"
|
||||||
|
}
|
||||||
|
EOTXT
|
||||||
|
, $f
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testGenerator()
|
public function testGenerator()
|
||||||
{
|
{
|
||||||
if (\extension_loaded('xdebug')) {
|
if (\extension_loaded('xdebug')) {
|
||||||
|
Reference in New Issue
Block a user