Skip to content

Commit 33f31e9

Browse files
dunglasfabpot
authored andcommitted
[DependencyInjection] Fix named args support in ChildDefinition
1 parent 988c7bd commit 33f31e9

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

ChildDefinition.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function setParent($parent)
6262
* If replaceArgument() has been used to replace an argument, this method
6363
* will return the replacement value.
6464
*
65-
* @param int $index
65+
* @param int|string $index
6666
*
6767
* @return mixed The argument value
6868
*
@@ -74,13 +74,7 @@ public function getArgument($index)
7474
return $this->arguments['index_'.$index];
7575
}
7676

77-
$lastIndex = count(array_filter(array_keys($this->arguments), 'is_int')) - 1;
78-
79-
if ($index < 0 || $index > $lastIndex) {
80-
throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, $lastIndex));
81-
}
82-
83-
return $this->arguments[$index];
77+
return parent::getArgument($index);
8478
}
8579

8680
/**
@@ -91,8 +85,8 @@ public function getArgument($index)
9185
* certain conventions when you want to overwrite the arguments of the
9286
* parent definition, otherwise your arguments will only be appended.
9387
*
94-
* @param int $index
95-
* @param mixed $value
88+
* @param int|string $index
89+
* @param mixed $value
9690
*
9791
* @return self the current instance
9892
*
@@ -105,7 +99,7 @@ public function replaceArgument($index, $value)
10599
} elseif (0 === strpos($index, '$')) {
106100
$this->arguments[$index] = $value;
107101
} else {
108-
throw new InvalidArgumentException('$index must be an integer.');
102+
throw new InvalidArgumentException('The argument must be an existing index or the name of a constructor\'s parameter.');
109103
}
110104

111105
return $this;

Tests/ChildDefinitionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public function testReplaceArgument()
113113
$this->assertSame('baz', $def->getArgument(1));
114114

115115
$this->assertSame(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz'), $def->getArguments());
116+
117+
$this->assertSame($def, $def->replaceArgument('$bar', 'val'));
118+
$this->assertSame('val', $def->getArgument('$bar'));
119+
$this->assertSame(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz', '$bar' => 'val'), $def->getArguments());
116120
}
117121

118122
/**

0 commit comments

Comments
 (0)