Skip to content

[VarExporter] fix ProxyHelper::generateLazyProxy() when a method returns null #54850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Component/VarDumper/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
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
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;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarExporter/ProxyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public static function exportType(\ReflectionFunctionAbstract|\ReflectionPropert
return '';
}
if (null === $glue) {
return (!$noBuiltin && $type->allowsNull() && 'mixed' !== $name ? '?' : '').$types[0];
return (!$noBuiltin && $type->allowsNull() && !\in_array($name, ['mixed', 'null'], true) ? '?' : '').$types[0];
}
sort($types);

Expand Down
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\VarExporter\Tests\Fixtures\LazyProxy;

class Php82NullStandaloneReturnType
{
public function foo(): null
{
return null;
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\VarExporter\Exception\LogicException;
use Symfony\Component\VarExporter\ProxyHelper;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\Php82NullStandaloneReturnType;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\StringMagicGetClass;

class ProxyHelperTest extends TestCase
Expand Down Expand Up @@ -188,6 +189,17 @@ public function testCannotGenerateGhostForStringMagicGet()
$this->expectException(LogicException::class);
ProxyHelper::generateLazyGhost(new \ReflectionClass(StringMagicGetClass::class));
}

/**
* @requires PHP 8.2
*/
public function testNullStandaloneReturnType()
{
self::assertStringContainsString(
'public function foo(): null',
ProxyHelper::generateLazyProxy(new \ReflectionClass(Php82NullStandaloneReturnType::class))
);
}
}

abstract class TestForProxyHelper
Expand Down
Loading