Skip to content

Commit

Permalink
[VarExporter] fix proxy helper when a method returns null
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed May 7, 2024
1 parent 7a9cd97 commit 41c4628
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Caster/ReflectionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
if (isset($a[$prefix.'returnType'])) {
$v = $a[$prefix.'returnType'];
$v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $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 : '', '']);
$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 : '', '']);
}
if (isset($a[$prefix.'class'])) {
$a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
Expand Down Expand Up @@ -413,7 +413,7 @@ public static function getSignature(array $a)
if (!$type instanceof \ReflectionNamedType) {
$signature .= $type.' ';
} else {
if ($param->allowsNull() && 'mixed' !== $type->getName()) {
if ($param->allowsNull() && !\in_array($type->getName(), ['mixed', 'null'], true)) {
$signature .= '?';
}
$signature .= substr(strrchr('\\'.$type->getName(), '\\'), 1).' ';
Expand Down
23 changes: 22 additions & 1 deletion Tests/Caster/ReflectionCasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
use Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes;
use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass;
use Symfony\Component\VarDumper\Tests\Fixtures\Php82NullStandaloneReturnType;
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionIntersectionTypeFixture;
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture;
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionUnionTypeFixture;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function testClosureCaster()
$b: & 123
}
file: "%sReflectionCasterTest.php"
line: "88 to 88"
line: "%s"
}
EOTXT
, $var
Expand Down Expand Up @@ -406,6 +407,26 @@ class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
);
}

/**
* @requires PHP 8.2
*/
public function testNullReturnType()
{
$className = Php82NullStandaloneReturnType::class;

$this->assertDumpMatchesFormat(
<<<EOTXT
{$className}::foo(null \$bar): null {
returnType: "null"
this: {$className} { …}
file: "%s"
line: "%s"
}
EOTXT
, (new Php82NullStandaloneReturnType())->foo(...)
);
}

public function testUnionReturnType()
{
$f = function (): int|float {};
Expand Down
20 changes: 20 additions & 0 deletions Tests/Fixtures/Php82NullStandaloneReturnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Tests\Fixtures;

class Php82NullStandaloneReturnType
{
public function foo(null $bar): null
{
return null;
}
}

0 comments on commit 41c4628

Please sign in to comment.