You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
thrownewRuntimeException(sprintf('Cannot autowire service "%s": class %s has no constructor but arguments are defined.', $this->currentId, $reflectionClass->name, $method));
if (array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
172
220
continue;
@@ -176,11 +224,11 @@ private function autowireMethod(Definition $definition, \ReflectionMethod $refle
176
224
if (!$typeHint = $parameter->getClass()) {
177
225
// no default value? Then fail
178
226
if (!$parameter->isOptional()) {
179
-
if ($isConstructor) {
180
-
thrownewRuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $this->currentId));
227
+
if ($mustAutowire) {
228
+
thrownewRuntimeException(sprintf('Cannot autowire service "%s": argument $%s of method %s::%s() must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $reflectionMethod->class, $reflectionMethod->name));
181
229
}
182
230
183
-
return;
231
+
returnarray();
184
232
}
185
233
186
234
// specifically pass the default value
@@ -195,35 +243,35 @@ private function autowireMethod(Definition $definition, \ReflectionMethod $refle
// The exception code is set to 1 if the exception must be thrown even if it's a setter
210
-
if (1 === $e->getCode() || $isConstructor) {
257
+
// The exception code is set to 1 if the exception must be thrown even if it's an optional setter
258
+
if (1 === $e->getCode() || $mustAutowire) {
211
259
throw$e;
212
260
}
213
261
214
-
return;
262
+
returnarray();
215
263
}
216
264
}
217
265
}
218
266
} catch (\ReflectionException$e) {
219
267
// Typehint against a non-existing class
220
268
221
269
if (!$parameter->isDefaultValueAvailable()) {
222
-
if ($isConstructor) {
223
-
thrownewRuntimeException(sprintf('Cannot autowire argument %s for %s because the type-hinted class does not exist (%s).', $index + 1, $definition->getClass(), $e->getMessage()), 0, $e);
270
+
if ($mustAutowire) {
271
+
thrownewRuntimeException(sprintf('Cannot autowire argument $%s of method %s::%s() for service "%s": %s.', $parameter->name, $reflectionMethod->class, $reflectionMethod->name, $this->currentId, $e->getMessage()), 0, $e);
224
272
}
225
273
226
-
return;
274
+
returnarray();
227
275
}
228
276
229
277
$value = $parameter->getDefaultValue();
@@ -232,15 +280,15 @@ private function autowireMethod(Definition $definition, \ReflectionMethod $refle
232
280
$arguments[$index] = $value;
233
281
}
234
282
283
+
if (!$mustAutowire && !$didAutowire) {
284
+
returnarray();
285
+
}
286
+
235
287
// it's possible index 1 was set, then index 0, then 2, etc
236
288
// make sure that we re-order so they're injected as expected
* @expectedExceptionMessage Cannot autowire argument 2 for Symfony\Component\DependencyInjection\Tests\Compiler\BadTypeHintedArgument because the type-hinted class does not exist (Class Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass does not exist).
292
+
* @expectedExceptionMessage Cannot autowire argument $r of method Symfony\Component\DependencyInjection\Tests\Compiler\BadTypeHintedArgument::__construct() for service "a": Class Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass does not exist.
293
293
*/
294
294
publicfunctiontestClassNotFoundThrowsException()
295
295
{
@@ -304,7 +304,7 @@ public function testClassNotFoundThrowsException()
* @expectedExceptionMessage Cannot autowire argument 2 for Symfony\Component\DependencyInjection\Tests\Compiler\BadParentTypeHintedArgument because the type-hinted class does not exist (Class Symfony\Component\DependencyInjection\Tests\Compiler\OptionalServiceClass does not exist).
307
+
* @expectedExceptionMessage Cannot autowire argument $r of method Symfony\Component\DependencyInjection\Tests\Compiler\BadParentTypeHintedArgument::__construct() for service "a": Class Symfony\Component\DependencyInjection\Tests\Compiler\OptionalServiceClass does not exist.
* @expectedExceptionMessage Unable to autowire argument index 1 ($foo) for the service "arg_no_type_hint". If this is an object, give it a type-hint. Otherwise, specify this argument's value explicitly.
366
+
* @expectedExceptionMessage Cannot autowire service "arg_no_type_hint": argument $foo of method Symfony\Component\DependencyInjection\Tests\Compiler\MultipleArguments::__construct() must have a type-hint or be given a value explicitly.
367
367
*/
368
368
publicfunctiontestScalarArgsCannotBeAutowired()
369
369
{
@@ -382,7 +382,7 @@ public function testScalarArgsCannotBeAutowired()
* @expectedExceptionMessage Unable to autowire argument index 1 ($foo) for the service "not_really_optional_scalar". If this is an object, give it a type-hint. Otherwise, specify this argument's value explicitly.
385
+
* @expectedExceptionMessage Cannot autowire service "not_really_optional_scalar": argument $foo of method Symfony\Component\DependencyInjection\Tests\Compiler\MultipleArgumentsOptionalScalarNotReallyOptional::__construct() must have a type-hint or be given a value explicitly.
@@ -593,6 +593,22 @@ public function testLogUnusedPatterns()
593
593
594
594
$this->assertEquals(array(AutowirePass::class.': Autowiring\'s patterns "not", "exist*" for service "foo" don\'t match any method.'), $container->getCompiler()->getLog());
0 commit comments