[wp-trac] [WordPress Trac] #49728: [PHP 8] Prepare for the internal functions throwing `TypeError` or `ValueError` exceptions on unexpected types/values

WordPress Trac noreply at wordpress.org
Mon Mar 30 12:02:20 UTC 2020


#49728: [PHP 8] Prepare for the internal functions throwing `TypeError` or
`ValueError` exceptions on unexpected types/values
-------------------------+-------------------------------------------------
 Reporter:  ayeshrajans  |      Owner:  (none)
     Type:  defect       |     Status:  new
  (bug)                  |
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Build/Test   |    Version:
  Tools                  |   Keywords:  needs-patch needs-unit-tests needs-
 Severity:  minor        |  dev-note needs-codex
  Focuses:               |
-------------------------+-------------------------------------------------
 **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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/49728>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list