|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Symfony package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Argument\AbstractArgument; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Argument\BoundArgument; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Target; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
23
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
|
24
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
25
|
|
|
use Symfony\Component\DependencyInjection\TypedReference; |
|
26
|
|
|
use Symfony\Component\VarExporter\ProxyHelper; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @author Guilhem Niot <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class ResolveBindingsPass extends AbstractRecursivePass |
|
32
|
|
|
{ |
|
33
|
|
|
protected bool $skipScalars = true; |
|
34
|
|
|
|
|
35
|
|
|
private array $usedBindings = []; |
|
36
|
|
|
private array $unusedBindings = []; |
|
37
|
|
|
private array $errorMessages = []; |
|
38
|
|
|
|
|
39
|
|
|
public function process(ContainerBuilder $container): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this->usedBindings = $container->getRemovedBindingIds(); |
|
42
|
|
|
|
|
43
|
|
|
try { |
|
44
|
|
|
parent::process($container); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($this->unusedBindings as [$key, $serviceId, $bindingType, $file]) { |
|
47
|
|
|
$argumentType = $argumentName = $message = null; |
|
48
|
|
|
|
|
49
|
|
|
if (str_contains($key, ' ')) { |
|
50
|
|
|
[$argumentType, $argumentName] = explode(' ', $key, 2); |
|
51
|
|
|
} elseif ('$' === $key[0]) { |
|
52
|
|
|
$argumentName = $key; |
|
53
|
|
|
} else { |
|
54
|
|
|
$argumentType = $key; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($argumentType) { |
|
58
|
|
|
$message .= \sprintf('of type "%s" ', $argumentType); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($argumentName) { |
|
62
|
|
|
$message .= \sprintf('named "%s" ', $argumentName); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (BoundArgument::DEFAULTS_BINDING === $bindingType) { |
|
66
|
|
|
$message .= 'under "_defaults"'; |
|
67
|
|
|
} elseif (BoundArgument::INSTANCEOF_BINDING === $bindingType) { |
|
68
|
|
|
$message .= 'under "_instanceof"'; |
|
69
|
|
|
} else { |
|
70
|
|
|
$message .= \sprintf('for service "%s"', $serviceId); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($file) { |
|
74
|
|
|
$message .= \sprintf(' in file "%s"', $file); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$message = \sprintf('A binding is configured for an argument %s, but no corresponding argument has been found. It may be unused and should be removed, or it may have a typo.', $message); |
|
78
|
|
|
|
|
79
|
|
|
if ($this->errorMessages) { |
|
|
|
|
|
|
80
|
|
|
$message .= \sprintf("\nCould be related to%s:", 1 < \count($this->errorMessages) ? ' one of' : ''); |
|
81
|
|
|
} |
|
82
|
|
|
foreach ($this->errorMessages as $m) { |
|
83
|
|
|
$message .= "\n - ".$m; |
|
84
|
|
|
} |
|
85
|
|
|
throw new InvalidArgumentException($message); |
|
86
|
|
|
} |
|
87
|
|
|
} finally { |
|
88
|
|
|
$this->usedBindings = []; |
|
89
|
|
|
$this->unusedBindings = []; |
|
90
|
|
|
$this->errorMessages = []; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function processValue(mixed $value, bool $isRoot = false): mixed |
|
95
|
|
|
{ |
|
96
|
|
|
if ($value instanceof TypedReference && $value->getType() === (string) $value) { |
|
97
|
|
|
// Already checked |
|
98
|
|
|
$bindings = $this->container->getDefinition($this->currentId)->getBindings(); |
|
|
|
|
|
|
99
|
|
|
$name = $value->getName(); |
|
100
|
|
|
|
|
101
|
|
|
if (isset($name, $bindings[$name = $value.' $'.$name])) { |
|
102
|
|
|
return $this->getBindingValue($bindings[$name]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if (isset($bindings[$value->getType()])) { |
|
106
|
|
|
return $this->getBindingValue($bindings[$value->getType()]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return parent::processValue($value, $isRoot); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (!$value instanceof Definition || !$bindings = $value->getBindings()) { |
|
113
|
|
|
return parent::processValue($value, $isRoot); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$bindingNames = []; |
|
117
|
|
|
|
|
118
|
|
|
foreach ($bindings as $key => $binding) { |
|
119
|
|
|
[$bindingValue, $bindingId, $used, $bindingType, $file] = $binding->getValues(); |
|
120
|
|
|
if ($used) { |
|
121
|
|
|
$this->usedBindings[$bindingId] = true; |
|
122
|
|
|
unset($this->unusedBindings[$bindingId]); |
|
123
|
|
|
} elseif (!isset($this->usedBindings[$bindingId])) { |
|
124
|
|
|
$this->unusedBindings[$bindingId] = [$key, $this->currentId, $bindingType, $file]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if (preg_match('/^(?:(?:array|bool|float|int|string|iterable|([^ $]++)) )\$/', $key, $m)) { |
|
128
|
|
|
$bindingNames[substr($key, \strlen($m[0]))] = $binding; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (!isset($m[1])) { |
|
132
|
|
|
continue; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (is_subclass_of($m[1], \UnitEnum::class)) { |
|
|
|
|
|
|
136
|
|
|
$bindingNames[substr($key, \strlen($m[0]))] = $binding; |
|
137
|
|
|
continue; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument && !$bindingValue instanceof ServiceLocatorArgument) { |
|
141
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid value for binding key "%s" for service "%s": expected "%s", "%s", "%s", "%s" or null, "%s" given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, ServiceLocatorArgument::class, get_debug_type($bindingValue))); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if ($value->isAbstract()) { |
|
146
|
|
|
return parent::processValue($value, $isRoot); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$calls = $value->getMethodCalls(); |
|
150
|
|
|
|
|
151
|
|
|
try { |
|
152
|
|
|
if ($constructor = $this->getConstructor($value, false)) { |
|
153
|
|
|
$calls[] = [$constructor, $value->getArguments()]; |
|
154
|
|
|
} |
|
155
|
|
|
} catch (RuntimeException $e) { |
|
156
|
|
|
$this->errorMessages[] = $e->getMessage(); |
|
157
|
|
|
$this->container->getDefinition($this->currentId)->addError($e->getMessage()); |
|
158
|
|
|
|
|
159
|
|
|
return parent::processValue($value, $isRoot); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
foreach ($calls as $i => $call) { |
|
163
|
|
|
[$method, $arguments] = $call; |
|
164
|
|
|
|
|
165
|
|
|
if ($method instanceof \ReflectionFunctionAbstract) { |
|
166
|
|
|
$reflectionMethod = $method; |
|
167
|
|
|
} else { |
|
168
|
|
|
try { |
|
169
|
|
|
$reflectionMethod = $this->getReflectionMethod($value, $method); |
|
170
|
|
|
} catch (RuntimeException $e) { |
|
171
|
|
|
if ($value->getFactory()) { |
|
172
|
|
|
continue; |
|
173
|
|
|
} |
|
174
|
|
|
throw $e; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$names = []; |
|
179
|
|
|
|
|
180
|
|
|
foreach ($reflectionMethod->getParameters() as $key => $parameter) { |
|
181
|
|
|
$names[$key] = $parameter->name; |
|
182
|
|
|
|
|
183
|
|
|
if (\array_key_exists($key, $arguments) && '' !== $arguments[$key] && !$arguments[$key] instanceof AbstractArgument) { |
|
184
|
|
|
continue; |
|
185
|
|
|
} |
|
186
|
|
|
if (\array_key_exists($parameter->name, $arguments) && '' !== $arguments[$parameter->name] && !$arguments[$parameter->name] instanceof AbstractArgument) { |
|
187
|
|
|
continue; |
|
188
|
|
|
} |
|
189
|
|
|
if ( |
|
190
|
|
|
$value->isAutowired() |
|
191
|
|
|
&& !$value->hasTag('container.ignore_attributes') |
|
192
|
|
|
&& $parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF) |
|
193
|
|
|
) { |
|
194
|
|
|
continue; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$typeHint = ltrim(ProxyHelper::exportType($parameter) ?? '', '?'); |
|
198
|
|
|
|
|
199
|
|
|
$name = Target::parseName($parameter, parsedName: $parsedName); |
|
200
|
|
|
|
|
201
|
|
|
if ($typeHint && ( |
|
202
|
|
|
\array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\1', $typeHint).' $'.$name, $bindings) |
|
203
|
|
|
|| \array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\1', $typeHint).' $'.$parsedName, $bindings) |
|
204
|
|
|
)) { |
|
205
|
|
|
$arguments[$key] = $this->getBindingValue($bindings[$k]); |
|
206
|
|
|
|
|
207
|
|
|
continue; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
if (\array_key_exists($k = '$'.$name, $bindings) || \array_key_exists($k = '$'.$parsedName, $bindings)) { |
|
211
|
|
|
$arguments[$key] = $this->getBindingValue($bindings[$k]); |
|
212
|
|
|
|
|
213
|
|
|
continue; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
if ($typeHint && '\\' === $typeHint[0] && isset($bindings[$typeHint = substr($typeHint, 1)])) { |
|
217
|
|
|
$arguments[$key] = $this->getBindingValue($bindings[$typeHint]); |
|
218
|
|
|
|
|
219
|
|
|
continue; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
if (isset($bindingNames[$name]) || isset($bindingNames[$parsedName]) || isset($bindingNames[$parameter->name])) { |
|
223
|
|
|
$bindingKey = array_search($binding, $bindings, true); |
|
|
|
|
|
|
224
|
|
|
$argumentType = substr($bindingKey, 0, strpos($bindingKey, ' ')); |
|
225
|
|
|
$this->errorMessages[] = \sprintf('Did you forget to add the type "%s" to argument "$%s" of method "%s::%s()"?', $argumentType, $parameter->name, $reflectionMethod->class, $reflectionMethod->name); |
|
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
foreach ($names as $key => $name) { |
|
230
|
|
|
if (\array_key_exists($name, $arguments) && (0 === $key || \array_key_exists($key - 1, $arguments))) { |
|
231
|
|
|
if (!array_key_exists($key, $arguments)) { |
|
232
|
|
|
$arguments[$key] = $arguments[$name]; |
|
233
|
|
|
} |
|
234
|
|
|
unset($arguments[$name]); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
if ($arguments !== $call[1]) { |
|
239
|
|
|
ksort($arguments, \SORT_NATURAL); |
|
240
|
|
|
$calls[$i][1] = $arguments; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
if ($constructor) { |
|
245
|
|
|
[, $arguments] = array_pop($calls); |
|
246
|
|
|
|
|
247
|
|
|
if ($arguments !== $value->getArguments()) { |
|
248
|
|
|
$value->setArguments($arguments); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
if ($calls !== $value->getMethodCalls()) { |
|
253
|
|
|
$value->setMethodCalls($calls); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return parent::processValue($value, $isRoot); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
private function getBindingValue(BoundArgument $binding): mixed |
|
260
|
|
|
{ |
|
261
|
|
|
[$bindingValue, $bindingId] = $binding->getValues(); |
|
262
|
|
|
|
|
263
|
|
|
$this->usedBindings[$bindingId] = true; |
|
264
|
|
|
unset($this->unusedBindings[$bindingId]); |
|
265
|
|
|
|
|
266
|
|
|
return $bindingValue; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths