-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[OptionsResolver] remove dead code and useless else #28458
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
[OptionsResolver] remove dead code and useless else #28458
Conversation
ronfroy
commented
Sep 13, 2018
Q | A |
---|---|
Branch? | master |
Bug fix? | no |
New feature? | no |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
License | MIT |
@@ -526,11 +526,11 @@ public function addAllowedValues($option, $allowedValues) | |||
} | |||
|
|||
if (!isset($this->allowedValues[$option])) { | |||
$this->allowedValues[$option] = $allowedValues; |
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.
If we make this change in master
, which uses modern PHP versions, we could even simplify it more:
- if (!isset($this->allowedValues[$option])) {
- $this->allowedValues[$option] = $allowedValues;
- } else {
- $this->allowedValues[$option] = array_merge($this->allowedValues[$option], $allowedValues);
- }
+ $this->allowedValues[$option] = array_merge($this->allowedValues[$option] ?? array(), $allowedValues);
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.
Those if/else constructs aren't useless: they prevent a unnecessary call to array_merge
, which is a costly function, even though the damage should be limited when it is called with an empty array as argument.
As such, these changes don't do anything valuable, they just reduce the number of lines of code at the expense of performance, so they should be left untouched IMHO.
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.
@skalpa it is a really minor cost.
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.
a minor cost repeated hundreds or thousands times when building complex forms with many fields becomes major
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 agree with stof on this one. Perhaps it would be a good idea to add a comment in the code as of why this was done, instead of always calling array_merge?
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.
removed
} | ||
|
||
$this->allowedValues[$option] = array_merge($this->allowedValues[$option], $allowedValues); |
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.
Also, we can remove the array check above and use (array) $allowedValues
instead, just like addAllowedTypes()
method.
@yceruto I understand that the performance cost I discussed is minor. However your changes don't add any functionality and some would argue that you are just changing code for the sake of changing code. What do you think was wrong with the if/else statements ? |
Thank you @ronfroy. |
…froy) This PR was submitted for the master branch but it was squashed and merged into the 3.4 branch instead (closes #28458). Discussion ---------- [OptionsResolver] remove dead code and useless else | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT <!-- remove dead method and useless else --> Commits ------- 0c1484b [OptionsResolver] remove dead code and useless else