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

WordPress Trac noreply at wordpress.org
Sun Feb 11 17:49:30 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:
----------------------------------------+------------------------------

Comment (by jarednova):

 @flixos90 I resolved some of the points you raised in `43025.2.diff`...

 > 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 created a function called `WP_List_Util::maybe_list()` to take an input
 and determine whether it's a valid iterable object. If it's not, it just
 sends back an empty array. I took a stab at the naming here — would love
 your thoughts on the name and whether this matches with what you were
 looking for.
 ------
 > I agree that instanceof should be used instead of is_a(). Also
 recommended since it's a language construct and not a function.

 Done!
 ------

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

 Like you said, this "more complex" stuff I left for now

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


More information about the wp-trac mailing list