bug #22981 [DependencyInjection] Fix named args support in ChildDefinition (dunglas)

This PR was squashed before being merged into the 3.3 branch (closes #22981).

Discussion
----------

[DependencyInjection] Fix named args support in ChildDefinition

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Following @Tobion's review of #21383.

Commits
-------

1ab3e413d4 [DependencyInjection] Fix named args support in ChildDefinition
This commit is contained in:
Fabien Potencier 2017-06-05 09:24:57 -07:00
commit 1272d2ac8a
2 changed files with 9 additions and 11 deletions

View File

@ -62,7 +62,7 @@ class ChildDefinition extends Definition
* If replaceArgument() has been used to replace an argument, this method
* will return the replacement value.
*
* @param int $index
* @param int|string $index
*
* @return mixed The argument value
*
@ -74,13 +74,7 @@ class ChildDefinition extends Definition
return $this->arguments['index_'.$index];
}
$lastIndex = count(array_filter(array_keys($this->arguments), 'is_int')) - 1;
if ($index < 0 || $index > $lastIndex) {
throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, $lastIndex));
}
return $this->arguments[$index];
return parent::getArgument($index);
}
/**
@ -91,8 +85,8 @@ class ChildDefinition extends Definition
* certain conventions when you want to overwrite the arguments of the
* parent definition, otherwise your arguments will only be appended.
*
* @param int $index
* @param mixed $value
* @param int|string $index
* @param mixed $value
*
* @return self the current instance
*
@ -105,7 +99,7 @@ class ChildDefinition extends Definition
} elseif (0 === strpos($index, '$')) {
$this->arguments[$index] = $value;
} else {
throw new InvalidArgumentException('$index must be an integer.');
throw new InvalidArgumentException('The argument must be an existing index or the name of a constructor\'s parameter.');
}
return $this;

View File

@ -113,6 +113,10 @@ class ChildDefinitionTest extends TestCase
$this->assertSame('baz', $def->getArgument(1));
$this->assertSame(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz'), $def->getArguments());
$this->assertSame($def, $def->replaceArgument('$bar', 'val'));
$this->assertSame('val', $def->getArgument('$bar'));
$this->assertSame(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz', '$bar' => 'val'), $def->getArguments());
}
/**