Skip to content

Commit

Permalink
Code Modernization: Fix implicit nullable parameter type deprecation …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
SergeyBiryukov committed Apr 15, 2024
1 parent 593ccf4 commit dd6d4e7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion wp-admin/includes/export.php
Expand Up @@ -401,7 +401,7 @@ function wxr_term_meta( $term ) {
*
* @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 ) ) {
Expand Down
16 changes: 8 additions & 8 deletions wp-includes/l10n/class-wp-translation-controller.php
Expand Up @@ -98,7 +98,7 @@ public function set_locale( string $locale ) {
* @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;
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public function load_file( string $translation_file, string $textdomain = 'defau
* @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 );
}
Expand Down Expand Up @@ -198,7 +198,7 @@ public function unload_file( $file, string $textdomain = 'default', string $loca
* @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 ) {
Expand Down Expand Up @@ -240,7 +240,7 @@ public function unload_textdomain( string $textdomain = 'default', string $local
* @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;
}
Expand All @@ -260,7 +260,7 @@ public function is_textdomain_loaded( string $textdomain = 'default', string $lo
* @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";
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public function translate( string $text, string $context = '', string $textdomai
* @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";
}
Expand Down Expand Up @@ -394,7 +394,7 @@ public function get_entries( string $textdomain = 'default' ): array {
* @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;
}
Expand Down Expand Up @@ -427,7 +427,7 @@ protected function locate_translation( string $singular, string $textdomain = 'd
* @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;
}
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/l10n/class-wp-translation-file.php
Expand Up @@ -81,7 +81,7 @@ protected function __construct( string $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;
}
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/media.php
Expand Up @@ -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' )
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Expand Up @@ -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.
Expand Down

0 comments on commit dd6d4e7

Please sign in to comment.