WordPress.org

Make WordPress Core

Opened 4 months ago

Last modified 6 weeks ago

#49728 new defect (bug)

[PHP 8] Prepare for the internal functions throwing `TypeError` or `ValueError` exceptions on unexpected types/values

Reported by: ayeshrajans Owned by:
Milestone: 5.6 Priority: normal
Severity: minor Version:
Component: Build/Test Tools Keywords: needs-patch needs-unit-tests needs-dev-note needs-codex php8
Focuses: Cc:

Description

Description

PHP 8.0 is still in the making, but we know that certain changes are coming up to make things more straightforward and in favor of exceptions instead of rather silent PHP warnings.

One of these changes is that from PHP 8+, internal functions will throw an exception if the function call arguments are of a type that is not expected.

The simplest example would be strlen([]), which would throw TypeError: strlen() expects parameter 1 to be string, array given exception in PHP 8. You can read more at the RFC (https://wiki.php.net/rfc/consistent_type_errors) and a detailed Change Record (https://php.watch/versions/8.0/internal-function-exceptions).

WordPress has a PHPUnit annotation with the name @expectedIncorrectUsage that are used to track these kinds of errors. One example is at Tests_DB::test_prepare_sprintf_invalid_args, which uses the @expectedIncorrectUsage annotation to track PHP warnings, but instead, PHP 8's new TypeError triggers an exception (https://travis-ci.com/github/Ayesh/wordpress-develop/jobs/308369270#L2827).

Proposal

I propose to extend the expectedIncorrectUsage annotation to check the PHP version, and make PHPUnit expect a TypeError or a ValueError. Ideally, we should be expecting exactly a TypeError or a ValueError (sprintf too few arguments for example), which we can pass either from the annotation in the caller, or a check on the parent/sibling classes.

Change History (2)

#1 @desrosj
6 weeks ago

  • Milestone changed from Awaiting Review to 5.6

Let's aim to get this in 5.6, though it is dependent on PHP8 entering the beta/RC stages in time.

#2 @desrosj
6 weeks ago

  • Keywords php8 added

Also, feel free to use the php8 keyword for anything related. It makes it easier to get the big picture of all those tickets.

Note: See TracTickets for help on using tickets.