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:
{{{#!diff
- 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:
* [https://wiki.php.net/rfc/deprecate-implicitly-nullable-types PHP RFC: Deprecate implicitly nullable parameter types]
* [https://php.watch/versions/8.4/implicitly-marking-parameter-type-nullable-deprecated PHP.Watch: PHP 8.4: Implicitly nullable parameter declarations deprecated]

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

Props ayeshrajans, jrf, audrasjb, jorbin.
Fixes #60786.
Built from https://develop.svn.wordpress.org/trunk@58009


git-svn-id: http://core.svn.wordpress.org/trunk@57480 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2024-04-15 20:03:09 +00:00
parent 593ccf4342
commit dd6d4e74c1
5 changed files with 12 additions and 12 deletions

View File

@ -401,7 +401,7 @@ function export_wp( $args = array() ) {
*
* @param int[] $post_ids Optional. Array of post IDs to filter the query by.
*/
function wxr_authors_list( array $post_ids = null ) {
function wxr_authors_list( ?array $post_ids = null ) {
global $wpdb;
if ( ! empty( $post_ids ) ) {

View File

@ -98,7 +98,7 @@ final class WP_Translation_Controller {
* @param string $locale Optional. Locale. Default current locale.
* @return bool True on success, false otherwise.
*/
public function load_file( string $translation_file, string $textdomain = 'default', string $locale = null ): bool {
public function load_file( string $translation_file, string $textdomain = 'default', ?string $locale = null ): bool {
if ( null === $locale ) {
$locale = $this->current_locale;
}
@ -153,7 +153,7 @@ final class WP_Translation_Controller {
* @param string $locale Optional. Locale. Defaults to all locales.
* @return bool True on success, false otherwise.
*/
public function unload_file( $file, string $textdomain = 'default', string $locale = null ): bool {
public function unload_file( $file, string $textdomain = 'default', ?string $locale = null ): bool {
if ( is_string( $file ) ) {
$file = realpath( $file );
}
@ -198,7 +198,7 @@ final class WP_Translation_Controller {
* @param string $locale Optional. Locale. Defaults to all locales.
* @return bool True on success, false otherwise.
*/
public function unload_textdomain( string $textdomain = 'default', string $locale = null ): bool {
public function unload_textdomain( string $textdomain = 'default', ?string $locale = null ): bool {
$unloaded = false;
if ( null !== $locale ) {
@ -240,7 +240,7 @@ final class WP_Translation_Controller {
* @param string $locale Optional. Locale. Default current locale.
* @return bool True if there are any loaded translations, false otherwise.
*/
public function is_textdomain_loaded( string $textdomain = 'default', string $locale = null ): bool {
public function is_textdomain_loaded( string $textdomain = 'default', ?string $locale = null ): bool {
if ( null === $locale ) {
$locale = $this->current_locale;
}
@ -260,7 +260,7 @@ final class WP_Translation_Controller {
* @param string $locale Optional. Locale. Default current locale.
* @return string|false Translation on success, false otherwise.
*/
public function translate( string $text, string $context = '', string $textdomain = 'default', string $locale = null ) {
public function translate( string $text, string $context = '', string $textdomain = 'default', ?string $locale = null ) {
if ( '' !== $context ) {
$context .= "\4";
}
@ -294,7 +294,7 @@ final class WP_Translation_Controller {
* @param string $locale Optional. Locale. Default current locale.
* @return string|false Translation on success, false otherwise.
*/
public function translate_plural( array $plurals, int $number, string $context = '', string $textdomain = 'default', string $locale = null ) {
public function translate_plural( array $plurals, int $number, string $context = '', string $textdomain = 'default', ?string $locale = null ) {
if ( '' !== $context ) {
$context .= "\4";
}
@ -394,7 +394,7 @@ final class WP_Translation_Controller {
* @type string[] $entries Array of translation entries.
* }
*/
protected function locate_translation( string $singular, string $textdomain = 'default', string $locale = null ) {
protected function locate_translation( string $singular, string $textdomain = 'default', ?string $locale = null ) {
if ( array() === $this->loaded_translations ) {
return false;
}
@ -427,7 +427,7 @@ final class WP_Translation_Controller {
* @param string $locale Optional. Locale. Default current locale.
* @return WP_Translation_File[] List of translation files.
*/
protected function get_files( string $textdomain = 'default', string $locale = null ): array {
protected function get_files( string $textdomain = 'default', ?string $locale = null ): array {
if ( null === $locale ) {
$locale = $this->current_locale;
}

View File

@ -81,7 +81,7 @@ abstract class WP_Translation_File {
* @param string|null $filetype Optional. File type. Default inferred from file name.
* @return false|WP_Translation_File
*/
public static function create( string $file, string $filetype = null ) {
public static function create( string $file, ?string $filetype = null ) {
if ( ! is_readable( $file ) ) {
return false;
}

View File

@ -5499,7 +5499,7 @@ function wp_show_heic_upload_error( $plupload_settings ) {
* @param array $image_info Optional. Extended image information (passed by reference).
* @return array|false Array of image information or false on failure.
*/
function wp_getimagesize( $filename, array &$image_info = null ) {
function wp_getimagesize( $filename, ?array &$image_info = null ) {
// Don't silence errors when in debug mode, unless running unit tests.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG
&& ! defined( 'WP_RUN_CORE_TESTS' )

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.6-alpha-58005';
$wp_version = '6.6-alpha-58009';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.