Skip to content
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

Conversation

lfluvisotto
Copy link
Contributor

Description

Variable as a method parameter might be overridden by the loop.
Rename the variable in the loop let the code more readable.

@magento-engcom-team
Copy link
Contributor

Hi @lfluvisotto. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on Pull Request changes
  • @magento-engcom-team give me new test instance - deploy NEW test instance based on Pull Request changes
  • @magento-engcom-team give me {$VERSION} instance - deploy Vanilla Magento instance for Issue or Pull Request

For more details, please, review the Magento Contributor Assistant documentation

@orlangur
Copy link
Contributor

This makes code LESS readable. Never like such underscore-prefixed variables in phtml templates, it goes against the spirit of PSR-2.

public function updateQtyOption($options, \Magento\Framework\DataObject $option, $value, $product)
    {
        $optionProduct = $option->getProduct($product);
        $optionUpdateFlag = $option->getHasQtyOptionUpdate();
        $optionCollection = $this->getOptionsCollection($product);
        $selections = $this->getSelectionsCollection($optionCollection->getAllIds(), $product);
        foreach ($selections as $selection) {
            if ($selection->getProductId() == $optionProduct->getId()) {
                foreach ($options as &$_option) {

There is no risk in overriding $option here as it is not passed by reference into function. Didn't check other occurrences, please provide all of them containing a real problem.

@lfluvisotto
Copy link
Contributor Author

lfluvisotto commented Jun 14, 2018

@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
parentheses, spaces, and braces, but not about the variable names.

I underscored the variable name in the loop which means that variable scope it is just for loop (local).

@lfluvisotto
Copy link
Contributor Author

Also @sidolov what is your opinion? what do you think? it's relevant or not?

@lfluvisotto lfluvisotto requested a review from sidolov June 14, 2018 21:58
@lfluvisotto
Copy link
Contributor Author

Also @okorshenko what is your opinion? what do you think? it's relevant or not?

@orlangur
Copy link
Contributor

@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.

@lfluvisotto
Copy link
Contributor Author

@orlangur

Do you agree if I rename those variables to other names without underscore?

@orlangur
Copy link
Contributor

@lfluvisotto,

please provide all of them containing a real problem

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.

@lfluvisotto
Copy link
Contributor Author

I will work on the changes tonight.

@lfluvisotto
Copy link
Contributor Author

Got a little free time.

@orlangur "Only in places where there is a real problem - like passing method argument by reference"

Ok, done.

Copy link
Contributor

@orlangur orlangur left a 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.

@lfluvisotto
Copy link
Contributor Author

@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[]

Copy link
Contributor

@orlangur orlangur left a 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);
Copy link
Contributor Author

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) {
Copy link
Contributor

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.

@orlangur
Copy link
Contributor

orlangur commented Jul 2, 2018

@lfluvisotto please squash it and then I'll give an approval in no time.

@lfluvisotto lfluvisotto force-pushed the 2.2-develop-variable-renamed branch from f1abf05 to bbb0d24 Compare July 2, 2018 18:22
@lfluvisotto
Copy link
Contributor Author

@orlangur everything just in one commit is that you need?

@orlangur
Copy link
Contributor

orlangur commented Jul 3, 2018

@lfluvisotto exactly! 👍

@magento-engcom-team
Copy link
Contributor

Hi @orlangur, thank you for the review.
ENGCOM-2176 has been created to process this Pull Request

@magento-engcom-team
Copy link
Contributor

Hi @lfluvisotto. Thank you for your contribution.
We will aim to release these changes as part of 2.2.6.
Please check the release notes for final confirmation.

Please, consider to port this solution to 2.3 release line.
You may use Porting tool to port commits automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants