Skip to content

Commit 26989d4

Browse files
bug #27830 [Dependency-Injection] Fix issue where non-defined services were attempted to be removed (ciaranmcnulty)
This PR was squashed before being merged into the 4.2-dev branch (closes #27830). Discussion ---------- [Dependency-Injection] Fix issue where non-defined services were attempted to be removed | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #27802 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> The RemoveUnusedServicesPass generates a graph of services that are connected, then removes services that are not part of the graph. To build this graph it creates a list of services then examines their definitions for more services. The current scenario was causing an error, thrown by getDefinition (triggered in Behat): * Service was referenced by another service definition * Service was _not_ defined * Service was subsequently set() on the container Commits ------- 53155c9 [Dependency-Injection] Fix issue where non-defined services were attempted to be removed
2 parents a32393b + 53155c9 commit 26989d4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function process(ContainerBuilder $container)
6060
$ids = $this->connectedIds;
6161
$this->connectedIds = array();
6262
foreach ($ids as $id) {
63-
if (!isset($connectedIds[$id]) && $container->has($id)) {
63+
if (!isset($connectedIds[$id]) && $container->hasDefinition($id)) {
6464
$connectedIds[$id] = true;
6565
$this->processValue($container->getDefinition($id));
6666
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ public function testProcessConsiderEnvVariablesAsUsedEvenInPrivateServices()
127127
$this->assertSame(1, $envCounters['FOOBAR']);
128128
}
129129

130+
public function testProcessDoesNotErrorOnServicesThatDoNotHaveDefinitions()
131+
{
132+
$container = new ContainerBuilder();
133+
$container
134+
->register('defined')
135+
->addArgument(new Reference('not.defined'))
136+
->setPublic(true);
137+
138+
$container->set('not.defined', new \StdClass());
139+
140+
$this->process($container);
141+
142+
$this->assertFalse($container->hasDefinition('not.defined'));
143+
}
144+
130145
protected function process(ContainerBuilder $container)
131146
{
132147
(new RemoveUnusedDefinitionsPass())->process($container);

0 commit comments

Comments
 (0)