[wp-trac] [WordPress Trac] #47704: PHP 7.4: Fix issues regarding accessing null/bool/etc as if they were an array

WordPress Trac noreply at wordpress.org
Mon Jul 15 00:46:33 UTC 2019


#47704: PHP 7.4: Fix issues regarding accessing null/bool/etc as if they were an
array
--------------------------+-----------------------------------
 Reporter:  jrf           |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  trunk
 Severity:  normal        |   Keywords:  has-patch 2nd-opinion
  Focuses:                |
--------------------------+-----------------------------------
 == **The change in PHP 7.4**

 PHP 7.4 introduces a "''Trying to access array offset on value of type
 ...''" warning for accessing null/bool/int/float/resource (everything but
 array, string and object) as if it were an array.

 Previously PHP already threw a warning when you would attempt to use an
 offset that is of an invalid type. Now PHP will also throw a warning when
 a container is of an invalid type.

 The undocumented, default behavior, of a silent NULL return, is still
 maintained.

 Refs:
 * https://wiki.php.net/rfc/notice-for-non-valid-array-container
 * https://github.com/php/php-src/pull/4386

 == **The problem**

 While code which relies on this undocumented behaviour of PHP returning
 `null` when attempting to array access a null/bool/int/float/resource,
 will continue to work as expected, each and every instance of this will
 now throw a PHP `E_WARNING`.

 This is already causing the unit tests to fail when run against PHP 7.4 as
 can be seen in the latest Travis build against `master`: https://travis-
 ci.com/WordPress/wordpress-develop/jobs/215901645

 == **The solution**

 When using array access on the outcome of functions with mixed return
 values, i.e. `array|false` and the likes, more type checking needs to be
 done before attempting to access the return value as if it were an array
 to prevent this warning.


 == **Patches**

 For those unit tests currently failing because of this issue, I will be
 attaching patches.

 This does not imply that all instances of this issue will be solved by
 merging these patches as not all code is 100% unit tested, so there may
 well be more problem code which has not surfaced yet.

 I expect more issues to be reported - both in Core as well as in plugins
 and themes - once PHP 7.4 is out and starting to be used for real.

 == **Finding additional issues**

 At this time, AFAIK there are no (static) analysis tools available to find
 these issues.

 I can already tell you now that tools such as
 [https://github.com/PHPCompatibility/PHPCompatibility PHPCompatibility]
 will not be able to detect these issues.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47704>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list