Skip to content

Commit 61966df

Browse files
[DI] There are no initialized-only refs to undeclared services
1 parent cf35e0c commit 61966df

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,12 +1711,12 @@ private function getServiceCall($id, Reference $reference = null)
17111711
return '$this';
17121712
}
17131713

1714-
if (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
1715-
$code = 'null';
1716-
} elseif ($this->container->hasDefinition($id)) {
1714+
if ($this->container->hasDefinition($id)) {
17171715
$definition = $this->container->getDefinition($id);
17181716

1719-
if ($this->isTrivialInstance($definition)) {
1717+
if (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
1718+
$code = 'null';
1719+
} elseif ($this->isTrivialInstance($definition)) {
17201720
$code = substr($this->addNewInstance($definition, '', '', $id), 8, -2);
17211721
if ($definition->isShared()) {
17221722
$code = sprintf('$this->services[\'%s\'] = %s', $id, $code);
@@ -1726,6 +1726,8 @@ private function getServiceCall($id, Reference $reference = null)
17261726
} else {
17271727
$code = sprintf('$this->%s()', $this->generateMethodName($id));
17281728
}
1729+
} elseif (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
1730+
return 'null';
17291731
} elseif (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
17301732
$code = sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
17311733
} else {

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_uninitialized_ref.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ protected function getBarService()
7676
$this->services['bar'] = $instance = new \stdClass();
7777

7878
$instance->foo1 = ${($_ = isset($this->services['foo1']) ? $this->services['foo1'] : null) && false ?: '_'};
79-
$instance->foo2 = ${($_ = isset($this->services['foo2']) ? $this->services['foo2'] : null) && false ?: '_'};
79+
$instance->foo2 = null;
8080
$instance->foo3 = ${($_ = isset($this->services['foo3']) ? $this->services['foo3'] : null) && false ?: '_'};
8181
$instance->closures = array(0 => function () {
8282
return ${($_ = isset($this->services['foo1']) ? $this->services['foo1'] : null) && false ?: '_'};
8383
}, 1 => function () {
84-
return ${($_ = isset($this->services['foo2']) ? $this->services['foo2'] : null) && false ?: '_'};
84+
return null;
8585
}, 2 => function () {
8686
return ${($_ = isset($this->services['foo3']) ? $this->services['foo3'] : null) && false ?: '_'};
8787
});
@@ -90,7 +90,7 @@ protected function getBarService()
9090
yield 'foo1' => ${($_ = isset($this->services['foo1']) ? $this->services['foo1'] : null) && false ?: '_'};
9191
}
9292
if (false) {
93-
yield 'foo2' => ${($_ = isset($this->services['foo2']) ? $this->services['foo2'] : null) && false ?: '_'};
93+
yield 'foo2' => null;
9494
}
9595
if (isset($this->services['foo3'])) {
9696
yield 'foo3' => ${($_ = isset($this->services['foo3']) ? $this->services['foo3'] : null) && false ?: '_'};

0 commit comments

Comments
 (0)