Skip to content

[DependencyInjection] Fix manual ordering of arguments #21700

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

Closed
wants to merge 3 commits into from
Closed
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 @@ -45,11 +45,19 @@ protected function processValue($value, $isRoot = false)
$parameters = null;
$resolvedArguments = array();

$previousIndex = null;
foreach ($arguments as $key => $argument) {
if (is_int($key) || '' === $key || '$' !== $key[0]) {
if (!is_int($key)) {
@trigger_error(sprintf('Using key "%s" for defining arguments of method "%s" for service "%s" is deprecated since Symfony 3.3 and will throw an exception in 4.0. Use no keys or $named arguments instead.', $key, $method, $this->currentId), E_USER_DEPRECATED);
} else {
if (null !== $previousIndex && $key < $previousIndex) {
@trigger_error(sprintf('Using key "%s" after the key "%s" for defining arguments of method "%s" for service "%s" is deprecated since Symfony 3.3. They will be automatically reordered in 4.0.', $key, $previousIndex, $method, $this->currentId), E_USER_DEPRECATED);
} else {
$previousIndex = $key;
}
}

$resolvedArguments[] = $argument;
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ public function testArgumentNotFound()
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
}

/**
* @group legacy
* @expectedDeprecation Using key "0" after the key "1" for defining arguments of method "__construct" for service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy" is deprecated since Symfony 3.3. They will be automatically reordered in 4.0.
*/
public function testArgumentsWithManualIndexes()
{
$container = new ContainerBuilder();

$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
$definition->setArguments(array(1 => '123', 0 => new Reference('foo')));

$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
}

public function testCase()
{
$container = new ContainerBuilder();

$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
$definition->setArguments(array('$apiKey' => '123', 0 => new Reference('foo')));

$pass = new ResolveNamedArgumentsPass();
$pass->process($container);

$this->assertEquals(array(1 => '123', 2 => new Reference('foo')), $definition->getArguments());
}
}

class NoConstructor
Expand Down