Skip to content

Commit

Permalink
bug #22981 [DependencyInjection] Fix named args support in ChildDefin…
Browse files Browse the repository at this point in the history
…ition (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
-------

1ab3e41 [DependencyInjection] Fix named args support in ChildDefinition
  • Loading branch information
fabpot committed Jun 5, 2017
2 parents c973e8a + 1ab3e41 commit 1272d2a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/Symfony/Component/DependencyInjection/ChildDefinition.php
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
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

0 comments on commit 1272d2a

Please sign in to comment.