Skip to content

Commit 59b3b7a

Browse files
committed
[VarExporter] fix proxy helper when a method returns null
1 parent 202199b commit 59b3b7a

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/Symfony/Component/VarExporter/ProxyHelper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public static function exportType(\ReflectionFunctionAbstract|\ReflectionPropert
311311
return '';
312312
}
313313
if (null === $glue) {
314-
return (!$noBuiltin && $type->allowsNull() && 'mixed' !== $name ? '?' : '').$types[0];
314+
return (!$noBuiltin && $type->allowsNull() && 'null' !== $type->getName() && 'mixed' !== $name ? '?' : '').$types[0];
315315
}
316316
sort($types);
317317

@@ -357,11 +357,11 @@ private static function exportDefault(\ReflectionParameter $param): string
357357
'parent' => ($parent = $class->getParentClass()) ? '\\'.$parent->name : 'parent',
358358
default => '\\'.$m[2],
359359
}
360-
: fn ($m) => $m[1].match ($m[2]) {
361-
'new', 'false', 'true', 'null', 'self', 'parent' => $m[2],
362-
'NULL' => 'null',
363-
default => '\\'.$m[2],
364-
};
360+
: fn ($m) => $m[1].match ($m[2]) {
361+
'new', 'false', 'true', 'null', 'self', 'parent' => $m[2],
362+
'NULL' => 'null',
363+
default => '\\'.$m[2],
364+
};
365365

366366
return implode('', array_map(fn ($part) => match ($part[0]) {
367367
'"' => $part, // for internal classes only
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy;
6+
7+
/**
8+
* @author Nicolas PHILIPPE <nikophil@gmail.com>
9+
*/
10+
class TestNullStandaloneReturnType
11+
{
12+
public function foo(): null
13+
{
14+
return null;
15+
}
16+
}

src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\VarExporter\Exception\LogicException;
1616
use Symfony\Component\VarExporter\ProxyHelper;
1717
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\StringMagicGetClass;
18+
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestNullStandaloneReturnType;
1819

1920
class ProxyHelperTest extends TestCase
2021
{
@@ -188,6 +189,17 @@ public function testCannotGenerateGhostForStringMagicGet()
188189
$this->expectException(LogicException::class);
189190
ProxyHelper::generateLazyGhost(new \ReflectionClass(StringMagicGetClass::class));
190191
}
192+
193+
/**
194+
* @requires PHP 8.2
195+
*/
196+
public function testNullStandaloneReturnType()
197+
{
198+
self::assertStringContainsString(
199+
'public function foo(): null',
200+
ProxyHelper::generateLazyProxy(new \ReflectionClass(TestNullStandaloneReturnType::class))
201+
);
202+
}
191203
}
192204

193205
abstract class TestForProxyHelper

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy