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
Add checks and unit tests to Transformers #44634
Conversation
if ( ! is_string( $value ) ) { | ||
return $default; | ||
} | ||
|
||
$url_parts = wp_parse_url( rtrim( $value, '/' ) ); |
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.
https://developer.wordpress.org/reference/functions/wp_parse_url/
Return
mixed False on parse failure; Array of URL components on success; When a specific component has been requested: null if the component doesn’t exist in the given URL; a string or – in the case of PHP_URL_PORT – integer when it does. See parse_url()’s return values.
Hi @ilyasfoo, @moon0326, @woocommerce/ghidorah 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: |
null !== $arguments->key && | ||
! is_string( $arguments->key ) && | ||
! is_int( $arguments->key ) | ||
) { |
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.
https://www.php.net/manual/en/function.array-column.php
array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array
column_key
The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be null to return complete arrays or objects (this is useful together with index_key to reindex the array).
Test Results SummaryCommit SHA: c819b75
To view the full API test report, click here. To view the full E2E test report, click here. To view all test reports, visit the WooCommerce Test Reports Dashboard. |
@@ -21,6 +21,10 @@ class ArrayKeys implements TransformerInterface { | |||
* @return mixed | |||
*/ | |||
public function transform( $value, stdClass $arguments = null, $default = null ) { | |||
if ( ! is_array( $value ) ) { | |||
return $default; |
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.
What do you think of setting the default to an empty array?
@@ -21,6 +21,10 @@ class ArrayValues implements TransformerInterface { | |||
* @return mixed | |||
*/ | |||
public function transform( $value, stdClass $arguments = null, $default = null ) { | |||
if ( ! is_array( $value ) ) { | |||
return $default; |
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.
Same as above. What do you think of setting the default value to an empty array? I think it makes more sense as the caller expects the return value to be an array. It also aligns with the built-in array_values
function.
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.
Good point! @moon0326 That makes sense to me. I'll update the default value to an empty array. 🙂
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.
Changed in 1eb5627. I also updated default value for ArrayFlatten
.
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've left a few minor comments, but LGTM! 👍
f01824a
to
c72005f
Compare
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.
Changes LGTM! 👍 🚀
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.
Changes look great! I have a minor comment but pre-approving
@@ -20,7 +20,11 @@ class ArrayFlatten implements TransformerInterface { | |||
* | |||
* @return mixed|null | |||
*/ | |||
public function transform( $value, stdClass $arguments = null, $default = null ) { | |||
public function transform( $value, stdClass $arguments = null, $default = array() ) { |
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.
It seems ArrayFlatten
, ArrayKeys
, and ArrayValues
default is set to array()
, but in ArrayColumn
and ArraySearch
it's null
. Do you have any reason on why we treat these transformers differently? Otherwise, I think it's better to be consistent.
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.
@ilyasfoo Thanks for pointing that out. I missed that for ArrayColumn
and just updated it in c819b75 . I think we should keep the default value of ArraySearch
null because we are returning null when value is not found. Does that sound good to you?
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.
Sounds good, thanks!
Submission Review Guidelines:
Changes proposed in this Pull Request:
Partially close #44249
Related to #44448
This PR audits every PHP class that implements TransformerInterface to prevent fatal errors and ensure PHP 8+ compliance.
Changes
How to test the changes in this Pull Request:
Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:
Please review the changes and unit tests carefully to ensure that the changes are correct and don't introduce any regressions.
wp transient delete --all
Display suggestions within WooCommerce
is checked/wp-admin/admin.php?page=wc-admin&path=/setup-wizard
I'm just starting my business
and continueAustralia — Australian Capital Territory
and continueChangelog entry
Significance
Type
Message
Comment