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

#38365: Fixed wrong type for InvalidArgumentException to avoid fatal error #38366

Open
wants to merge 1 commit into
base: 2.4-develop
Choose a base branch
from

Conversation

andrewbess
Copy link

Description (*)

This pull request (PR) provides fixes for store resolver to avoid fatal error when system tries to throw InvalidArgumentException with message: "Invalid store parameter."

Fixed Issues (if relevant)

  1. Fixes Fatal error thrown on exception constructor when cookies store is an array #38365

Manual testing scenarios (*)

  1. You need to have the $_COOKIE['store'] to be an array and not contain a valid store code. For example: $_COOKIE['store'] = ['test'];

Questions or comments

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)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Copy link

m2-assistant bot commented Jan 19, 2024

Hi @andrewbess. Thank you for your contribution!
Here are some useful tips on 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 give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@m2-community-project m2-community-project bot added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Jan 19, 2024
@m2-github-services m2-github-services added Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner labels Jan 19, 2024
@andrewbess
Copy link
Author

@magento run all tests

Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@hostep
Copy link
Contributor

hostep commented Jan 19, 2024

May I suggest a different solution.

I think somebody by mistake used \InvalidArgumentException instead of Magento\Framework\Exception\InvalidArgumentException. That last one does take a Phrase object as it inherits from LocalizedException.
So I would use that class instead and remove the ->render() call again.

If I search the entire codebase for \InvalidArgumentException(__ I find 6 other occurrences. Maybe we can fix them all at the same time? (maybe somebody can come up with a better regex to search for this pattern, as more may be hidden by different formatting and maybe other types of exceptions...)

@andrewbess
Copy link
Author

May I suggest a different solution.

I think somebody by mistake used \InvalidArgumentException instead of Magento\Framework\Exception\InvalidArgumentException. That last one does take a Phrase object as it inherits from LocalizedException. So I would use that class instead and remove the ->render() call again.

If I search the entire codebase for \InvalidArgumentException(__ I find 6 other occurrences. Maybe we can fix them all at the same time? (maybe somebody can come up with a better regex to search for this pattern, as more may be hidden by different formatting and maybe other types of exceptions...)

Thank you @hostep for your suggestion
I thought about it and I agree with you
I'll check usage \InvalidArgumentException in the code base and fix it.
Thank you again.

@andrewbess
Copy link
Author

We have failed static test here.
A couple protected properties have been deprecated more then 5 years ago. This class is not @api. I checked usage of it in the Magento Open Source and Adobe Commerce and didn't find usage of it.
I think we already can remove it from codebase.
What do you think? @hostep @ihor-sviziev @rogyar

@ihor-sviziev
Copy link
Contributor

@andrewbess if it's not used, why should we touch it?

@hostep
Copy link
Contributor

hostep commented Jan 20, 2024

Just one extra thought around the the Phrase not automatically being cast to a string despite it having a __toString() method.
It looks like this is determined by the statement declare(strict_types=1);
If you don't use that statement, it just works: https://3v4l.org/XlRpX
If you do use it, it fails: https://3v4l.org/S9kYE

I'm a fan of using declare(strict_types=1); but only for new classes, I'm not a fan of introducing that statement in existing classes "just because" (which seems to happen all the time in this code base), because it might introduce hard to detect bugs, just like it did over here in this case.

PHPstan is able to detect this bug, but you need to use level 5 and I believe the static tests used by Magento only run on level 0 or 1:

So another way to find violations in the current codebase would be to run something like this:

$ vendor/bin/phpstan analyse --level=5 ./path/to/code/to/scan | grep 'Phrase given'

Executing this on the lib/internal/Magento path finds 3 problems, executing it on app/code it finds a whopping 229 problems (not sure if all these are valid, didn't check in detail).


Around my last comment, I'm maybe a bit worried about breaking backwards compatibility if we change types of exceptions, users may be surprised that the type changed. So maybe we do need to keep them as-is and just explicitly cast the Phrases to a string, by using (string) or ->__toString(); or ->render();. I'm mostly in favour of using (string) to fix it.
Anyway, just my 2 cents, other opinions are welcome :)

@andrewbess
Copy link
Author

Thank you @hostep

I also thought about it.
Changing type of exception may affect a lot of 3rd-party modules.
So, I agree with @hostep.
But let's discuss which operator to make Phrase to string we will use.
I don't like __toString() in this case.
I propose (string) or ->render()

What do you think?

@ihor-sviziev @rogyar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: pending review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fatal error thrown on exception constructor when cookies store is an array
4 participants