-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Variable as a method parameter might be overridden by the loop #16143
Variable as a method parameter might be overridden by the loop #16143
Conversation
Hi @lfluvisotto. Thank you for your contribution
For more details, please, review the Magento Contributor Assistant documentation |
This makes code LESS readable. Never like such underscore-prefixed variables in phtml templates, it goes against the spirit of PSR-2.
There is no risk in overriding |
@orlangur less readable? Sorry, I don't think so. I understand that each developer has its way of developing, but I think this question has to be in the consensus of other developers who shares the same opinion. It is a improvement to the code, there is no problem. I think Magento also cares about improving the current code. About https://www.php-fig.org/psr/psr-2/ in loop foreach is about the placement of I underscored the variable name in the loop which means that variable scope it is just for loop (local). |
Also @sidolov what is your opinion? what do you think? it's relevant or not? |
Also @okorshenko what is your opinion? what do you think? it's relevant or not? |
@lfluvisotto please answer on "Didn't check other occurrences, please provide all of them containing a real problem.". Underscores were discouraged in PSR-2 to indicate visibility in multiple contexts. As I said, "goes against the spirit of PSR-2", of course there is no specific rules for variable naming. |
Do you agree if I rename those variables to other names without underscore? |
Only in places where there is a real problem - like passing method argument by reference. There is no need to change 21 files if only one is really affected. In such case tool you used just gave a false-positive result. |
I will work on the changes tonight. |
8b24cba
to
695aaf8
Compare
Got a little free time. @orlangur "Only in places where there is a real problem - like passing method argument by reference" Ok, done. |
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.
$option->setValue(intval($option->getValue()));
Oh, that was weird.
Could you tell me from logic, optionSelection
is not really clear to me, is it selectedOption
or selectionOption
(i.e. "option of selection")? Please rename variable accordingly.
@orlangur I've renamed as $quoteItemOption, I think is clear now. The call comes from app/code/Magento/Quote/Model/Quote/Item.php:662 $this->getOptions() returns \Magento\Quote\Model\Quote\Item\Option[] |
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.
@lfluvisotto really nice! 👍
First time I met in Magento when static analysis caught a real bug :)
} | ||
} | ||
} | ||
|
||
unset($quoteItemOption); |
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.
https://secure.phabricator.com/book/phabflavor/article/php_pitfalls/
Following the tip below:
The easiest way to avoid this is to avoid using foreach-by-reference. If you do use it, unset the reference after the loop:
foreach ($array as &$value) {
// ...
}
unset($value);
@@ -536,15 +536,17 @@ public function updateQtyOption($options, \Magento\Framework\DataObject $option, | |||
|
|||
foreach ($selections as $selection) { | |||
if ($selection->getProductId() == $optionProduct->getId()) { | |||
foreach ($options as &$option) { | |||
if ($option->getCode() == 'selection_qty_' . $selection->getSelectionId()) { | |||
foreach ($options as &$quoteItemOption) { |
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.
Why do we need foreach by reference here at all? It is an object, please just remove ampersand and unset
.
@lfluvisotto please squash it and then I'll give an approval in no time. |
f1abf05
to
bbb0d24
Compare
@orlangur everything just in one commit is that you need? |
@lfluvisotto exactly! 👍 |
Hi @orlangur, thank you for the review. |
Hi @lfluvisotto. Thank you for your contribution. Please, consider to port this solution to 2.3 release line. |
Description
Variable as a method parameter might be overridden by the loop.
Rename the variable in the loop let the code more readable.