diff --git a/rules-tests/Configs/Rector/Closure/RemoveConstructorAutowireServiceRector/Fixture/named_class.php.inc b/rules-tests/Configs/Rector/Closure/RemoveConstructorAutowireServiceRector/Fixture/named_class.php.inc new file mode 100644 index 000000000..ac0977c7d --- /dev/null +++ b/rules-tests/Configs/Rector/Closure/RemoveConstructorAutowireServiceRector/Fixture/named_class.php.inc @@ -0,0 +1,42 @@ +services(); + + $services->defaults()->autowire(); + + $services->set('named-class', AnotherClassWithoutConstructor::class) + ->arg('$passedAsDependency', service(PassedAsDependency::class)); +}; + +?> +----- +services(); + + $services->defaults()->autowire(); + + $services->set('named-class', AnotherClassWithoutConstructor::class); +}; + +?> diff --git a/rules/Configs/Rector/Closure/RemoveConstructorAutowireServiceRector.php b/rules/Configs/Rector/Closure/RemoveConstructorAutowireServiceRector.php index 7f8372a7d..2c44e4cb8 100644 --- a/rules/Configs/Rector/Closure/RemoveConstructorAutowireServiceRector.php +++ b/rules/Configs/Rector/Closure/RemoveConstructorAutowireServiceRector.php @@ -182,13 +182,19 @@ private function matchSetServicesClass(MethodCall $methodCall): ?string $methodCall = $methodCall->var; } - /** @var MethodCall $methodCall */ - $firstArg = $methodCall->getArgs()[0]; - if (! $firstArg->value instanceof ClassConstFetch) { + if (! $methodCall instanceof MethodCall) { return null; } - return $this->valueResolver->getValue($firstArg->value); + foreach ($methodCall->getArgs() as $arg) { + if (! $arg->value instanceof ClassConstFetch) { + continue; + } + + return $this->valueResolver->getValue($arg->value); + } + + return null; } private function isParameterTypeMatchingPassedArgExprClass(