From 185454367a65540c3ccae3b029edaae57f9f3144 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 7 Jan 2021 02:06:44 +0100 Subject: [PATCH] [DependencyInjection] Fix InvalidParameterTypeException for function parameters --- .../InvalidParameterTypeException.php | 7 ++- .../InvalidParameterTypeExceptionTest.php | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Exception/InvalidParameterTypeExceptionTest.php diff --git a/src/Symfony/Component/DependencyInjection/Exception/InvalidParameterTypeException.php b/src/Symfony/Component/DependencyInjection/Exception/InvalidParameterTypeException.php index 461c50cce6c68..2a11626fe2bf0 100644 --- a/src/Symfony/Component/DependencyInjection/Exception/InvalidParameterTypeException.php +++ b/src/Symfony/Component/DependencyInjection/Exception/InvalidParameterTypeException.php @@ -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)); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Exception/InvalidParameterTypeExceptionTest.php b/src/Symfony/Component/DependencyInjection/Tests/Exception/InvalidParameterTypeExceptionTest.php new file mode 100644 index 0000000000000..d61388ea2dd72 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Exception/InvalidParameterTypeExceptionTest.php @@ -0,0 +1,52 @@ + + * + * 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 +{ +} 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