[DependencyInjection] Fix named args support in ChildDefinition

This commit is contained in:
Kévin Dunglas 2017-05-31 08:51:10 +02:00 committed by Fabien Potencier
parent adc39a2f57
commit 1ab3e413d4
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 * If replaceArgument() has been used to replace an argument, this method
* will return the replacement value. * will return the replacement value.
* *
* @param int $index * @param int|string $index
* *
* @return mixed The argument value * @return mixed The argument value
* *
@ -74,13 +74,7 @@ class ChildDefinition extends Definition
return $this->arguments['index_'.$index]; return $this->arguments['index_'.$index];
} }
$lastIndex = count(array_filter(array_keys($this->arguments), 'is_int')) - 1; return parent::getArgument($index);
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];
} }
/** /**
@ -91,7 +85,7 @@ class ChildDefinition extends Definition
* certain conventions when you want to overwrite the arguments of the * certain conventions when you want to overwrite the arguments of the
* parent definition, otherwise your arguments will only be appended. * parent definition, otherwise your arguments will only be appended.
* *
* @param int $index * @param int|string $index
* @param mixed $value * @param mixed $value
* *
* @return self the current instance * @return self the current instance
@ -105,7 +99,7 @@ class ChildDefinition extends Definition
} elseif (0 === strpos($index, '$')) { } elseif (0 === strpos($index, '$')) {
$this->arguments[$index] = $value; $this->arguments[$index] = $value;
} else { } 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; return $this;

View File

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