Make WordPress Core

Changeset 58009


Ignore:
Timestamp:
04/15/2024 08:01:03 PM (6 months ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/export.php

    r57681 r58009  
    402402     * @param int[] $post_ids Optional. Array of post IDs to filter the query by.
    403403     */
    404     function wxr_authors_list( array $post_ids = null ) {
     404    function wxr_authors_list( ?array $post_ids = null ) {
    405405        global $wpdb;
    406406
  • trunk/src/wp-includes/l10n/class-wp-translation-controller.php

    r57350 r58009  
    9999     * @return bool True on success, false otherwise.
    100100     */
    101     public function load_file( string $translation_file, string $textdomain = 'default', string $locale = null ): bool {
     101    public function load_file( string $translation_file, string $textdomain = 'default', ?string $locale = null ): bool {
    102102        if ( null === $locale ) {
    103103            $locale = $this->current_locale;
     
    154154     * @return bool True on success, false otherwise.
    155155     */
    156     public function unload_file( $file, string $textdomain = 'default', string $locale = null ): bool {
     156    public function unload_file( $file, string $textdomain = 'default', ?string $locale = null ): bool {
    157157        if ( is_string( $file ) ) {
    158158            $file = realpath( $file );
     
    199199     * @return bool True on success, false otherwise.
    200200     */
    201     public function unload_textdomain( string $textdomain = 'default', string $locale = null ): bool {
     201    public function unload_textdomain( string $textdomain = 'default', ?string $locale = null ): bool {
    202202        $unloaded = false;
    203203
     
    241241     * @return bool True if there are any loaded translations, false otherwise.
    242242     */
    243     public function is_textdomain_loaded( string $textdomain = 'default', string $locale = null ): bool {
     243    public function is_textdomain_loaded( string $textdomain = 'default', ?string $locale = null ): bool {
    244244        if ( null === $locale ) {
    245245            $locale = $this->current_locale;
     
    261261     * @return string|false Translation on success, false otherwise.
    262262     */
    263     public function translate( string $text, string $context = '', string $textdomain = 'default', string $locale = null ) {
     263    public function translate( string $text, string $context = '', string $textdomain = 'default', ?string $locale = null ) {
    264264        if ( '' !== $context ) {
    265265            $context .= "\4";
     
    295295     * @return string|false Translation on success, false otherwise.
    296296     */
    297     public function translate_plural( array $plurals, int $number, string $context = '', string $textdomain = 'default', string $locale = null ) {
     297    public function translate_plural( array $plurals, int $number, string $context = '', string $textdomain = 'default', ?string $locale = null ) {
    298298        if ( '' !== $context ) {
    299299            $context .= "\4";
     
    395395     * }
    396396     */
    397     protected function locate_translation( string $singular, string $textdomain = 'default', string $locale = null ) {
     397    protected function locate_translation( string $singular, string $textdomain = 'default', ?string $locale = null ) {
    398398        if ( array() === $this->loaded_translations ) {
    399399            return false;
     
    428428     * @return WP_Translation_File[] List of translation files.
    429429     */
    430     protected function get_files( string $textdomain = 'default', string $locale = null ): array {
     430    protected function get_files( string $textdomain = 'default', ?string $locale = null ): array {
    431431        if ( null === $locale ) {
    432432            $locale = $this->current_locale;
  • trunk/src/wp-includes/l10n/class-wp-translation-file.php

    r57519 r58009  
    8282     * @return false|WP_Translation_File
    8383     */
    84     public static function create( string $file, string $filetype = null ) {
     84    public static function create( string $file, ?string $filetype = null ) {
    8585        if ( ! is_readable( $file ) ) {
    8686            return false;
  • trunk/src/wp-includes/media.php

    r57921 r58009  
    55005500 * @return array|false Array of image information or false on failure.
    55015501 */
    5502 function wp_getimagesize( $filename, array &$image_info = null ) {
     5502function wp_getimagesize( $filename, ?array &$image_info = null ) {
    55035503    // Don't silence errors when in debug mode, unless running unit tests.
    55045504    if ( defined( 'WP_DEBUG' ) && WP_DEBUG
Note: See TracChangeset for help on using the changeset viewer.