8000 Fixing a bug where an odd number of type collisions would incorrectly… · symfony/symfony@2aea337 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2aea337

Browse files
committed
Fixing a bug where an odd number of type collisions would incorrectly autowire (instead of an error)
1 parent b868feb commit 2aea337

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