Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 178a0f7

Browse filesBrowse 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
Copy full SHA for 178a0f7

File tree

Expand file treeCollapse file tree

2 files changed

+23
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+23
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+6Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
+17Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.