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
Deprecate the $check_key_exists parameter from AssetDataRegistry and disallow duplicate data for all cases #46139
Deprecate the $check_key_exists parameter from AssetDataRegistry and disallow duplicate data for all cases #46139
Conversation
…CAdminSharedSettings
…ing registered is not a string and whenever attempting to override existing data.
Hi @albarin, @samueljseay, @nerrad, @gigitux, @senadir, @wavvves, @kmanijak, @woocommerce/rubik, @woocommerce/woo-fse Apart from reviewing the code changes, please make sure to review the testing instructions as well. You can follow this guide to find out what good testing instructions should look like: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm approving initially on the premise of this PR, but there might be some unintended side-effect so let's wait and see if all tests pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be a related test failure
Time: 07:40.002, Memory: 305.00 MB
There was 1 error:
1) Automattic\WooCommerce\Tests\Blocks\Assets\AssetDataRegistry::test_invalid_key_on_adding_data
array_key_exists(): The first argument should be either a string or an integer
/var/www/html/wp-content/plugins/woocommerce/src/Blocks/Assets/AssetDataRegistry.php:288
/var/www/html/wp-content/plugins/woocommerce/src/Blocks/Assets/AssetDataRegistry.php:310
/var/www/html/wp-content/plugins/woocommerce/tests/php/src/Blocks/Assets/AssetDataRegistry.php:53
phpvfscomposer:///var/www/html/wp-content/plugins/woocommerce/vendor/phpunit/phpunit/phpunit:[106](https://github.com/woocommerce/woocommerce/actions/runs/8540886005/job/23399405244?pr=46139#step:5:107)
--
There was 1 failure:
1) Automattic\WooCommerce\Tests\Blocks\Assets\AssetDataRegistry::test_already_existing_key_on_adding_data
Failed asserting that exception of type "InvalidArgumentException" is thrown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from the need to update the unit tests this is looking good to me. Thanks for including the warnings too 🙇🏻
…est, considering we now return early whenever the key is duplicated, the code never reaches this stage via AssetDataRegistry:add
…mbler-is-not-loaded
I did some additional changes here to account for the fact that we are not throwing an exception anymore 627b911 Also, I've moved the call to $this->exists to the @samueljseay would you help me out with a second review here? We want to open a CFE for this work, it would be great to have another final review here to confirm we are on track. |
throw $e; | ||
} | ||
wc_caught_exception( $e, __METHOD__, [ $key, $data ] ); | ||
if ( $check_key_exists ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this since we're deprecating the parameter, what do you think if we check for isset
instead. This way, the caller will get a deprecated feedback whether they passed 'true' or 'false' there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this since we're deprecating the parameter, what do you think if we check for isset instead. This way, the caller will get a deprecated feedback whether they passed 'true' or 'false' there.
Thanks for the review @roykho. This is a good point, and I agree it makes sense to check for isset
, but given the urgency of this work, I'd like to propose working on this as part of a separate PR so we can unblock the release. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, if you don't mind please create a separate issue for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, absolutely! #46349
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a comment which will be addressed at a later date. I've tested this PR with WP_DEBUG enabled and it fixes the fatal error. LGTM!
…disallow duplicate data for all cases (#46139) * Ensures data is added to the registry just once. * ditch unnecessary param. * Deprecate the param for AssetDataRegistry * Remove the param for additional calls to AssetDataRegistry::add() * Remove the param for additional call to AssetDataRegistry::add() on WCAdminSharedSettings * Trigger a PHP warning instead of throwing a fatal whenever the key being registered is not a string and whenever attempting to override existing data. * Add changefile(s) from automation for the following project(s): woocommerce * Replace trigger_error with error_log to clear out lint errors. * Address lint for trigger_error * Update the test_already_existing_key_on_adding_data test to expect a warning instead of an exception * Update tests * Update method from add_data to add. * Remove the throw InvalidArgumentException for the add and add_data methods. * Remove the now unnecessary test_already_existing_key_on_adding_data test, considering we now return early whenever the key is duplicated, the code never reaches this stage via AssetDataRegistry:add * Move the call to the exists method to within the add_data method. --------- Co-authored-by: github-actions <github-actions@github.com>
Submission Review Guidelines:
Changes proposed in this Pull Request:
Currently, AssetDataRegistry::add_data doesn't allow overriding existing data for a given key.
Still, the only method that calls it, AssetDataRegistry::add, has an optional param
$check_key_exists
with the default value set to false, which in practice doesn't have any impact in the end results, as whenever set to false, no data is overridden: in this case we have an early return combined with a fatal error triggered exclusively wheneverWP_DEBUG
is set to TRUE.This logic has caused us a few problems, and after internal discussion, which happened on pdnLyh-5U0-p2, I'm proposing for us to deprecate the
$check_key_exists
parameter, as if we allow data override, quoting @senadir :Porting over @samueljseay 's comment here as well:
Following Sam's suggestion, I've also updated the pre-existing logic that would throw a fatal exclusively whenever DEBUG is set to true to throw a PHP warning instead.
This is a change that should not have any impact on functionality, still, I'm tagging folks from the various teams to review this PR and test so we can confirm things are working as expected.
Closes #43404
How to test the changes in this Pull Request:
Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:
WP_DEBUG
set to true.wp-admin/admin.php?page=wc-admin&path=%2Fcustomize-store%2Fassembler-hub
)Changelog entry
Significance
Type
Message
Deprecate the $check_key_exists parameter from AssetDataRegistry and disallow duplicate data for all cases.
Comment