bug #34223 [DI] Suggest typed argument when binding fails with untyped argument (gudfar)
This PR was merged into the 4.3 branch.
Discussion
----------
[DI] Suggest typed argument when binding fails with untyped argument
| Q | A
| ------------- | ---
| Branch? | 4.3
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | #33470
| License | MIT
I've added a condition that looks for arguments and if the typehint doesn’t match, throws an `InvalidArgumentException`
Commits
-------
0e92399daa
[DI] Suggest typed argument when binding fails with untyped argument
This commit is contained in:
commit
477e843790
@ -112,6 +112,8 @@ class ResolveBindingsPass extends AbstractRecursivePass
|
|||||||
return parent::processValue($value, $isRoot);
|
return parent::processValue($value, $isRoot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$bindingNames = [];
|
||||||
|
|
||||||
foreach ($bindings as $key => $binding) {
|
foreach ($bindings as $key => $binding) {
|
||||||
list($bindingValue, $bindingId, $used, $bindingType, $file) = $binding->getValues();
|
list($bindingValue, $bindingId, $used, $bindingType, $file) = $binding->getValues();
|
||||||
if ($used) {
|
if ($used) {
|
||||||
@ -121,7 +123,11 @@ class ResolveBindingsPass extends AbstractRecursivePass
|
|||||||
$this->unusedBindings[$bindingId] = [$key, $this->currentId, $bindingType, $file];
|
$this->unusedBindings[$bindingId] = [$key, $this->currentId, $bindingType, $file];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preg_match('/^(?:(?:array|bool|float|int|string) )?\$/', $key)) {
|
if (preg_match('/^(?:(?:array|bool|float|int|string|([^ $]++)) )\$/', $key, $m)) {
|
||||||
|
$bindingNames[substr($key, \strlen($m[0]))] = $binding;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($m[1])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,11 +188,17 @@ class ResolveBindingsPass extends AbstractRecursivePass
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$typeHint || '\\' !== $typeHint[0] || !isset($bindings[$typeHint = substr($typeHint, 1)])) {
|
if ($typeHint && '\\' === $typeHint[0] && isset($bindings[$typeHint = substr($typeHint, 1)])) {
|
||||||
|
$arguments[$key] = $this->getBindingValue($bindings[$typeHint]);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$arguments[$key] = $this->getBindingValue($bindings[$typeHint]);
|
if (isset($bindingNames[$parameter->name])) {
|
||||||
|
$bindingKey = array_search($binding, $bindings, true);
|
||||||
|
$argumentType = substr($bindingKey, 0, strpos($bindingKey, ' '));
|
||||||
|
$this->errorMessages[] = sprintf('Did you forget to add the type "%s" to argument "$%s" of method "%s::%s()"?', $argumentType, $parameter->name, $reflectionMethod->class, $reflectionMethod->name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($arguments !== $call[1]) {
|
if ($arguments !== $call[1]) {
|
||||||
|
@ -18,6 +18,7 @@ use Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass;
|
|||||||
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
|
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Definition;
|
use Symfony\Component\DependencyInjection\Definition;
|
||||||
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||||
use Symfony\Component\DependencyInjection\Reference;
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
|
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
|
||||||
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
|
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
|
||||||
@ -157,4 +158,19 @@ class ResolveBindingsPassTest extends TestCase
|
|||||||
|
|
||||||
$this->assertSame([1 => 'bar'], $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
|
$this->assertSame([1 => 'bar'], $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testEmptyBindingTypehint()
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage('Did you forget to add the type "string" to argument "$apiKey" of method "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy::__construct()"?');
|
||||||
|
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$bindings = [
|
||||||
|
'string $apiKey' => new BoundArgument('foo'),
|
||||||
|
];
|
||||||
|
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
|
||||||
|
$definition->setBindings($bindings);
|
||||||
|
$pass = new ResolveBindingsPass();
|
||||||
|
$pass->process($container);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user