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

Add checks and unit tests to Transformers #44634

Merged
merged 6 commits into from Feb 16, 2024
Merged

Conversation

chihsuan
Copy link
Member

@chihsuan chihsuan commented Feb 15, 2024

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

  • Apply defensive programming principles - add type checks to prevent fatal errors
  • Add unit tests

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.

  1. In your environment, use PHP 8.1+
  2. If using an existing site, clear transients by running wp transient delete --all
  3. Ensure WooCommerce > Settings > Advanced > Woo.com > Display suggestions within WooCommerce is checked
  4. Go to Setup Wizard, /wp-admin/admin.php?page=wc-admin&path=/setup-wizard
  5. Click continue
  6. Select I'm just starting my business and continue
  7. Select Australia — Australian Capital Territory and continue
  8. Skip plugin installation
  9. Observe no fatal when loading homescreen
  10. Go to Tools > WCA Test Helper > Tools > Run wc_admin_daily job
  11. Ensure the job is run successfully

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Comment

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Feb 15, 2024
@chihsuan chihsuan self-assigned this Feb 15, 2024
@chihsuan chihsuan marked this pull request as ready for review February 15, 2024 08:00
if ( ! is_string( $value ) ) {
return $default;
}

$url_parts = wp_parse_url( rtrim( $value, '/' ) );
Copy link
Member Author

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.

@chihsuan chihsuan requested review from a team, ilyasfoo and moon0326 February 15, 2024 08:06
Copy link
Contributor

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:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

null !== $arguments->key &&
! is_string( $arguments->key ) &&
! is_int( $arguments->key )
) {
Copy link
Member Author

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).

Copy link
Contributor

github-actions bot commented Feb 15, 2024

Test Results Summary

Commit SHA: c819b75

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 36s
E2E Tests32200303258m 59s

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;
Copy link
Contributor

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;
Copy link
Contributor

@moon0326 moon0326 Feb 15, 2024

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.

Copy link
Member Author

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. 🙂

Copy link
Member Author

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.

Copy link
Contributor

@moon0326 moon0326 left a 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! 👍

Copy link
Contributor

@moon0326 moon0326 left a comment

Choose a reason for hiding this comment

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

Changes LGTM! 👍 🚀

Copy link
Contributor

@ilyasfoo ilyasfoo left a 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() ) {
Copy link
Contributor

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.

Copy link
Member Author

@chihsuan chihsuan Feb 16, 2024

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good, thanks!

@chihsuan chihsuan merged commit 75118fb into trunk Feb 16, 2024
36 checks passed
@chihsuan chihsuan deleted the dev/audit-transformaters branch February 16, 2024 08:51
@github-actions github-actions bot added this to the 8.7.0 milestone Feb 16, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Feb 16, 2024
@Stojdza Stojdza added needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. status: analysis complete Indicates if a PR has been analysed by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add checks and unit tests to classes that implements RuleProcessorInterface and TransformerInterface
4 participants