[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
Wed Dec 9 15:43:27 UTC 2020


#51986: PHP Warning: array_intersect_key(): Expected parameter 1 to be an array,
string given in class-wp-rest-server.php
------------------------------------------------+-----------------------
 Reporter:  slaFFik                             |       Owner:  (none)
     Type:  defect (bug)                        |      Status:  new
 Priority:  normal                              |   Milestone:  5.6.1
Component:  Editor                              |     Version:  5.6
 Severity:  normal                              |  Resolution:
 Keywords:  reporter-feedback needs-patch php8  |     Focuses:  rest-api
------------------------------------------------+-----------------------
Changes (by desrosj):

 * keywords:  reporter-feedback needs-testing => reporter-feedback needs-
     patch php8
 * milestone:  Awaiting Review => 5.6.1


Comment:

 > It looks like it should be `'args' => [ 'type' => [] ]` instead of
 `'args' => [ 'type' ]` (same for `slug`).

 Correct!

 One additional note here, this pattern will cause a fatal error in PHP 8
 due to strict typing now being applied to all internal PHP functionality.

 Looks like this is a result of the efforts on #51020
 ([https://github.com/WordPress/wordpress-develop/pull/634/files see the
 diff]). Non-arrays were never truly supported, but the changes in #51020
 seem to have surfaced warnings when being defined incorrectly.


 I've been working through this with @TimothyBlynJacobs a bit in Slack.

 > The args is used primarily for validation, but with those strings set,
 it wouldn't be able to validate parameters. The `get_data_for_route`
 outputs the index. So the endpoint would "work" but wouldn't be doing
 validation properly and wouldn't be outputting correct schemas, but the
 route should actually be able to process the request and the response.


 The type casting suggested above does not appear to be the correct
 solution here. While it would silence the warning, it would not force the
 correct behavior. A `_doing_it_wrong()` to notify developers of incorrect
 usage paired with skipping the item in the array when not the correct type
 seems like the safest way forward.

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


More information about the wp-trac mailing list