Fixing a bug where an odd number of type collisions would incorrectly autowire (instead of an error)

This commit is contained in:
Ryan Weaver 2016-03-06 17:03:14 -05:00
parent b868feb4ab
commit 2aea337f42
2 changed files with 21 additions and 7 deletions

View File

@ -181,6 +181,13 @@ class AutowirePass implements CompilerPassInterface
return; return;
} }
// is this already a type/class that is known to match multiple services?
if (isset($this->ambiguousServiceTypes[$type])) {
$this->addServiceToAmbiguousType($id, $type);
return;
}
// check to make sure the type doesn't match multiple services // check to make sure the type doesn't match multiple services
if (isset($this->types[$type])) { if (isset($this->types[$type])) {
if ($this->types[$type] === $id) { if ($this->types[$type] === $id) {
@ -188,12 +195,7 @@ class AutowirePass implements CompilerPassInterface
} }
// keep an array of all services matching this type // keep an array of all services matching this type
if (!isset($this->ambiguousServiceTypes[$type])) { $this->addServiceToAmbiguousType($id, $type);
$this->ambiguousServiceTypes[$type] = array(
$this->types[$type],
);
}
$this->ambiguousServiceTypes[$type][] = $id;
unset($this->types[$type]); unset($this->types[$type]);
@ -265,4 +267,15 @@ class AutowirePass implements CompilerPassInterface
// return null // return null
} }
} }
private function addServiceToAmbiguousType($id, $type)
{
// keep an array of all services matching this type
if (!isset($this->ambiguousServiceTypes[$type])) {
$this->ambiguousServiceTypes[$type] = array(
$this->types[$type],
);
}
$this->ambiguousServiceTypes[$type][] = $id;
}
} }

View File

@ -103,7 +103,7 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
/** /**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @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). * @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).
*/ */
public function testTypeCollision() public function testTypeCollision()
{ {
@ -111,6 +111,7 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
$container->register('c1', __NAMESPACE__.'\CollisionA'); $container->register('c1', __NAMESPACE__.'\CollisionA');
$container->register('c2', __NAMESPACE__.'\CollisionB'); $container->register('c2', __NAMESPACE__.'\CollisionB');
$container->register('c3', __NAMESPACE__.'\CollisionB');
$aDefinition = $container->register('a', __NAMESPACE__.'\CannotBeAutowired'); $aDefinition = $container->register('a', __NAMESPACE__.'\CannotBeAutowired');
$aDefinition->setAutowired(true); $aDefinition->setAutowired(true);