Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DependencyInjection] Fix support for named arguments on non-autowired services #48926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected function processValue($value, bool $isRoot = false)
foreach ($calls as $i => $call) {
[$method, $arguments] = $call;
$parameters = null;
$resolvedKeys = [];
$resolvedArguments = [];

foreach ($arguments as $key => $argument) {
Expand All @@ -51,6 +52,7 @@ protected function processValue($value, bool $isRoot = false)
}

if (\is_int($key)) {
$resolvedKeys[$key] = $key;
$resolvedArguments[$key] = $argument;
continue;
}
Expand All @@ -71,9 +73,11 @@ protected function processValue($value, bool $isRoot = false)
if ($key === '$'.$p->name) {
if ($p->isVariadic() && \is_array($argument)) {
foreach ($argument as $variadicArgument) {
$resolvedKeys[$j] = $j;
$resolvedArguments[$j++] = $variadicArgument;
}
} else {
$resolvedKeys[$j] = $p->name;
$resolvedArguments[$j] = $argument;
}

Expand All @@ -91,6 +95,7 @@ protected function processValue($value, bool $isRoot = false)
$typeFound = false;
foreach ($parameters as $j => $p) {
if (!\array_key_exists($j, $resolvedArguments) && ProxyHelper::getTypeHint($r, $p, true) === $key) {
$resolvedKeys[$j] = $p->name;
$resolvedArguments[$j] = $argument;
$typeFound = true;
}
Expand All @@ -103,6 +108,12 @@ protected function processValue($value, bool $isRoot = false)

if ($resolvedArguments !== $call[1]) {
ksort($resolvedArguments);

if (!$value->isAutowired() && !array_is_list($resolvedArguments)) {
ksort($resolvedKeys);
$resolvedArguments = array_combine($resolvedKeys, $resolvedArguments);
}

$calls[$i][1] = $resolvedArguments;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ private function createService(Definition $definition, array &$inlineServices, b
} else {
$r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass()));

$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs(array_values($arguments));
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);

if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
trigger_deprecation('', '', 'The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function testInterfaceTypedArgument()
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);

$this->assertSame($expected, $definition->getArgument(3));
$this->assertSame($expected, $definition->getArgument('container'));
}

public function testResolvesMultipleArgumentsOfTheSameType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,24 @@ public function testFindTags()

$this->assertSame(['tag1', 'tag2', 'tag3'], $container->findTags());
}

/**
* @requires PHP 8
*/
public function testNamedArgument()
{
$container = new ContainerBuilder();
$container->register(E::class)
->setPublic(true)
->setArguments(['$second' => 2]);

$container->compile();

$e = $container->get(E::class);

$this->assertSame('', $e->first);
$this->assertSame(2, $e->second);
}
}

class FooClass
Expand Down Expand Up @@ -1801,3 +1819,15 @@ class C implements X
class D implements X
{
}

class E
{
public $first;
public $second;

public function __construct($first = '', $second = '')
{
$this->first = $first;
$this->second = $second;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.