Skip to content

Commit 3b16002

Browse files
[DI] Improve error message for non-autowirable scalar argument
1 parent 24a6871 commit 3b16002

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

Compiler/AutowirePass.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
233233
if ($parameter->isOptional()) {
234234
continue;
235235
}
236-
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));
236+
$type = ProxyHelper::getTypeHint($reflectionMethod, $parameter, false);
237+
$type = $type ? sprintf('is type-hinted "%s"', $type) : 'has no type-hint';
238+
239+
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));
237240
}
238241

239242
// specifically pass the default value

Tests/Compiler/AutowirePassTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ public function testSomeSpecificArgumentsAreSet()
436436
// args are: A, Foo, Dunglas
437437
->setArguments(array(
438438
1 => new Reference('foo'),
439+
3 => array('bar'),
439440
));
440441

441442
(new ResolveClassPass())->process($container);
@@ -447,19 +448,38 @@ public function testSomeSpecificArgumentsAreSet()
447448
new TypedReference(A::class, A::class, MultipleArguments::class),
448449
new Reference('foo'),
449450
new TypedReference(Dunglas::class, Dunglas::class, MultipleArguments::class),
451+
array('bar'),
450452
),
451453
$definition->getArguments()
452454
);
453455
}
454456

455457
/**
456458
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
457-
* @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.
459+
* @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.
458460
*/
459461
public function testScalarArgsCannotBeAutowired()
460462
{
461463
$container = new ContainerBuilder();
462464

465+
$container->register(A::class);
466+
$container->register(Dunglas::class);
467+
$container->register('arg_no_type_hint', __NAMESPACE__.'\MultipleArguments')
468+
->setArguments(array(1 => 'foo'))
469+
->setAutowired(true);
470+
471+
(new ResolveClassPass())->process($container);
472+
(new AutowirePass())->process($container);
473+
}
474+
475+
/**
476+
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
477+
* @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.
478+
*/
479+
public function testNoTypeArgsCannotBeAutowired()
480+
{
481+
$container = new ContainerBuilder();
482+
463483
$container->register(A::class);
464484
$container->register(Dunglas::class);
465485
$container->register('arg_no_type_hint', __NAMESPACE__.'\MultipleArguments')

Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function __construct(A $k)
181181
}
182182
class MultipleArguments
183183
{
184-
public function __construct(A $k, $foo, Dunglas $dunglas)
184+
public function __construct(A $k, $foo, Dunglas $dunglas, array $bar)
185185
{
186186
}
187187
}

0 commit comments

Comments
 (0)