Skip to content

Commit 673bb6b

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

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/Symfony/Component/VarExporter/ProxyHelper.php

+6-6
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

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

+22
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,28 @@ public function testCannotGenerateGhostForStringMagicGet()
188188
$this->expectException(LogicException::class);
189189
ProxyHelper::generateLazyGhost(new \ReflectionClass(StringMagicGetClass::class));
190190
}
191+
192+
/**
193+
* @requires PHP 8.2
194+
*/
195+
public function testNullStandaloneReturnType()
196+
{
197+
$classCode = <<<PHP
198+
class TestNullStandaloneReturnType
199+
{
200+
public function foo(): null
201+
{
202+
return null;
203+
}
204+
}
205+
PHP;
206+
eval($classCode);
207+
208+
self::assertStringContainsString(
209+
'public function foo(): null',
210+
ProxyHelper::generateLazyProxy(new \ReflectionClass(\TestNullStandaloneReturnType::class))
211+
);
212+
}
191213
}
192214

193215
abstract class TestForProxyHelper

0 commit comments

Comments
 (0)