Skip to content

Commit 41c4628

Browse files
committed
[VarExporter] fix proxy helper when a method returns null
1 parent 7a9cd97 commit 41c4628

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

Caster/ReflectionCaster.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
231231
if (isset($a[$prefix.'returnType'])) {
232232
$v = $a[$prefix.'returnType'];
233233
$v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
234-
$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 : '', '']);
234+
$a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType'] instanceof \ReflectionNamedType && $a[$prefix.'returnType']->allowsNull() && !\in_array($v, ['mixed', 'null'], true) ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
235235
}
236236
if (isset($a[$prefix.'class'])) {
237237
$a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
@@ -413,7 +413,7 @@ public static function getSignature(array $a)
413413
if (!$type instanceof \ReflectionNamedType) {
414414
$signature .= $type.' ';
415415
} else {
416-
if ($param->allowsNull() && 'mixed' !== $type->getName()) {
416+
if ($param->allowsNull() && !\in_array($type->getName(), ['mixed', 'null'], true)) {
417417
$signature .= '?';
418418
}
419419
$signature .= substr(strrchr('\\'.$type->getName(), '\\'), 1).' ';

Tests/Caster/ReflectionCasterTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
1919
use Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes;
2020
use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass;
21+
use Symfony\Component\VarDumper\Tests\Fixtures\Php82NullStandaloneReturnType;
2122
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionIntersectionTypeFixture;
2223
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture;
2324
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionUnionTypeFixture;
@@ -95,7 +96,7 @@ public function testClosureCaster()
9596
$b: & 123
9697
}
9798
file: "%sReflectionCasterTest.php"
98-
line: "88 to 88"
99+
line: "%s"
99100
}
100101
EOTXT
101102
, $var
@@ -406,6 +407,26 @@ class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
406407
);
407408
}
408409

410+
/**
411+
* @requires PHP 8.2
412+
*/
413+
public function testNullReturnType()
414+
{
415+
$className = Php82NullStandaloneReturnType::class;
416+
417+
$this->assertDumpMatchesFormat(
418+
<<<EOTXT
419+
{$className}::foo(null \$bar): null {
420+
returnType: "null"
421+
this: {$className} { …}
422+
file: "%s"
423+
line: "%s"
424+
}
425+
EOTXT
426+
, (new Php82NullStandaloneReturnType())->foo(...)
427+
);
428+
}
429+
409430
public function testUnionReturnType()
410431
{
411432
$f = function (): int|float {};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
13+
14+
class Php82NullStandaloneReturnType
15+
{
16+
public function foo(null $bar): null
17+
{
18+
return null;
19+
}
20+
}

0 commit comments

Comments
 (0)