Make WordPress Core

Opened 4 months ago

Closed 2 months ago

#63957 closed defect (bug) (fixed)

PHP 8.5: Using `null` as array offsets

Reported by: swissspidy's profile swissspidy Owned by: swissspidy's profile swissspidy
Milestone: 6.9 Priority: normal
Severity: normal Version:
Component: General Keywords: php85 has-patch needs-testing
Focuses: php-compatibility Cc:

Description

Parent ticket for all things PHP 8.5: #63061


Using null as an array offset and when calling array_key_exists() is deprecated in PHP 8.5

There are some places in core that need to be fixed.

Two examples I've ran into today:

do_blocks calls parse_blocks on post content, which can parse a simple string as a block with blockName of null. So when functions like _wp_add_block_level_preset_styles (hooked to pre_render_block use $block['blockName'] and pass it to WP_Block_Type_Registry, this is where the warnings are triggered.

Another, more obvious on in WP_Theme_JSON: https://github.com/WordPress/wordpress-develop/blob/c5909c4c51044758e3e9d75551c70b9c7fbace5f/src/wp-includes/class-wp-theme-json.php#L2888-L2894 - $current_element can be null

Change History (17)

This ticket was mentioned in PR #9849 on WordPress/wordpress-develop by @swissspidy.


4 months ago
#1

  • Keywords has-patch added; needs-patch removed

#2 @rollybueno
4 months ago

  • Keywords needs-testing added

Interesting, I haven't updated my local PHP version to 8.5 yet so I'm adding needs-testing for other testers to it give a try. Will post my result here as well this week.

#3 @swissspidy
4 months ago

  • Owner set to swissspidy
  • Resolution set to fixed
  • Status changed from new to closed

In 60809:

Code Modernization: Fix instances of using null as an array offset.

Addresses a new deprecation in PHP 8.5.

Props swissspidy.
Fixes #63957.

@mukesh27 commented on PR #10136:


3 months ago
#5

Follow-up to #9849

@swissspidy commented on PR #10136:


3 months ago
#6

How did you encounter these? #9845 doesn't report them, which would indicate a lack of test coverage.

@mukesh27 commented on PR #10136:


3 months ago
#7

How did you encounter these? #9845 doesn't report them, which would indicate a lack of test coverage.

Yes.

@swissspidy commented on PR #10136:


3 months ago
#8

Yes there is a lack of test coverage?

In that case, some tests would be great :-)

#9 @mukesh27
3 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

@swissspidy commented on PR #10136:


3 months ago
#10

Yes there is a lack of test coverage?

In that case, some tests would be great :-)

Curious to see any stack traces where these happen in action too, to find the root cause

#11 @swissspidy
3 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 60904:

Code Modernization: Fix instances of using null as an array offset.

Addresses a new deprecation in PHP 8.5 in several block-related registry classes.

Follow-up to [60809].

Props mukesh27, swissspidy.
Fixes #63957.

#12 @desrosj
2 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

It seems that 8.5 RC4 fixes a bug that prevented instances where a null offset is used from being flagged as deprecated. As a result, the test suite is currently failing for single site jobs using PHP 8.5.

This ticket was mentioned in Slack in #core by desrosj. View the logs.


2 months ago

This ticket was mentioned in PR #10498 on WordPress/wordpress-develop by @westonruter.


2 months ago
#14

This avoids a deprecation warning in PHP 8.5:

Using null as an array offset is deprecated, use an empty string instead

/var/www/src/wp-includes/class-wp-date-query.php:516
/var/www/src/wp-includes/class-wp-date-query.php:170
/var/www/src/wp-includes/class-wp-query.php:2138
/var/www/src/wp-includes/class-wp-query.php:3958
/var/www/src/wp-includes/class-wp.php:704
/var/www/src/wp-includes/class-wp.php:824
/var/www/tests/phpunit/includes/abstract-testcase.php:1346
/var/www/tests/phpunit/includes/testcase-canonical.php:303
/var/www/tests/phpunit/tests/canonical.php:68
/var/www/vendor/bin/phpunit:122

Specifically, the issue is that $wpdb->blogs is null when not on a single-site install. So this ensures that the $wpdb->blogs column is only added to the $known_columns array when on multisite.

It also updates the $valid_columns array to only include multisite-related columns when on multisite, namely registered and last_updated.

[!NOTE]
Some multisite columns appear to not have been accounted for: wp_blog_versions.last_updated, wp_registration_log.date_registered, and wp_signups.activated.

Lastly, the phpdoc @var tags for $wpdb->blogs, $wpdb->blogmeta, and the other multisite-specific tables have been updated to indicate they may be null.

Trac ticket: https://core.trac.wordpress.org/ticket/63957

This issue was introduced in r37477 (https://github.com/WordPress/wordpress-develop/commit/381930974cdcf35b0db6539aaf91ea90d37c3787) for WordPress 4.6.0.

@westonruter commented on PR #10498:


2 months ago
#15

Added a few small nits. I don't think it's in the coding standards, but I prefer to have an empty line before a control structures.

Cool. I'll apply when committing.

@desrosj commented on PR #10498:


2 months ago
#16

I changed the PR description to point to Core-63957 since that's the more specific issue being addressed here.

#17 @westonruter
2 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 61191:

Code Modernization: Fix instances of using null as an array offset.

This avoids using multisite-specific tables and columns in WP_Date_Query::validate_column() when on a non-multisite install. Attempting to use $wpdb->blogs as an array index when null on a non-multisite install causes a deprecation notice in PHP 8.5. This also updates the multisite table alias in wpdb to make it clear they could be null.

Developed in https://github.com/WordPress/wordpress-develop/pull/10498

Follow-up to [60904], [60809], [37477].

Props westonruter, desrosj.
See #63061.
Fixes #63957.

Note: See TracTickets for help on using tickets.