[wp-trac] [WordPress Trac] #51986: PHP Warning: array_intersect_key(): Expected parameter 1 to be an array, string given in class-wp-rest-server.php

WordPress Trac noreply at wordpress.org
Tue Sep 27 21:31:18 UTC 2022


#51986: PHP Warning: array_intersect_key(): Expected parameter 1 to be an array,
string given in class-wp-rest-server.php
-------------------------------------------+----------------------------
 Reporter:  slaFFik                        |       Owner:  davidbaumwald
     Type:  defect (bug)                   |      Status:  reopened
 Priority:  normal                         |   Milestone:  6.1
Component:  Editor                         |     Version:  5.6
 Severity:  normal                         |  Resolution:
 Keywords:  php8 has-patch has-unit-tests  |     Focuses:  rest-api
-------------------------------------------+----------------------------
Changes (by TobiasBg):

 * status:  closed => reopened
 * resolution:  fixed =>


Comment:

 {{{#!php
 if ( count( array_filter( $arg_group['args'], 'is_array' ) ) !== count(
 $arg_group['args'] ) ) {
 }}}
 as a custom implementation of an `any()` function is not very readable and
 not that nice performance-wise (`array_filter()` will always loop the full
 array, even though we are only interested in finding one (the first) non-
 array).

 How about a simple `foreach` that is left early once a match is found?:
 {{{#!php
 foreach( $arg_group['args'] as $arg ) {
         if ( ! is_array( $arg ) ) {
                 _doing_it_wrong(
                         __FUNCTION__,
                         sprintf(
                                 /* translators: %s: The REST API route
 being registered. */
                                 __( 'REST API $args should be an array of
 arrays. Non-array value detected for %s.' ),
                                 '<code>' . $clean_namespace . '/' . trim(
 $route, '/' ) . '</code>'
                         ),
                         '6.1.0'
                 );
                 break; // Leave the foreach loop once one non-array
 argument was found.
         }
 }
 }}}
 instead?

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


More information about the wp-trac mailing list