8000 Fixes issues #27828 and #28326 · symfony/symfony@08163ae · GitHub
[go: up one dir, main page]

Skip to content

Commit 08163ae

Browse files
Fixes issues #27828 and #28326
1 parent 0f0c30a commit 08163ae

File tree

9 files changed

+270
-147
lines changed

9 files changed

+270
-147
lines changed

src/Symfony/Component/DependencyInjection/Argument/BoundArgument.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,37 @@
1616
*/
1717
final class BoundArgument implements ArgumentInterface
1818
{
19+
const SERVICE_BIND = 0;
20+
const DEFAULT_BIND = 1;
21+
const INSTANCE_BIND = 2;
22+
23+
const MESSAGES = [
24+
1 => 'under "_defaults"',
25+
2 => 'under "_instanceof"',
26+
];
27+
1928
private static $sequence = 0;
2029

2130
private $value;
2231
private $identifier;
2332
private $used;
33+
private $type;
34+
private $file;
2435

25-
public function __construct($value)
36+
public function __construct($value, $type = 0, $file = null)
2637
{
2738
$this->value = $value;
2839
$this->identifier = ++self::$sequence;
40+
$this->type = (int) $type;
41+
$this->file = (string) $file;
2942
}
3043

3144
/**
3245
* {@inheritdoc}
3346
*/
3447
public function getValues()
3548
{
36-
return array($this->value, $this->identifier, $this->used);
49+
return [$this->value, $this->identifier, $this->used, $this->type, $this->file];
3750
}
3851

3952
/**

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

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,43 @@
2525
*/
2626
class ResolveBindingsPass extends AbstractRecursivePass
2727
{
28-
private $usedBindings = array();
29-
private $unusedBindings = array();
30-
private $errorMessages = array();
28+
private $usedBindings = [];
29+
private $unusedBindings = [];
30+
private $errorMessages = [];
3131

3232
/**
3333
* {@inheritdoc}
3434
*/
3535
public function process(ContainerBuilder $container)
3636
{
37+
foreach ($container->getBindings() as $definition) {
38+
foreach ($definition as $argument => $values) {
39+
if (1 < \count($values)) {
40+
foreach (\array_slice(array_keys($values), 0, -1) as $value) {
41+
$this->usedBindings[$value] = true;
42+
}
43+
}
44+
}
45+
}
46+
3747
try {
3848
parent::process($container);
3949

40-
foreach ($this->unusedBindings as list($key, $serviceId)) {
41-
$message = sprintf('Unused binding "%s" in service "%s".', $key, $serviceId);
50+
foreach ($this->unusedBindings as list($key, $serviceId, $type, $file)) {
51+
$message = sprintf('You have a "bind" configured for an argument named "%s" ', $key);
52+
53+
if ($type) {
54+
$message .= BoundArgument::MESSAGES[$type];
55+
} else {
56+
$message .= sprintf('in service "%s"', $serviceId);
57+
}
58+
59+
if ($file) {
60+
$message .= sprintf(' in file "%s"', $file);
61+
}
62+
63+
$message .= '. But, this argument was not found in any of the services it was applied to. It may be unused and can be removed, or it may have a typo.';
64+
4265
if ($this->errorMessages) {
4366
$message .= sprintf("\nCould be related to%s:", 1 < \count($this->errorMessages) ? ' one of' : '');
4467
}
@@ -48,9 +71,9 @@ public function process(ContainerBuilder $container)
4871
throw new InvalidArgumentException($message);
4972
}
5073
} finally {
51-
$this->usedBindings = array();
52-
$this->unusedBindings = array();
53-
$this->errorMessages = array();
74+
$this->usedBindings = [];
75+
$this->unusedBindings = [];
76+
$this->errorMessages = [];
5477
}
5578
}
5679

@@ -75,12 +98,12 @@ protected function processValue($value, $isRoot = false)
7598
}
7699

77100
foreach ($bindings as $key => $binding) {
78-
list($bindingValue, $bindingId, $used) = $binding->getValues();
101+
list($bindingValue, $bindingId, $used, $bindingType, $file) = $binding->getValues();
79102
if ($used) {
80103
$this->usedBindings[$bindingId] = true;
81104
unset($this->unusedBindings[$bindingId]);
82105
} elseif (!isset($this->usedBindings[$bindingId])) {
83-
$this->unusedBindings[$bindingId] = array($key, $this->currentId);
106+
$this->unusedBindings[$bindingId] = [$key, $this->currentId, $bindingType, $file];
84107
}
85108

86109
if (isset($key[0]) && '$' === $key[0]) {
@@ -100,7 +123,7 @@ protected function processValue($value, $isRoot = false)
100123

101124
try {
102125
if ($constructor = $this->getConstructor($value, false)) {
103-
$calls[] = array($constructor, $value->getArguments());
126+
$calls[] = [$constructor, $value->getArguments()];
104127
}
105128
} catch (RuntimeException $e) {
106129
$this->errorMessages[] = $e->getMessage();

0 commit comments

Comments
 (0)
0