8000 [DependencyInjection] Fix DefinitionDecorator::getArgument() for repl… · Kiruban2011/symfony@80f0b98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 80f0b98

Browse files
committed
[DependencyInjection] Fix DefinitionDecorator::getArgument() for replacements
While Definition::getArgument() could be used to fetch replaced values, it relied upon bad comparison logic (e.g. "index_1" > 1). Additionally, storing original arguments and replacements in the same array makes Definition::getArguments()'s bounds check unreliable. A single argument and its replacement would count twice, allowing getArgument(2) to pass the bounds check and result in an array index error. With this new method, fetching of replacement arguments is more straightforward and bounds checking functions as it should.
1 parent 4bbb685 commit 80f0b98

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/Symfony/Component/DependencyInjection/DefinitionDecorator.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,33 @@ public function setPublic($boolean)
146146
return parent::setPublic($boolean);
147147
}
148148

149+
/**
150+
* Gets an argument to pass to the service constructor/factory method.
151+
*
152+
* If replaceArgument() has been used to replace an argument, this method
153+
* will return the replacement value.
154+
*
155+
* @param integer $index
156+
*
157+
* @return mixed The argument value
158+
*
159+
* @api
160+
*/
161+
public function getArgument($index)
162+
{
163+
if (array_key_exists('index_'.$index, $this->arguments)) {
164+
return $this->arguments['index_'.$index];
165+
}
166+
167+
$lastIndex = count(array_filter(array_keys($this->arguments), 'is_int')) - 1;
168+
169+
if ($index < 0 || $index > $lastIndex) {
170+
throw new \OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, $lastIndex));
171+
}
172+
173+
return $this->arguments[$index];
174+
}
175+
149176
/**
150177
* You should always use this method when overwriting existing arguments
151178
* of the parent definition.

tests/Symfony/Tests/Component/DependencyInjection/DefinitionDecoratorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,32 @@ public function testReplaceArgumentShouldRequireIntegerIndex()
7979

8080
$def->replaceArgument('0', 'foo');
8181
}
82+
83+
public function testReplaceArgument()
84+
{
85+
$def = new DefinitionDecorator('foo');
86+
87+
$def->setArguments(array(0 => 'foo', 1 => 'bar'));
88+
$this->assertEquals('foo', $def->getArgument(0));
89+
$this->assertEquals('bar', $def->getArgument(1));
90+
91+
$this->assertSame($def, $def->replaceArgument(1, 'baz'));
92+
$this->assertEquals('foo', $def->getArgument(0));
93+
$this->assertEquals('baz', $def->getArgument(1));
94+
95+
$this->assertEquals(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz'), $def->getArguments());
96+
}
97+
98+
/**
99+
* @expectedException OutOfBoundsException
100+
*/
101+
public function testGetArgumentShouldCheckBounds()
102+
{
103+
$def = new DefinitionDecorator('foo');
104+
105+
$def->setArguments(array(0 => 'foo'));
106+
$def->replaceArgument(0, 'foo');
107+
108+
$def->getArgument(1);
109+
}
82110
}

0 commit comments

Comments
 (0)
0