Skip to content

Commit

Permalink
Fix error when proxying a method with a mixed argument with a null de…
Browse files Browse the repository at this point in the history
…fault 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.
  • Loading branch information
aboks authored and nicolas-grekas committed Mar 20, 2024
1 parent 0235d4d commit 2c8a6cf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ProxyManager/Generator/MethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function copyMethodSignature(MethodReflection $reflectionMethod):
$parameter->setDefaultValue(new ValueGenerator($default, $reflectionParameter));
$type = $parameter->getType();

if ($default->getValue() === null && strpos($type ?? '?', '?') !== 0 && strpos($type, '|') === false) {
if ($default->getValue() === null && strpos($type ?? '?', '?') !== 0 && strpos($type, '|') === false && $type !== 'mixed') {
$parameter->setType('?' . $type);
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/ProxyManagerTest/Generator/MethodGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ProxyManager\Generator\MethodGenerator;
use ProxyManagerTestAsset\BaseClass;
use ProxyManagerTestAsset\ClassWithAbstractPublicMethod;
use ProxyManagerTestAsset\ClassWithNullDefaultMethodArguments;
use ProxyManagerTestAsset\EmptyClass;
use ProxyManagerTestAsset\ReturnTypeHintedClass;
use ProxyManagerTestAsset\ScalarTypeHintedClass;
Expand Down Expand Up @@ -134,6 +135,27 @@ public function scalarTypeHintedMethods(): array
];
}

/**
* @requires PHP 8.0
*/
public function testGenerateMethodWithNullDefaultMixedArgument(): void
{
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection(
ClassWithNullDefaultMethodArguments::class,
'acceptMixed'
));

self::assertSame('acceptMixed', $method->getName());

$parameters = $method->getParameters();

self::assertCount(1, $parameters);

$param = $parameters['param'];

self::assertSame('mixed', $param->getType());
}

public function testGenerateMethodWithVoidReturnTypeHinting(): void
{
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace ProxyManagerTestAsset;

class ClassWithNullDefaultMethodArguments
{
public function acceptMixed(mixed $param = null)
{
return $param;
}
}

0 comments on commit 2c8a6cf

Please sign in to comment.