Skip to content

Support the use of PHP 8.0 "mixed" type #35572

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 7 commits into from
Aug 31, 2022

Conversation

pmzandbergen
Copy link
Contributor

@pmzandbergen pmzandbergen commented Jun 2, 2022

Currently Magento does not compile when using the mixed type, neither as a parameter or return type. The generated Interceptor / Proxy / etc. classes try to make this union type nullable (?mixed). This will result in a fatal PHP error since the union type already includes null, and null is not nullable.

During compilation you will be encountered with the following error: Type "mixed" cannot be nullable

This patch checks if the type is mixed before adding the nullable (?) prefix.

Resolved issues:

  1. resolves [Issue] Support the use of PHP 8.0 "mixed" type #35579: Support the use of PHP 8.0 "mixed" type

The PHP "mixed" type is a union type including null, see: https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.mixed

Therefore the type is never nullable, e.g. `?mixed $someParameter` is invalid and will generate a Fatal PHP Error: "Mixed types cannot be nullable, null is already part of the mixed type."

This commit will fix mixed parameters.
@m2-assistant
Copy link

m2-assistant bot commented Jun 2, 2022

Hi @pmzandbergen. Thank you for your contribution
Here are 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 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 Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@sdzhepa sdzhepa added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Jun 2, 2022
@xmav
Copy link
Contributor

xmav commented Jun 2, 2022

Hi @pmzandbergen !
Could you please share your use case/code snipped to reproduce this issue ?

Thanks,
Maksym

@pmzandbergen
Copy link
Contributor Author

Hi @pmzandbergen ! Could you please share your use case/code snipped to reproduce this issue ?

Thanks, Maksym

Created a very simple example module @ https://github.com/pmzandbergen/some-module

Simply add it to a vanilla Magento 2.4.4 installation and run bin/magento setup:di:compile. Output:

% bin/magento setup:di:compile                        
Compilation was started.
Interceptors generation... 4/9 [============>---------------]  44% 4 secs 218.0 MiB
In Generator.php line 140:
                                                                                                                                                                                                                
  Type "mixed" cannot be nullable                                                                                                                                                                               
  Class Some\Module\Model\TestClass\Interceptor generation error: The requested class did not generate properly, because the 'generated' directory permission is read-only. If --- after running the 'bin/mage  
  nto setup:di:compile' CLI command when the 'generated' directory permission is set to write --- the requested class did not generate properly, then you must add the generated class object to the signature  
   of the related construct method, only.                                                                                                                                                                       
                                                                                                                                                                                                                

setup:di:compile

This is caused by Magento using ?mixed as a parameter type for the Interceptor for Some\Module\Model\TestClass

@ihor-sviziev
Copy link
Contributor

Hi @pmzandbergen,
Could you please cover your changes with integration tests?

@ihor-sviziev
Copy link
Contributor

@magento run all tests
@magento create issue

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@ihor-sviziev ihor-sviziev added Award: test coverage Auto-Tests: Covered All changes in Pull Request is covered by auto-tests and removed Auto-Tests: Not Covered Changes in Pull Request requires coverage by auto-tests help wanted labels Aug 5, 2022
@ihor-sviziev
Copy link
Contributor

@magento run Static Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@ihor-sviziev
Copy link
Contributor

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

3 similar comments
@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Lima
Copy link
Contributor

✔️ QA Passed

Preconditions:
Install fresh vanilla Magento 2.4-develop

Manual testing scenario:

  1. Go to https://github.com/pmzandbergen/some-module and download "some modules".
  2. Copy the module to app/code (app/code/Some/Module/...)
  3. Enable the module bin/magento module:enable Some_Module
  4. Run compiler bin/magento setup:di:compile

Before: ✖️ Getting error while compiling.
Screenshot from 2022-08-25 14-35-39

After: ✔️ No error while compilation.
Screenshot from 2022-08-25 14-45-31

Additionally tested:

  1. Login as admin.
  2. Create order from admin.
  3. Login as customer.
  4. Place an order from customer.
  5. Created invoice,shipped and credit memo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Award: bug fix Award: test coverage Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: accept Severity: S1 Affects critical data or functionality and forces users to employ a workaround.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Issue] Support the use of PHP 8.0 "mixed" type
7 participants