Skip to content

Commit

Permalink
minor #26856 [DI] Improve error message for non-autowirable scalar ar…
Browse files Browse the repository at this point in the history
…gument (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26838
| License       | MIT
| Doc PR        | -

Commits
-------

7f39811 [DI] Improve error message for non-autowirable scalar argument
  • Loading branch information
Tobion committed Apr 10, 2018
2 parents 7cd5e43 + 7f39811 commit 11bdd80
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
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
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
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

0 comments on commit 11bdd80

Please sign in to comment.