Make WordPress Core

Opened 5 weeks ago

Closed 3 days ago

Last modified 3 days ago

#60786 closed defect (bug) (fixed)

[PHP 8.4] Fix implicit nullable parameter type depcation

Reported by: ayeshrajans's profile ayeshrajans Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.6 Priority: normal
Severity: normal Version:
Component: General Keywords: php84 has-patch
Focuses: php-compatibility Cc:

Description

In PHP 8.4, declaring function/method parameters with a default value of null is deprecated if the type is not nullable. RFC and PHP 8.4: Implicitly nullable parameter declarations deprecated

Out current minimum PHP requirement is PHP 7.0, but nullable types are only supported in PHP 7.1 and later.

Submitting the PR with the fixes and to start a conversation, but merging these changes without bumping minimum PHP requirement to 7.1 will break things on PHP 7.0.

Change History (9)

#1 @ayeshrajans
5 weeks ago

(gentle ping to @jrf, I'm sure Juliette was working on an automated fix, but I couldn't find a ticket. If there is one, I apologize and will happily close this one)

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


5 weeks ago
#2

Fixes all issues that emit deprecation notices on PHP 8.4 for implicit nullable parameter type declarations.

See:

Exclude potential changes in the Requests library and sodium_compat.

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

#3 @audrasjb
5 weeks ago

  • Version trunk deleted

Thanks for the ticket and patch.
Just noting that this implementation would require to drop support for PHP 7.0 ahead of the change.

#4 @jrf
5 weeks ago

  • Version set to trunk

@ayeshrajans Thanks for the ping. I'm not working on an automated solution (as the solution should be decided on a case-by-case basis), though I have written a sniff to reliably find the issues already ;-)

I've had a look at your patch, but as the minimum PHP version is currently still 7.0, the patch is not viable.

Aside from the export.php and the media.php file, the other files are relatively new files, which should never have been allowed to be committed with scalar type declarations as without enforcing strict types throughout the eco-system (which we can't), that's next to useless and will hide bugs, so in my opinion, removing the type declarations from the function signatures in those files would be the correct solution direction.

As for export.php and media.php, as the involved functions are global functions, there is no risk of the function being overloaded in a child class, so removing the type and optionally, selectively casting to array from within the function should allow for a workable solution.

#5 @jorbin
4 weeks ago

  • Milestone changed from Awaiting Review to 6.6

As there is a discussion about raising the minimum version during 6.6, I'm moving this to that milestone.

#6 @audrasjb
4 weeks ago

  • Version trunk deleted

Removing again trunk version label as this is not something only introduced in trunk.

#7 @jrf
10 days ago

As the version bump to a minimum of PHP 7.2 has been approved, let's keep an eye on the bump patch going in and get this patch committed as soon as the version drop has been committed to trunk.

#8 @SergeyBiryukov
3 days ago

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

In 58009:

Code Modernization: Fix implicit nullable parameter type deprecation on PHP 8.4.

In PHP 8.4, declaring function or method parameters with a default value of null is deprecated if the type is not nullable.

PHP applications are recommended to explicitly declare the type as nullable. All type declarations that have a default value of null, but without declaring null in the type declaration, will emit a deprecation notice:

function test( array $value = null ) {}

Deprecated: Implicitly marking parameter $value as nullable is deprecated, the explicit nullable type must be used instead

Recommended Changes

Change the implicit nullable type declaration to a nullable type declaration, available since PHP 7.1:

- function test( string $test = null ) {}
+ function test( ?string $test = null ) {}

This commit updates the affected instances in core to use a nullable type declaration.

References:

Follow-up to [28731], [50552], [57337], [57985].

Props ayeshrajans, jrf, audrasjb, jorbin.
Fixes #60786.

@SergeyBiryukov commented on PR #6280:


3 days ago
#9

Thanks for the PR! Merged in r58009.

Note: See TracTickets for help on using tickets.