From 2aea337f42cffed356250c36add8b25c691d315d Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sun, 6 Mar 2016 17:03:14 -0500 Subject: [PATCH] Fixing a bug where an odd number of type collisions would incorrectly autowire (instead of an error) --- .../Compiler/AutowirePass.php | 25 ++++++++++++++----- .../Tests/Compiler/AutowirePassTest.php | 3 ++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index cbf6a678a4..03ef374e5d 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -181,6 +181,13 @@ class AutowirePass implements CompilerPassInterface 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 if (isset($this->types[$type])) { if ($this->types[$type] === $id) { @@ -188,12 +195,7 @@ class AutowirePass implements CompilerPassInterface } // 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; + $this->addServiceToAmbiguousType($id, $type); unset($this->types[$type]); @@ -265,4 +267,15 @@ class AutowirePass implements CompilerPassInterface // 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; + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 658b66131b..00dbaef3b7 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -103,7 +103,7 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase /** * @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() { @@ -111,6 +111,7 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase $container->register('c1', __NAMESPACE__.'\CollisionA'); $container->register('c2', __NAMESPACE__.'\CollisionB'); + $container->register('c3', __NAMESPACE__.'\CollisionB'); $aDefinition = $container->register('a', __NAMESPACE__.'\CannotBeAutowired'); $aDefinition->setAutowired(true);