8000 bug #18039 Fixing autowiring collision failure (weaverryan) · symfony/symfony@681a349 · GitHub
[go: up one dir, main page]

Skip to content

Commit 681a349

Browse files
committed
bug #18039 Fixing autowiring collision failure (weaverryan)
This PR was merged into the 3.1-dev branch. Discussion ---------- Fixing autowiring collision failure | Q | A | ------------- | --- | Branch | master | Bug fix? | yes (bug introduced in master) | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | n/a In #17877, I introduced a bug: https://github.com/symfony/symfony/pull/17877/files#diff-62df969ae028c559d33ffd256de1ac49L200. Namely, if some class cannot be autowired because there is an *odd* number of matching services, then it *would* autowire the last service found, instead of throwing an exception. The tests only tested even numbers, which is how it was missed. This fixes that. Thanks! Commits ------- 2aea337 Fixing a bug where an odd number of type collisions would incorrectly autowire (instead of an error)
2 parents 1887261 + 2aea337 commit 681a349

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,21 @@ private function set($type, $id)
181181
return;
182182
}
183183

184+
// is this already a type/class that is known to match multiple services?
185+
if (isset($this->ambiguousServiceTypes[$type])) {
186+
$this->addServiceToAmbiguousType($id, $type);
187+
188+
return;
189+
}
190+
184191
// check to make sure the type doesn't match multiple services
185192
if (isset($this->types[$type])) {
186193
if ($this->types[$type] === $id) {
187194
return;
188195
}
189196

190197
// keep an array of all services matching this type
191-
if (!isset($this->ambiguousServiceTypes[$type])) {
192-
$this->ambiguousServiceTypes[$type] = array(
193-
$this->types[$type],
194-
);
195-
}
196-
$this->ambiguousServiceTypes[$type][] = $id;
198+
$this->addServiceToAmbiguousType($id, $type);
197199

198200
unset($this->types[$type]);
199201

@@ -265,4 +267,15 @@ private function getReflectionClass($id, Definition $definition)
265267
// return null
266268
}
267269
}
270+
271+
private function addServiceToAmbiguousType($id, $type)
272+
{
273+
// keep an array of all services matching this type
274+
if (!isset($this->ambiguousServiceTypes[$type])) {
275+
$this->ambiguousServiceTypes[$type] = array(
276+
$this->types[$type],
277+
);
278+
}
279+
$this->ambiguousServiceTypes[$type][] = $id;
280+
}
268281
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,15 @@ public function testCompleteExistingDefinitionWithNotDefinedArguments()
103103

104104
/**
105105
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
106-
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" for the service "a". Multiple services exist for this interface (c1, c2).
106+
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" for the service "a". Multiple services exist for this interface (c1, c2, c3).
107107
*/
108108
public function testTypeCollision()
109109
{
110110
$container = new ContainerBuilder();
111111

112112
$container->register('c1', __NAMESPACE__.'\CollisionA');
113113
$container->register('c2', __NAMESPACE__.'\CollisionB');
114+
$container->register('c3', __NAMESPACE__.'\CollisionB');
114115
$aDefinition = $container->register('a', __NAMESPACE__.'\CannotBeAutowired');
115116
$aDefinition->setAutowired(true);
116117

0 commit comments

Comments
 (0)
0