From c0c08dbb79ab8ad7b8b2f700975101febf4f59f6 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 15 Nov 2017 18:44:00 +0200 Subject: [PATCH] [DI] Fix by-type args injection --- .../DependencyInjection/Compiler/PassConfig.php | 2 +- .../Compiler/ResolveNamedArgumentsPass.php | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php b/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php index d01385107fc92..b5959618ed2f8 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php @@ -57,9 +57,9 @@ public function __construct() new FactoryReturnTypePass($resolveClassPass), new CheckDefinitionValidityPass(), new RegisterServiceSubscribersPass(), - new ResolveNamedArgumentsPass(), new AutowireRequiredMethodsPass(), new ResolveBindingsPass(), + new ResolveNamedArgumentsPass(), new AutowirePass(false), new ResolveTaggedIteratorArgumentPass(), new ResolveServiceSubscribersPass(), diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php index a96f981d074b4..cd6bb6fe8c58a 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php @@ -68,15 +68,17 @@ protected function processValue($value, $isRoot = false) throw new InvalidArgumentException(sprintf('Invalid service "%s": the value of argument "%s" of method "%s()" must be null, an instance of %s or an instance of %s, %s given.', $this->currentId, $key, $class !== $this->currentId ? $class.'::'.$method : $method, Reference::class, Definition::class, gettype($argument))); } + $typeFound = false; foreach ($parameters as $j => $p) { - if (ProxyHelper::getTypeHint($r, $p, true) === $key) { + if (!array_key_exists($j, $resolvedArguments) && ProxyHelper::getTypeHint($r, $p, true) === $key) { $resolvedArguments[$j] = $argument; - - continue 2; + $typeFound = true; } } - throw new InvalidArgumentException(sprintf('Invalid service "%s": method "%s()" has no argument type-hinted as "%s". Check your service definition.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method, $key)); + if (!$typeFound) { + throw new InvalidArgumentException(sprintf('Invalid service "%s": method "%s()" has no argument type-hinted as "%s". Check your service definition.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method, $key)); + } } if ($resolvedArguments !== $call[1]) {