Skip to content

[DependencyInjection] Fix named args support in ChildDefinition #22981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/Symfony/Component/DependencyInjection/ChildDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function setParent($parent)
* 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
*
Expand All @@ -74,13 +74,7 @@ public function getArgument($index)
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);
}

/**
Expand All @@ -91,8 +85,8 @@ public function getArgument($index)
* 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
*
Expand All @@ -105,7 +99,7 @@ public function replaceArgument($index, $value)
} 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public function testReplaceArgument()
$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());
}

/**
Expand Down