Skip to content

[DependencyInjection] Fix InvalidParameterTypeException for function parameters #39746

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
Jan 7, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function __construct(string $serviceId, string $type, \ReflectionParamete
$acceptedType = $acceptedType instanceof \ReflectionNamedType ? $acceptedType->getName() : (string) $acceptedType;
$this->code = $type;

parent::__construct(sprintf('Invalid definition for service "%s": argument %d of "%s::%s()" accepts "%s", "%s" passed.', $serviceId, 1 + $parameter->getPosition(), $parameter->getDeclaringClass()->getName(), $parameter->getDeclaringFunction()->getName(), $acceptedType, $type));
$function = $parameter->getDeclaringFunction();
$functionName = $function instanceof \ReflectionMethod
? sprintf('%s::%s', $function->getDeclaringClass()->getName(), $function->getName())
: $function->getName();

parent::__construct(sprintf('Invalid definition for service "%s": argument %d of "%s()" accepts "%s", "%s" passed.', $serviceId, 1 + $parameter->getPosition(), $functionName, $acceptedType, $type));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\DependencyInjection\Tests\Exception;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Exception\InvalidParameterTypeException;

final class InvalidParameterTypeExceptionTest extends TestCase
{
/**
* @dataProvider provideReflectionParameters
*/
public function testExceptionMessage(\ReflectionParameter $parameter, string $expectedMessage)
{
$exception = new InvalidParameterTypeException('my_service', 'int', $parameter);

self::assertSame($expectedMessage, $exception->getMessage());
}

public function provideReflectionParameters(): iterable
{
yield 'static method' => [
new \ReflectionParameter([MyClass::class, 'doSomething'], 0),
'Invalid definition for service "my_service": argument 1 of "Symfony\Component\DependencyInjection\Tests\Exception\MyClass::doSomething()" accepts "array", "int" passed.',
];

yield 'function' => [
new \ReflectionParameter(__NAMESPACE__.'\\myFunction', 0),
'Invalid definition for service "my_service": argument 1 of "Symfony\Component\DependencyInjection\Tests\Exception\myFunction()" accepts "array", "int" passed.',
];
}
}

class MyClass
{
public static function doSomething(array $arguments): void
{
}
}

function myFunction(array $arguments): void
{
}