Skip to content

Commit c5cf9de

Browse files
Merge branch '4.0'
* 4.0: [HttpKernel] Make ServiceValueResolver work if controller namespace starts with a backslash in routing Add d-block to bootstrap 4 alerts [Console] Don't go past exact matches when autocompleting [DI] Improve error message for non-autowirable scalar argument Disable autoloader call on interface_exists check [Validator] Fix LazyLoadingMetadataFactory with PSR6Cache for non classname if tested values isn't an existing class [HttpKernel] Dont create mock cookie for new sessions in tests
2 parents 41ece70 + 59768cb commit c5cf9de

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
@@ -207,7 +207,10 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
207207
if ($parameter->isOptional()) {
208208
continue;
209209
}
210-
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));
210+
$type = ProxyHelper::getTypeHint($reflectionMethod, $parameter, false);
211+
$type = $type ? sprintf('is type-hinted "%s"', $type) : 'has no type-hint';
212+
213+
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));
211214
}
212215

213216
// specifically pass the default value

Tests/Compiler/AutowirePassTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ public function testSomeSpecificArgumentsAreSet()
377377
// args are: A, Foo, Dunglas
378378
->setArguments(array(
379379
1 => new Reference('foo'),
380+
3 => array('bar'),
380381
));
381382

382383
(new ResolveClassPass())->process($container);
@@ -388,19 +389,38 @@ public function testSomeSpecificArgumentsAreSet()
388389
new TypedReference(A::class, A::class),
389390
new Reference('foo'),
390391
new TypedReference(Dunglas::class, Dunglas::class),
392+
array('bar'),
391393
),
392394
$definition->getArguments()
393395
);
394396
}
395397

396398
/**
397399
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
398-
* @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.
400+
* @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.
399401
*/
400402
public function testScalarArgsCannotBeAutowired()
401403
{
402404
$container = new ContainerBuilder();
403405

406+
$container->register(A::class);
407+
$container->register(Dunglas::class);
408+
$container->register('arg_no_type_hint', __NAMESPACE__.'\MultipleArguments')
409+
->setArguments(array(1 => 'foo'))
410+
->setAutowired(true);
411+
412+
(new ResolveClassPass())->process($container);
413+
(new AutowirePass())->process($container);
414+
}
415+
416+
/**
417+
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
418+
* @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.
419+
*/
420+
public function testNoTypeArgsCannotBeAutowired()
421+
{
422+
$container = new ContainerBuilder();
423+
404424
$container->register(A::class);
405425
$container->register(Dunglas::class);
406426
$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
@@ -183,7 +183,7 @@ public function __construct(A $k)
183183
}
184184
class MultipleArguments
185185
{
186-
public function __construct(A $k, $foo, Dunglas $dunglas)
186+
public function __construct(A $k, $foo, Dunglas $dunglas, array $bar)
187187
{
188188
}
189189
}

0 commit comments

Comments
 (0)