Skip to content

Commit 59768cb

Browse files
Merge branch '3.4' into 4.0
* 3.4: [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 9f1cea6 + 3b16002 commit 59768cb

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
@@ -179,7 +179,10 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
179179
if ($parameter->isOptional()) {
180180
continue;
181181
}
182-
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));
182+
$type = ProxyHelper::getTypeHint($reflectionMethod, $parameter, false);
183+
$type = $type ? sprintf('is type-hinted "%s"', $type) : 'has no type-hint';
184+
185+
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));
183186
}
184187

185188
// specifically pass the default value

Tests/Compiler/AutowirePassTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ public function testSomeSpecificArgumentsAreSet()
373373
// args are: A, Foo, Dunglas
374374
->setArguments(array(
375375
1 => new Reference('foo'),
376+
3 => array('bar'),
376377
));
377378

378379
(new ResolveClassPass())->process($container);
@@ -384,19 +385,38 @@ public function testSomeSpecificArgumentsAreSet()
384385
new TypedReference(A::class, A::class, MultipleArguments::class),
385386
new Reference('foo'),
386387
new TypedReference(Dunglas::class, Dunglas::class, MultipleArguments::class),
388+
array('bar'),
387389
),
388390
$definition->getArguments()
389391
);
390392
}
391393

392394
/**
393395
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
394-
* @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.
396+
* @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.
395397
*/
396398
public function testScalarArgsCannotBeAutowired()
397399
{
398400
$container = new ContainerBuilder();
399401

402+
$container->register(A::class);
403+
$container->register(Dunglas::class);
404+
$container->register('arg_no_type_hint', __NAMESPACE__.'\MultipleArguments')
405+
->setArguments(array(1 => 'foo'))
406+
->setAutowired(true);
407+
408+
(new ResolveClassPass())->process($container);
409+
(new AutowirePass())->process($container);
410+
}
411+
412+
/**
413+
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
414+
* @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.
415+
*/
416+
public function testNoTypeArgsCannotBeAutowired()
417+
{
418+
$container = new ContainerBuilder();
419+
400420
$container->register(A::class);
401421
$container->register(Dunglas::class);
402422
$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)