[wp-trac] [WordPress Trac] #43025: wp_list_filter should filter all iterable objects

WordPress Trac noreply at wordpress.org
Thu Feb 1 17:35:02 UTC 2018


#43025: wp_list_filter should filter all iterable objects
----------------------------------------+------------------------------
 Reporter:  jarednova                   |       Owner:
     Type:  defect (bug)                |      Status:  new
 Priority:  normal                      |   Milestone:  Awaiting Review
Component:  General                     |     Version:  4.9.1
 Severity:  normal                      |  Resolution:
 Keywords:  has-unit-tests needs-patch  |     Focuses:
----------------------------------------+------------------------------
Changes (by flixos90):

 * keywords:  has-patch has-unit-tests => has-unit-tests needs-patch


Comment:

 Some thoughts from my end:

 * I agree with @swissspidy that the `is_array()` clauses in the beginning
 of `wp_list_filter()` (and `wp_list_sort()` and `wp_filter_object_list()`
 as well!) should be removed from there and become part of `WP_List_Util`.
 The easiest way would be to check in the constructor whether `$input` is
 an array of traversable, and if not, simply set it to an empty array.
 * I agree that `instanceof` should be used instead of `is_a()`. Also
 recommended since it's a language construct and not a function.
 * Not sure whether that's necessary, but maybe we should adjust all
 related docblocks to say "iterable" instead of array.
 * And now it gets a little more complex: `WP_List_Util::filter()` and
 `WP_List_Util::sort()` create a new array with the data from the input.
 Not sure whether we should just keep it that way or try to return the same
 object that was passed (that would probably make sense). If yes, how could
 we achieve this reasonably? Add a private utility method that ensures that
 transforms the output array back to the same type of traversable object if
 the input is one? Should we use `clone` and then populate the cloned
 object with the new values?
 * `WP_List_Util::sort()` uses `usort()` and `uasort()` both of which don't
 work with a traversable object. Maybe we really need to transform the
 traversable into an array immediately and transform it back after the
 operations.

 IMO this is a little more complex than initially thought.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/43025#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list