Skip to content

[DI] Suggest typed argument when binding fails with untyped argument #34223

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 14, 2020
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 @@ -112,6 +112,8 @@ protected function processValue($value, $isRoot = false)
return parent::processValue($value, $isRoot);
}

$bindingNames = [];

foreach ($bindings as $key => $binding) {
list($bindingValue, $bindingId, $used, $bindingType, $file) = $binding->getValues();
if ($used) {
Expand All @@ -121,7 +123,11 @@ protected function processValue($value, $isRoot = false)
$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;
}

Expand Down Expand Up @@ -182,11 +188,17 @@ protected function processValue($value, $isRoot = false)
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;
}

$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]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
Expand Down Expand Up @@ -157,4 +158,19 @@ public function testSyntheticServiceWithBind()

$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);
}
}