Skip to content

[DependencyInjection] Properly ignore invalid reference arguments in collection arguments #17132

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

Merged
merged 1 commit into from
Jan 25, 2016
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,19 @@ public function process(ContainerBuilder $container)
*
* @param array $arguments An array of Reference objects
* @param bool $inMethodCall
* @param bool $inCollection
*
* @return array
*
* @throws RuntimeException When the config is invalid
*/
private function processArguments(array $arguments, $inMethodCall = false)
private function processArguments(array $arguments, $inMethodCall = false, $inCollection = false)
{
$isNumeric = array_keys($arguments) === range(0, count($arguments) - 1);

foreach ($arguments as $k => $argument) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggests doing $isNumeric = array_keys($arguments) === range(0, count($arguments) - 1); before the foreach

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$isSequential ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as you want

if (is_array($argument)) {
$arguments[$k] = $this->processArguments($argument, $inMethodCall);
$arguments[$k] = $this->processArguments($argument, $inMethodCall, true);
} elseif ($argument instanceof Reference) {
$id = (string) $argument;

Expand All @@ -91,6 +94,10 @@ private function processArguments(array $arguments, $inMethodCall = false)
if (!$exists && ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
$arguments[$k] = null;
} elseif (!$exists && ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
if ($inCollection) {
unset($arguments[$k]);
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure this should happen before throwing the exception just below (I didn't check)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, otherwise ResolveInvalidReferencesPassTest::testProcessKeepMethodCallOnInvalidArgumentInCollectionArgument will fail.

}
if ($inMethodCall) {
throw new RuntimeException('Method shouldn\'t be called.');
}
Expand All @@ -100,6 +107,11 @@ private function processArguments(array $arguments, $inMethodCall = false)
}
}

// Ensure numerically indexed arguments have sequential numeric keys.
if ($isNumeric) {
$arguments = array_values($arguments);
}

return $arguments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,63 @@ public function testProcess()
$container = new ContainerBuilder();
$def = $container
->register('foo')
->setArguments(array(new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE)))
->setArguments(array(
new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE),
new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
))
->addMethodCall('foo', array(new Reference('moo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
;

$this->process($container);

$arguments = $def->getArguments();
$this->assertNull($arguments[0]);
$this->assertSame(array(null, null), $arguments);
$this->assertCount(0, $def->getMethodCalls());
}

public function testProcessIgnoreInvalidArgumentInCollectionArgument()
{
$container = new ContainerBuilder();
$container->register('baz');
$def = $container
->register('foo')
->setArguments(array(
array(
new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
$baz = new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
new Reference('moo', ContainerInterface::NULL_ON_INVALID_REFERENCE),
),
))
;

$this->process($container);

$arguments = $def->getArguments();
$this->assertSame(array($baz, null), $arguments[0]);
}

public function testProcessKeepMethodCallOnInvalidArgumentInCollectionArgument()
{
$container = new ContainerBuilder();
$container->register('baz');
$def = $container
->register('foo')
->addMethodCall('foo', array(
array(
new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
$baz = new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
new Reference('moo', ContainerInterface::NULL_ON_INVALID_REFERENCE),
),
))
;

$this->process($container);

$calls = $def->getMethodCalls();
$this->assertCount(1, $def->getMethodCalls());
$this->assertSame(array($baz, null), $calls[0][1][0]);
}

public function testProcessIgnoreNonExistentServices()
{
$container = new ContainerBuilder();
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy