Skip to content

Commit 178a0f7

Browse files
committed
Fixing a bug where if a core class was autowired, autowiring tried to autowire optional args as if they were required
1 parent 34d5f9e commit 178a0f7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
273273

274274
// no default value? Then fail
275275
if (!$parameter->isDefaultValueAvailable()) {
276+
// For core classes, isDefaultValueAvailable() can
277+
// be false when isOptional() returns true. If the
278+
// argument *is* optional, allow it to be missing
279+
if ($parameter->isOptional()) {
280+
continue;
281+
}
276282
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));
277283
}
278284

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,23 @@ public function testOptionalScalarArgsNotPassedIfLast()
512512
);
513513
}
514514

515+
public function testOptionalArgsNoRequiredForCoreClasses()
516+
{
517+
$container = new ContainerBuilder();
518+
519+
$container->register('pdo_service', \PDO::class)
520+
->addArgument('sqlite:/foo.db')
521+
->setAutowired(true);
522+
523+
(new AutowirePass())->process($container);
524+
525+
$definition = $container->getDefinition('pdo_service');
526+
$this->assertEquals(
527+
array('sqlite:/foo.db'),
528+
$definition->getArguments()
529+
);
530+
}
531+
515532
public function testSetterInjection()
516533
{
517534
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)