Skip to content

[DI] Improve error message for non-autowirable scalar argument #26856

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
Apr 10, 2018
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 @@ -233,7 +233,10 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
if ($parameter->isOptional()) {
continue;
}
throw new AutowiringFailedException($this->currentId, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s()" must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
$type = ProxyHelper::getTypeHint($reflectionMethod, $parameter, false);
$type = $type ? sprintf('is type-hinted "%s"', $type) : 'has no type-hint';

throw new AutowiringFailedException($this->currentId, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s()" %s, you should configure its value explicitly.', $this->currentId, $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method, $type));
}

// specifically pass the default value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ public function testSomeSpecificArgumentsAreSet()
// args are: A, Foo, Dunglas
->setArguments(array(
1 => new Reference('foo'),
3 => array('bar'),
));

(new ResolveClassPass())->process($container);
Expand All @@ -447,19 +448,38 @@ public function testSomeSpecificArgumentsAreSet()
new TypedReference(A::class, A::class, MultipleArguments::class),
new Reference('foo'),
new TypedReference(Dunglas::class, Dunglas::class, MultipleArguments::class),
array('bar'),
),
$definition->getArguments()
);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
* @expectedExceptionMessage Cannot autowire service "arg_no_type_hint": argument "$foo" of method "Symfony\Component\DependencyInjection\Tests\Compiler\MultipleArguments::__construct()" must have a type-hint or be given a value explicitly.
* @expectedExceptionMessage Cannot autowire service "arg_no_type_hint": argument "$bar" of method "Symfony\Component\DependencyInjection\Tests\Compiler\MultipleArguments::__construct()" is type-hinted "array", you should configure its value explicitly.
*/
public function testScalarArgsCannotBeAutowired()
{
$container = new ContainerBuilder();

$container->register(A::class);
$container->register(Dunglas::class);
$container->register('arg_no_type_hint', __NAMESPACE__.'\MultipleArguments')
->setArguments(array(1 => 'foo'))
->setAutowired(true);

(new ResolveClassPass())->process($container);
(new AutowirePass())->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
* @expectedExceptionMessage Cannot autowire service "arg_no_type_hint": argument "$foo" of method "Symfony\Component\DependencyInjection\Tests\Compiler\MultipleArguments::__construct()" has no type-hint, you should configure its value explicitly.
*/
public function testNoTypeArgsCannotBeAutowired()
{
$container = new ContainerBuilder();

$container->register(A::class);
$container->register(Dunglas::class);
$container->register('arg_no_type_hint', __NAMESPACE__.'\MultipleArguments')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function __construct(A $k)
}
class MultipleArguments
{
public function __construct(A $k, $foo, Dunglas $dunglas)
public function __construct(A $k, $foo, Dunglas $dunglas, array $bar)
{
}
}
Expand Down