10000 [DependencyInjection] Fix autowiring collisions detection · symfony/symfony@bb70472 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb70472

Browse files
committed
[DependencyInjection] Fix autowiring collisions detection
1 parent 6f578ee commit bb70472

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class AutowirePass implements CompilerPassInterface
2828
private $definedTypes = array();
2929
private $types;
3030
private $notGuessableTypes = array();
31+
private $usedTypes = array();
3132

3233
/**
3334
* {@inheritdoc}
@@ -44,6 +45,15 @@ public function process(ContainerBuilder $container)
4445
$this->completeDefinition($id, $definition);
4546
}
4647
}
48+
49+
foreach ($this->usedTypes as $type => $id) {
50+
if (isset($this->usedTypes[$type]) && isset($this->notGuessableTypes[$type])) {
51+
$classOrInterface = class_exists($type) ? 'class' : 'interface';
52+
$matchingServices = implode(', ', $this->types[$type]);
53+
54+
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $type, $id, $classOrInterface, $matchingServices));
55+
}
56+
}
4757
} catch (\Exception $e) {
4858
} catch (\Throwable $e) {
4959
}
@@ -56,6 +66,7 @@ public function process(ContainerBuilder $container)
5666
$this->definedTypes = array();
5767
$this->types = null;
5868
$this->notGuessableTypes = array();
69+
$this->usedTypes = array();
5970

6071
if (isset($e)) {
6172
throw $e;
@@ -109,9 +120,11 @@ private function completeDefinition($id, Definition $definition)
109120

110121
if (isset($this->types[$typeHint->name]) && !isset($this->notGuessableTypes[$typeHint->name])) {
111122
$value = new Reference($this->types[$typeHint->name]);
123+
$this->usedTypes[$typeHint->name] = $id;
112124
} else {
113125
try {
114126
$value = $this->createAutowiredDefinition($typeHint, $id);
127+
$this->usedTypes[$typeHint->name] = $id;
115128
} catch (RuntimeException $e) {
116129
if ($parameter->allowsNull()) {
117130
$value = null;

0 commit comments

Comments
 (0)
0