Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 21072d5

Browse files
committed
[DependencyInjection] Fix InvalidParameterTypeException for function parameters
1 parent 92ccb0a commit 21072d5

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/Symfony/Component/DependencyInjection/Exception/InvalidParameterTypeException.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function __construct(string $serviceId, string $type, \ReflectionParamete
2525
$acceptedType = $acceptedType instanceof \ReflectionNamedType ? $acceptedType->getName() : (string) $acceptedType;
2626
$this->code = $type;
2727

28-
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));
28+
$function = $parameter->getDeclaringFunction();
29+
$functionName = $function instanceof \ReflectionMethod
30+
? sprintf('%s::%s', $function->getDeclaringClass()->getName(), $function->getName())
31+
: $function->getName();
32+
33+
parent::__construct(sprintf('Invalid definition for service "%s": argument %d of "%s()" accepts "%s", "%s" passed.', $serviceId, 1 + $parameter->getPosition(), $functionName, $acceptedType, $type));
2934
}
3035
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Exception;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Exception\InvalidParameterTypeException;
16+
17+
final class InvalidParameterTypeExceptionTest extends TestCase
18+
{
19+
/**
20+
* @dataProvider provideReflectionParameters
21+
*/
22+
public function testExceptionMessage(\ReflectionParameter $parameter, string $expectedMessage)
23+
{
24+
$exception = new InvalidParameterTypeException('my_service', 'int', $parameter);
25+
26+
self::assertSame($expectedMessage, $exception->getMessage());
27+
}
28+
29+
public function provideReflectionParameters(): iterable
30+
{
31+
yield 'static method' => [
32+
new \ReflectionParameter([MyClass::class, 'doSomething'], 0),
33+
'Invalid definition for service "my_service": argument 1 of "Symfony\Component\DependencyInjection\Tests\Exception\MyClass::doSomething()" accepts "array", "int" passed.'
34+
];
35+
36+
yield 'function' => [
37+
new \ReflectionParameter(__NAMESPACE__.'\\myFunction', 0),
38+
'Invalid definition for service "my_service": argument 1 of "Symfony\Component\DependencyInjection\Tests\Exception\myFunction()" accepts "array", "int" passed.'
39+
];
40+
}
41+
}
42+
43+
class MyClass
44+
{
45+
public static function doSomething(array $arguments): void
46+
{
47+
}
48+
}
49+
50+
function myFunction(array $arguments): void
51+
{
52+
}

0 commit comments

Comments
 (0)