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
feature #22256 [DI] Reduce complexity of autowiring (nicolas-grekas)
This PR was merged into the 3.3-dev branch.
Discussion
----------
[DI] Reduce complexity of autowiring
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | no (tweaking existing ones)
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
- optional args are autowired
- methods with only optional args are autowired
- default values of required args when possible
Both first changes remove the behavior delta between constructors and setters, and is expected to me now that things are more explicit. This reduces the "know-how" requirements for using autowiring and is easier to get correct intuitively. The 3rd change plays nice with named args.
Commits
-------
146f074 [DI] Reduce complexity of autowiring
if (!$isConstructor && !$arguments && !$reflectionMethod->getNumberOfRequiredParameters()) {
238
-
thrownewRuntimeException(sprintf('Cannot autowire service "%s": method %s() has only optional arguments, thus must be wired explicitly.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
239
-
}
240
235
$parameters = $reflectionMethod->getParameters();
241
236
if (method_exists('ReflectionMethod', 'isVariadic') && $reflectionMethod->isVariadic()) {
242
237
array_pop($parameters);
@@ -246,9 +241,6 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
246
241
if (array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
247
242
continue;
248
243
}
249
-
if (!$isConstructor && $parameter->isOptional() && !array_key_exists($index, $arguments)) {
@@ -258,7 +250,7 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
258
250
}
259
251
260
252
// no default value? Then fail
261
-
if (!$parameter->isOptional()) {
253
+
if (!$parameter->isDefaultValueAvailable()) {
262
254
thrownewRuntimeException(sprintf('Cannot autowire service "%s": argument $%s of method %s() must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
* @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.
@@ -659,10 +662,6 @@ public function provideNotWireableCalls()
659
662
{
660
663
returnarray(
661
664
array('setNotAutowireable', 'Cannot autowire service "foo": argument $n of method Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setNotAutowireable() has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class does not exist.'),
662
-
array('setBar', 'Cannot autowire service "foo": method Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setBar() has only optional arguments, thus must be wired explicitly.'),
663
-
array('setOptionalNotAutowireable', 'Cannot autowire service "foo": method Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setOptionalNotAutowireable() has only optional arguments, thus must be wired explicitly.'),
664
-
array('setOptionalNoTypeHint', 'Cannot autowire service "foo": method Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setOptionalNoTypeHint() has only optional arguments, thus must be wired explicitly.'),
665
-
array('setOptionalArgNoAutowireable', 'Cannot autowire service "foo": method Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setOptionalArgNoAutowireable() has only optional arguments, thus must be wired explicitly.'),
666
665
array(null, 'Cannot autowire service "foo": method Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setProtectedMethod() must be public.'),
0 commit comments