Skip to content

Commit

Permalink
bug #22677 [DI] Fixed index args bug with ResolveNamedArgumentsPass (…
Browse files Browse the repository at this point in the history
…weaverryan)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Fixed index args bug with ResolveNamedArgumentsPass

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | none
| License       | MIT
| Doc PR        | n/a

While upgrading a project, this code suddenly broke:

```yml
services:
    ice_cream_service:
        class: AppBundle\Service\IceCreamService
        autowire: true
        arguments:
            1: 'http://api.example.com'
```

```php
class IceCreamService
{
    public function __construct(EntityManager $em, $apiUrl)
    {
    }
}
```

Suddenly, the index `1` was not being mapped to `$apiUrl`. This was valid in 3.2, but broke when `ResolveNamedArgumentsPass` accidentally re-set the index. Simple fix :). Ping @dunglas

Cheers!

Commits
-------

7cc7c85 Fixing bug where indexed args were set wrong in pass in some situations
  • Loading branch information
fabpot committed May 9, 2017
2 parents 3646d08 + 7cc7c85 commit ca509ea
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
Expand Up @@ -40,7 +40,7 @@ protected function processValue($value, $isRoot = false)

foreach ($arguments as $key => $argument) {
if (is_int($key)) {
$resolvedArguments[] = $argument;
$resolvedArguments[$key] = $argument;
continue;
}
if ('' === $key || '$' !== $key[0]) {
Expand Down
Expand Up @@ -27,13 +27,17 @@ public function testProcess()
$container = new ContainerBuilder();

$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
$definition->setArguments(array(0 => new Reference('foo'), '$apiKey' => '123'));
$definition->setArguments(array(
2 => 'http://api.example.com',
'$apiKey' => '123',
0 => new Reference('foo'),
));
$definition->addMethodCall('setApiKey', array('$apiKey' => '123'));

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

$this->assertEquals(array(0 => new Reference('foo'), 1 => '123'), $definition->getArguments());
$this->assertEquals(array(0 => new Reference('foo'), 1 => '123', 2 => 'http://api.example.com'), $definition->getArguments());
$this->assertEquals(array(array('setApiKey', array('123'))), $definition->getMethodCalls());
}

Expand Down
Expand Up @@ -7,7 +7,7 @@
*/
class NamedArgumentsDummy
{
public function __construct(CaseSensitiveClass $c, $apiKey)
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName)
{
}

Expand Down

0 comments on commit ca509ea

Please sign in to comment.