Skip to content

Commit 98934e4

Browse files
committed
bug #27191 [DI] Display previous error messages when throwing unused bindings (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [DI] Display previous error messages when throwing unused bindings | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | n | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27146 | License | MIT | Doc PR | - As reported by @jvasseur, confirmation + review welcome. Commits ------- f2231b5 [DI] Display previous error messages when throwing unused bindings
2 parents 7b7f759 + f2231b5 commit 98934e4

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,12 @@ protected function getConstructor(Definition $definition, $required)
118118

119119
$class = $definition->getClass();
120120

121-
if (!$r = $this->container->getReflectionClass($class)) {
122-
throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
121+
try {
122+
if (!$r = $this->container->getReflectionClass($class)) {
123+
throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
124+
}
125+
} catch (\ReflectionException $e) {
126+
throw new RuntimeException(sprintf('Invalid service "%s": %s.', $this->currentId, lcfirst(rtrim($e->getMessage(), '.'))));
123127
}
124128
if (!$r = $r->getConstructor()) {
125129
if ($required) {

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ResolveBindingsPass extends AbstractRecursivePass
2727
{
2828
private $usedBindings = array();
2929
private $unusedBindings = array();
30+
private $errorMessages = array();
3031

3132
/**
3233
* {@inheritdoc}
@@ -37,11 +38,19 @@ public function process(ContainerBuilder $container)
3738
parent::process($container);
3839

3940
foreach ($this->unusedBindings as list($key, $serviceId)) {
40-
throw new InvalidArgumentException(sprintf('Unused binding "%s" in service "%s".', $key, $serviceId));
41+
$message = sprintf('Unused binding "%s" in service "%s".', $key, $serviceId);
42+
if ($this->errorMessages) {
43+
$message .= sprintf("\nCould be related to%s:", 1 < \count($this->errorMessages) ? ' one of' : '');
44+
}
45+
foreach ($this->errorMessages as $m) {
46+
$message .= "\n - ".$m;
47+
}
48+
throw new InvalidArgumentException($message);
4149
}
4250
} finally {
4351
$this->usedBindings = array();
4452
$this->unusedBindings = array();
53+
$this->errorMessages = array();
4554
}
4655
}
4756

@@ -94,6 +103,7 @@ protected function processValue($value, $isRoot = false)
94103
$calls[] = array($constructor, $value->getArguments());
95104
}
96105
} catch (RuntimeException $e) {
106+
$this->errorMessages[] = $e->getMessage();
97107
$this->container->getDefinition($this->currentId)->addError($e->getMessage());
98108

99109
return parent::processValue($value, $isRoot);

src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveBindingsPassTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\DependencyInjection\ContainerBuilder;
1919
use Symfony\Component\DependencyInjection\Reference;
2020
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
21+
use Symfony\Component\DependencyInjection\Tests\Fixtures\ParentNotExists;
2122
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2223
use Symfony\Component\DependencyInjection\TypedReference;
2324

@@ -61,6 +62,21 @@ public function testUnusedBinding()
6162
$pass->process($container);
6263
}
6364

65+
/**
66+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
67+
* @expectedExceptionMessageRegexp Unused binding "$quz" in service [\s\S]+ Invalid service ".*\\ParentNotExists": class NotExists not found\.
68+
*/
69+
public function testMissingParent()
70+
{
71+
$container = new ContainerBuilder();
72+
73+
$definition = $container->register(ParentNotExists::class, ParentNotExists::class);
74+
$definition->setBindings(array('$quz' => '123'));
75+
76+
$pass = new ResolveBindingsPass();
77+
$pass->process($container);
78+
}
79+
6480
public function testTypedReferenceSupport()
6581
{
6682
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)