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

WordPress Trac noreply at wordpress.org
Tue Aug 17 00:16:42 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 SergeyBiryukov):

 In [changeset:"51626" 51626]:
 #!CommitTicketReference repository="" revision="51626"
 Code Modernization: Check the return type of `parse_url()` in

 As per the PHP manual:
 > If the `component` parameter is omitted, an associative array is
 > If the `component` parameter is specified, `parse_url()` returns a
 string (or an int, in the case of `PHP_URL_PORT`) instead of an array. If
 the requested component doesn't exist within the given URL, `null` will be

 Reference: [https://www.php.net/manual/en/function.parse-
 url.php#refsect1-function.parse-url-returnvalues PHP Manual: parse_url():
 Return Values]

 This commit adds three unit tests for `download_url()`:

 * The first test is "girl-scouting" to make sure that the code up to the
 point where the error is expected is tested.
 * The second test exposed a PHP 8.1 `basename(): Passing null to parameter
 #1 ($path) of type string is deprecated` error due to the call to
 `parse_url()` returning `null` when the component requested does not exist
 in the passed URL.
 * The output of the call to `parse_url()` stored in the `$url_path`
 variable is used in more places in the function logic. The third test
 exposes a second PHP 8.1 deprecation notice, this time for `substr():
 Passing null to parameter #1 ($string) of type string is deprecated`.

 This commit also removes duplicate `parse_url()` calls. Neither `$url` nor
 `$url_filename` are changed between when they are first received/defined
 and when they are re-used, so there is no need to repeat the function

 Follow-up to [51606], [51622].

 Props jrf, hellofromTonya, SergeyBiryukov.
 See #53635.

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

More information about the wp-trac mailing list