Skip to content

Replace erroneous usage of unsData method #18814

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

Merged
merged 1 commit into from
Nov 12, 2018
Merged

Replace erroneous usage of unsData method #18814

merged 1 commit into from
Nov 12, 2018

Conversation

t-richards
Copy link
Contributor

Description (*)

This pull request replaces incorrect usages of the magic unsData method. Note that there is nothing inherently wrong with magic methods like unsData(), but calling the uns or has variants with any arguments is a mistake.

For example,

// Syntactically correct, but probably doesn't do what you think it does
$object->unsData('_form_key');
//          ^^^^
// __call method takes the key from the method name, not from the method argument

The above statement is functionally equivalent to the following code:

// We're unsetting the "data" key which is not what we wanted.
// This usually results in no data being unset from the object 
$object->unsetData('data');

Since the unsData method is being called with an argument, it is almost certainly the case that the original author(s) intended to use the unsetData method instead.

// Corrected code
$object->unsetData('_form_key');

Fixed Issues (if relevant)

N/A

Manual testing scenarios (*)

  1. Expect for all test results to stay exactly the same.
  2. If any test results change, then the tests should be re-examined for correctness.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@magento-cicd2
Copy link
Contributor

magento-cicd2 commented Oct 25, 2018

CLA assistant check
All committers have signed the CLA.

@magento-engcom-team
Copy link
Contributor

Hi @t-richards. 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 PR changes
  • @magento-engcom-team give me $VERSION instance - deploy vanilla Magento instance

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

@orlangur
Copy link
Contributor

@t-richards nice catch! Need to check if there are similar occurrences in non-public code.

@slavvka
Copy link
Member

slavvka commented Oct 26, 2018

@orlangur Probably it makes sense to add some logic which checks if uns***() method is called with arguments? And preferably to fallback to unset('***') method?

@slavvka
Copy link
Member

slavvka commented Oct 26, 2018

@orlangur and I see no reason on holding this PR on since it is a fix anyway :)

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.

No such occurrences in B2B/EE code.

@orlangur
Copy link
Contributor

orlangur commented Nov 6, 2018

@slavvka I would rather throw an InvalidArgumentException but this error is quite rare thus I don't think we really need to change DataObject.

@magento-engcom-team
Copy link
Contributor

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

@magento-engcom-team
Copy link
Contributor

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

@t-richards t-richards deleted the chore/unsdata branch November 12, 2018 18:02
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.

5 participants