-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
if (is_array($argument)) { | ||
$arguments[$k] = $this->processArguments($argument, $inMethodCall); | ||
$arguments[$k] = $this->processArguments($argument, $inMethodCall, true); | ||
} elseif ($argument instanceof Reference) { | ||
$id = (string) $argument; | ||
|
||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, otherwise |
||
} | ||
if ($inMethodCall) { | ||
throw new RuntimeException('Method shouldn\'t be called.'); | ||
} | ||
|
@@ -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; | ||
} | ||
} |
There was a problem hiding this comment.
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 foreachThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$isSequential
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as you want