Skip to content

Check if WeakMap keys exist before using#810

Draft
agibson-godaddy wants to merge 9 commits intorelease/6.1.2from
mwc-19335
Draft

Check if WeakMap keys exist before using#810
agibson-godaddy wants to merge 9 commits intorelease/6.1.2from
mwc-19335

Conversation

@agibson-godaddy
Copy link
Contributor

@agibson-godaddy agibson-godaddy commented Mar 23, 2026

Summary

This adds some explicit key checks before using, to avoid any exceptions that might get thrown if they don't exist.

Story: MWC-19335

Release: #812 (release PR)

Details

I also added some unit tests to cover the scenario. Each test runs in both WeakMap mode & the fallback. The original error can be reproduced by reverting the fix and running the tests:

$ vendor/bin/phpunit tests/unit/Payment_Gateway/Dynamic_Props_Test.php
PHPUnit 9.6.20 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.14
Configuration: /home/agibson/Projects/wc-plugin-framework/phpunit.xml

........E.                                                        10 / 10 (100%)

Time: 00:00.077, Memory: 16.00 MB

There was 1 error:

1) SkyVerge\WooCommerce\PluginFramework\v6_1_2\Tests\unit\Payment_Gateway\Dynamic_Props_Test::testCanUnset with data set "WeakMap" (true)
Error: Object Mockery_1__WC_Order#202 not contained in WeakMap

/home/agibson/Projects/wc-plugin-framework/woocommerce/payment-gateway/Dynamic_Props.php:134
/home/agibson/Projects/wc-plugin-framework/tests/unit/Payment_Gateway/Dynamic_Props_Test.php:118
/home/agibson/Projects/wc-plugin-framework/vendor/10up/wp_mock/php/WP_Mock/Tools/TestCase.php:123

ERRORS!
Tests: 10, Assertions: 18, Errors: 1.

QA

  • Code review
  • Checks pass

Before merge

  • I have confirmed these changes in each supported minor WooCommerce version

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds defensive checks around WeakMap access in the payment gateway framework’s Dynamic_Props helper to prevent exceptions when an order key is missing from the map.

Changes:

  • Return the provided default early when a \WC_Order key is not present in the WeakMap during Dynamic_Props::get().
  • Guard Dynamic_Props::unset() so it only attempts to unset properties when the order exists in the WeakMap.
  • Document the fix in the framework changelog.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
woocommerce/payment-gateway/Dynamic_Props.php Adds isset() guards before reading/unsetting WeakMap entries keyed by \WC_Order.
woocommerce/changelog.txt Adds a changelog entry noting the WeakMap exception fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Base automatically changed from release/6.1.1 to master March 23, 2026 13:44
@agibson-godaddy agibson-godaddy changed the base branch from master to release/6.1.2 March 23, 2026 13:50
@agibson-godaddy agibson-godaddy self-assigned this Mar 23, 2026
@agibson-godaddy agibson-godaddy mentioned this pull request Mar 23, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants