Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#53988 closed enhancement (fixed)

remove duplicate code in wp_list_filter()

Reported by: pbearne's profile pbearne Owned by: hellofromtonya's profile hellofromTonya
Milestone: 5.9 Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch commit
Focuses: Cc:

Description

The code in wp_list_filter() is the same as the code in wp_filter_object_list() with no $field so I have created a patch to wrap wp_filter_object_list removing duplicated code

Attachments (1)

53988.diff (1.3 KB) - added by SergeyBiryukov 3 years ago.

Download all attachments as: .zip

Change History (12)

This ticket was mentioned in PR #1615 on WordPress/wordpress-develop by pbearne.


3 years ago
#1

  • Keywords has-patch added

#3 follow-up: @SergeyBiryukov
3 years ago

  • Milestone changed from Awaiting Review to 5.9

Thanks for the PR!

It looks like wp_filter_object_list() used to wrap wp_list_filter() before [38928] / #37128.

So I think I would prefer reinstating that. Doing it the other way around might have more unnecessary if ( $field ) checks.

Last edited 3 years ago by SergeyBiryukov (previous) (diff)

#4 in reply to: ↑ 3 @SergeyBiryukov
3 years ago

Replying to SergeyBiryukov:

So I think I would prefer reinstating that.

Scratch that, it looks like that would require creating a second WP_List_Util instance, which might have a performance or memory impact, depending on the size of the list.

#5 @SergeyBiryukov
3 years ago

Some more history here: [14108], [15686], [17427], [38928].

#6 @SergeyBiryukov
3 years ago

The unit tests for wp_list_filter() currently fail with this PR:

There was 1 failure:

1) Tests_Functions_wpListUtil::test_wp_list_filter with data set "invalid operator" (array(stdClass Object (...), stdClass Object (...)), array('bar'), 'XOR', array())
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
+    0 => stdClass Object (...)
+    1 => stdClass Object (...)
 )

/var/www/tests/phpunit/includes/abstract-testcase.php:813
/var/www/tests/phpunit/tests/functions/wpListUtil.php:373

@SergeyBiryukov
3 years ago

#7 @SergeyBiryukov
3 years ago

The failure is because WP_List_Util::filter() performs validation of the $operator argument, but in case of an invalid operator it just returns early and does not assign the $this->output value that wp_filter_object_list() returns.

This means that wp_filter_object_list() and wp_list_filter() treat an invalid operator differently:

  • wp_filter_object_list() returns the original array.
  • wp_list_filter() returns an empty array.

53988.diff fixes that and the tests seem to pass now, though it might be up for discussion whether the current behavior is a bug or a feature.

#8 @hellofromTonya
3 years ago

  • Keywords commit added
  • Owner set to hellofromTonya
  • Status changed from new to reviewing

In reviewing and testing 53988.diff, @SergeyBiryukov I think you did resolve an issue. Bailing out without resetting the $output property to an empty array means WP_List_Util::get_output() may not be in the right state when invoked. This was why the tests were failing.

The patch looks good to me. Marking it for commit.

#9 @hellofromTonya
3 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 52066:

General: Convert wp_list_filter() into a wrapper for wp_filter_object_list().

The code in wp_list_filter() was a duplicate of wp_filter_object_list(), minus the WP_List_Util::pluck() (used when $field is configured).

In testing the wrapper, discovered an edge case (and potential bug) in WP_List_Util::filter() where if the operator matches an empty array was returned without resetting the output property. Without that property being set correctly, WP_List_Util::get_output() was not correct. This commit also fixes this by resetting the property to an empty array.

Follow-up to [15686], [17427], [38928], [51044].

Props pbearne, sergeybiryukov, hellofromTonya.
Fixes #53988.

hellofromtonya commented on PR #1615:


3 years ago
#10

Closing as the alternate patch was committed via changeset https://core.trac.wordpress.org/changeset/52066.

#11 @hellofromTonya
3 years ago

Thank you @pbearne and @SergeyBiryukov for your contributions to improving Core's codebase!

Note: See TracTickets for help on using tickets.