-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Fix duplication of placeholders #20199
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 1 commit
1e9d875
a0373ba
92da498
574da52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…vPlaceholderParameterBags
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,9 +62,17 @@ public function getEnvPlaceholders() | |
|
||
/** | ||
* Merges the env placeholders of another EnvPlaceholderParameterBag. | ||
* | ||
* @param EnvPlaceholderParameterBag $bag | ||
*/ | ||
public function mergeEnvPlaceholders(self $bag) | ||
{ | ||
$this->envPlaceholders = array_merge_recursive($this->envPlaceholders, $bag->getEnvPlaceholders()); | ||
$newPlaceholders = $bag->getEnvPlaceholders(); | ||
|
||
foreach ($newPlaceholders as $key => $newEntries) { | ||
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. What about this? should do the same isn't it? 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. or what about another approach like this? @@ -30,7 +30,9 @@ class EnvPlaceholderParameterBag extends ParameterBag
$env = substr($name, 4, -1);
if (isset($this->envPlaceholders[$env])) {
- return $this->envPlaceholders[$env][0];
+ foreach ($this->envPlaceholders[$env] as $placeholder) {
+ return $placeholder;
+ }
}
if (preg_match('/\W/', $env)) {
throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
@@ -43,8 +45,9 @@ class EnvPlaceholderParameterBag extends ParameterBag
throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar, but "%s" given to "%s".', gettype($defaultValue), $name));
}
}
+ $placeholder = sprintf('env_%s_%s', $env, md5($name.uniqid(mt_rand(), true)));
- return $this->envPlaceholders[$env][] = sprintf('env_%s_%s', $env, md5($name.uniqid(mt_rand(), true)));
+ return $this->envPlaceholders[$env][$placeholder] = $placeholder;
}
return parent::get($name); 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. About the For the changes inside
Finally for the changes in setting
Plus (and I know it's going outside the scope of this PR) I don't think that assignment in a return statement is very clear and would prefer something like:
I made some naming changes locally because I also found Sorry to sound like a dick and that I don't want to change it, but I think I should at least be sure about the changes before I make them. I think it's a really cool feature and the only reason I'm here is because when I was working on changing my own small project to use the 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.
not needless at all: the array is recursive (width two depth levels)
this would create a full copy of the entire array just to get the first element. foreach is the most efficient way to do so when not knowing the first key.
the point is using keys as a precomputed deduplication index, so that e.g. array_unique (slow compared to indexes) is not needed anymore.
no pb for that change, go for it 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. I can see your point for most of those, but really using a 3 line
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.
|
||
$existingEntries = isset($this->envPlaceholders[$key]) ? $this->envPlaceholders[$key] : array(); | ||
$mergedEntries = array_merge($existingEntries, $newEntries); | ||
$this->envPlaceholders[$key] = array_unique($mergedEntries); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,19 @@ public function testGetThrowsInvalidArgumentExceptionIfEnvNameContainsNonWordCha | |
$bag = new EnvPlaceholderParameterBag(); | ||
$bag->get('env(%foo%)'); | ||
} | ||
|
||
public function testMergeWillNotDuplicateIdenticalParameters() | ||
{ | ||
$originalPlaceholders = array('database_host' => array('localhost')); | ||
$firstBag = new EnvPlaceholderParameterBag($originalPlaceholders); | ||
|
||
// initialize placeholders | ||
$firstBag->get('env(database_host)'); | ||
$secondBag = clone $firstBag; | ||
|
||
$firstBag->mergeEnvPlaceholders($secondBag); | ||
$mergedPlaceholders = $firstBag->getEnvPlaceholders(); | ||
|
||
$this->assertEquals(1, count($mergedPlaceholders['database_host'])); | ||
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.
|
||
} | ||
} |
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.
should be remove as it doesn't add any value when considering the method signature
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 added it because it looks like most of the docblocks for methods in the Symfony core either use
{@inheritdoc}
or an explicit description of parameters. You're right (and I'm sure the clean code principles would agree with you) that it doesn't add anything by looking at it, although inside an IDE like PHPStorm people will use the quick documentation feature which makes it a little bit more understandable what the type parameter type is, but of course I can remove it - just let me knowThere 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.
We generally prefer removing those, eventhough the code base is not always consistent on this aspect.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sure, I'll change it then. I'll wait a bit for your response to the other part before committing and pushing.