Skip to content

Commit 2c8a6cf

Browse files
aboksnicolas-grekas
authored andcommitted
Fix error when proxying a method with a mixed argument with a null default value
Without this change, trying to proxy a Symfony\Component\Console\Command\Command would result in an InvalidArgumentException `Type "mixed" cannot be nullable` from the Laminas code generator.
1 parent 0235d4d commit 2c8a6cf

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/ProxyManager/Generator/MethodGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static function copyMethodSignature(MethodReflection $reflectionMethod):
6969
$parameter->setDefaultValue(new ValueGenerator($default, $reflectionParameter));
7070
$type = $parameter->getType();
7171

72-
if ($default->getValue() === null && strpos($type ?? '?', '?') !== 0 && strpos($type, '|') === false) {
72+
if ($default->getValue() === null && strpos($type ?? '?', '?') !== 0 && strpos($type, '|') === false && $type !== 'mixed') {
7373
$parameter->setType('?' . $type);
7474
}
7575
}

tests/ProxyManagerTest/Generator/MethodGeneratorTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ProxyManager\Generator\MethodGenerator;
1111
use ProxyManagerTestAsset\BaseClass;
1212
use ProxyManagerTestAsset\ClassWithAbstractPublicMethod;
13+
use ProxyManagerTestAsset\ClassWithNullDefaultMethodArguments;
1314
use ProxyManagerTestAsset\EmptyClass;
1415
use ProxyManagerTestAsset\ReturnTypeHintedClass;
1516
use ProxyManagerTestAsset\ScalarTypeHintedClass;
@@ -134,6 +135,27 @@ public function scalarTypeHintedMethods(): array
134135
];
135136
}
136137

138+
/**
139+
* @requires PHP 8.0
140+
*/
141+
public function testGenerateMethodWithNullDefaultMixedArgument(): void
142+
{
143+
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection(
144+
ClassWithNullDefaultMethodArguments::class,
145+
'acceptMixed'
146+
));
147+
148+
self::assertSame('acceptMixed', $method->getName());
149+
150+
$parameters = $method->getParameters();
151+
152+
self::assertCount(1, $parameters);
153+
154+
$param = $parameters['param'];
155+
156+
self::assertSame('mixed', $param->getType());
157+
}
158+
137159
public function testGenerateMethodWithVoidReturnTypeHinting(): void
138160
{
139161
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ProxyManagerTestAsset;
6+
7+
class ClassWithNullDefaultMethodArguments
8+
{
9+
public function acceptMixed(mixed $param = null)
10+
{
11+
return $param;
12+
}
13+
}

0 commit comments

Comments
 (0)