[wp-trac] [WordPress Trac] #53635: PHP 8.1: various compatibility fixes

WordPress Trac noreply at wordpress.org
Fri Oct 15 22:52:58 UTC 2021


#53635: PHP 8.1: various compatibility fixes
--------------------------------------------+------------------------------
 Reporter:  SergeyBiryukov                  |       Owner:  hellofromTonya
     Type:  task (blessed)                  |      Status:  accepted
 Priority:  normal                          |   Milestone:  5.9
Component:  General                         |     Version:
 Severity:  normal                          |  Resolution:
 Keywords:  php81 has-patch has-unit-tests  |     Focuses:  coding-
                                            |  standards
--------------------------------------------+------------------------------

Comment (by hellofromTonya):

 In [changeset:"51911" 51911]:
 {{{
 #!CommitTicketReference repository="" revision="51911"
 FileSystem API: Fix autovivification deprecation notice in
 `recurse_dirsize()`.

 >PHP natively allows for autovivification (auto-creation of arrays from
 falsey values). This feature is very useful and used in a lot of PHP
 projects, especially if the variable is undefined. However, there is a
 little oddity that allows creating an array from a `false` and `null`
 value.

 The above quote is from the PHP 8.1 RFC and the (accepted) RFC changes the
 behaviour described above to deprecated auto creation of arrays from
 `false`. As it is deprecated, it _will_ still work for the time being, but
 as of PHP 9.0, this will become a Fatal Error, so we may as well fix it
 now.

 The `recurse_dirsize()` function retrieves a transient and places it in
 the `$directory_cache` variable, but the `get_transient()` function in WP
 returns `false` when the transient doesn't exist, which subsequently can
 lead to the above mentioned deprecation notice.

 By verifying that the `$directory_cache` variable is an array before
 assigning to it and initializing it to an empty array, if it's not, we
 prevent the deprecation notice, as well as harden the function against
 potentially corrupted transients where this transient would not return the
 expected array format, but some other variable type.

 Includes adding dedicated unit tests for both the PHP 8.1 issue, as well
 as the hardening against corrupted transients.

 Includes some girl-scouting: touching up a parameter description and some
 code layout.

 Refs:
 * https://wiki.php.net/rfc/autovivification_false
 * https://developer.wordpress.org/reference/functions/get_transient/

 Follow-up to [49212], [49744].

 Props jrf, hellofromTonya.
 See #53635.
 }}}

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


More information about the wp-trac mailing list