Make WordPress Core

Changeset 63501


Ignore:
Timestamp:
09/05/2026 12:43:21 PM (2 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Use the null coalescing assignment operator for default value assignments.

Replacing further isset()/null guards that assign a default value with the null coalescing assignment operator (??=):

// Before
if ( ! isset( $size_data['width'] ) ) {
	$size_data['width'] = null;
}

if ( ! isset( $size_data['height'] ) ) {
	$size_data['height'] = null;
}

if ( ! isset( $size_data['crop'] ) ) {
	$size_data['crop'] = false;
}


// After
$size_data['width']  ??= null;
$size_data['height'] ??= null;
$size_data['crop']   ??= false;

Both forms are functionally identical; ??= is shorter and turns a nine-line block into three lines that read as a single list of defaults.

Scope

This is deliberately limited to blocks where every guard in a contiguous run can be converted, so no mixed styles are left behind within one function:

  • WP_Image_Editor_GD::resize() and WP_Image_Editor_Imagick::resize() — the $size_data defaults (identical blocks in both classes).
  • WP_Date_Query::build_mysql_datetime() — the $datetime defaults.
  • WP_Translation_Controller — the $locale fallback in load_file(), is_textdomain_loaded(), get_files() and has_translation().
  • register_post_status() — the $args defaults.

Why this is safe

  • ??= requires PHP 7.4, which is already the minimum supported version.
  • ??= and ! isset() test the same condition, so the default is applied in exactly the same cases.
  • Assignment order is preserved everywhere it matters. In register_post_status() the compound guard above the block is left untouched and still runs first, $args->internal ??= false; still follows it, and publicly_queryable is still derived after public has been resolved.
  • In WP_Date_Query::build_mysql_datetime() the preceding array_map( 'absint', $datetime ) turns any existing null into 0, which both isset() and ??= treat as set.
  • No behavior change.

Not converted

Similar runs in WP_Query::parse_query(), WP_Post_Type::set_props() and WP_Taxonomy::set_props() contain guards that cannot be expressed as ??= — an elseif branch in the first case, compound conditions such as if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) in the other two. Converting only the neighbouring guards would leave two styles interleaved in the same block, so those are left alone.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/12911.

Follow-up to r63443.

Props Soean, mukesh27.
See #65823.

Location:
trunk/src/wp-includes
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-date-query.php

    r63420 r63501  
    934934                $datetime = array_map( 'absint', $datetime );
    935935
    936                 if ( ! isset( $datetime['year'] ) ) {
    937                         $datetime['year'] = current_time( 'Y' );
    938                 }
    939 
    940                 if ( ! isset( $datetime['month'] ) ) {
    941                         $datetime['month'] = ( $default_to_max ) ? 12 : 1;
    942                 }
    943 
    944                 if ( ! isset( $datetime['day'] ) ) {
    945                         $datetime['day'] = ( $default_to_max ) ? (int) gmdate( 't', mktime( 0, 0, 0, $datetime['month'], 1, $datetime['year'] ) ) : 1;
    946                 }
    947 
    948                 if ( ! isset( $datetime['hour'] ) ) {
    949                         $datetime['hour'] = ( $default_to_max ) ? 23 : 0;
    950                 }
    951 
    952                 if ( ! isset( $datetime['minute'] ) ) {
    953                         $datetime['minute'] = ( $default_to_max ) ? 59 : 0;
    954                 }
    955 
    956                 if ( ! isset( $datetime['second'] ) ) {
    957                         $datetime['second'] = ( $default_to_max ) ? 59 : 0;
    958                 }
     936                $datetime['year']   ??= current_time( 'Y' );
     937                $datetime['month']  ??= ( $default_to_max ) ? 12 : 1;
     938                $datetime['day']    ??= ( $default_to_max ) ? (int) gmdate( 't', mktime( 0, 0, 0, $datetime['month'], 1, $datetime['year'] ) ) : 1;
     939                $datetime['hour']   ??= ( $default_to_max ) ? 23 : 0;
     940                $datetime['minute'] ??= ( $default_to_max ) ? 59 : 0;
     941                $datetime['second'] ??= ( $default_to_max ) ? 59 : 0;
    959942
    960943                return sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $datetime['year'], $datetime['month'], $datetime['day'], $datetime['hour'], $datetime['minute'], $datetime['second'] );
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r62517 r63501  
    313313                $orig_size = $this->size;
    314314
    315                 if ( ! isset( $size_data['width'] ) ) {
    316                         $size_data['width'] = null;
    317                 }
    318 
    319                 if ( ! isset( $size_data['height'] ) ) {
    320                         $size_data['height'] = null;
    321                 }
    322 
    323                 if ( ! isset( $size_data['crop'] ) ) {
    324                         $size_data['crop'] = false;
    325                 }
     315                $size_data['width']  ??= null;
     316                $size_data['height'] ??= null;
     317                $size_data['crop']   ??= false;
    326318
    327319                $resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r63378 r63501  
    876876                $orig_image = $this->image->getImage();
    877877
    878                 if ( ! isset( $size_data['width'] ) ) {
    879                         $size_data['width'] = null;
    880                 }
    881 
    882                 if ( ! isset( $size_data['height'] ) ) {
    883                         $size_data['height'] = null;
    884                 }
    885 
    886                 if ( ! isset( $size_data['crop'] ) ) {
    887                         $size_data['crop'] = false;
    888                 }
     878                $size_data['width']  ??= null;
     879                $size_data['height'] ??= null;
     880                $size_data['crop']   ??= false;
    889881
    890882                if ( ( $this->size['width'] === $size_data['width'] ) && ( $this->size['height'] === $size_data['height'] ) ) {
  • trunk/src/wp-includes/l10n/class-wp-translation-controller.php

    r63443 r63501  
    9898         */
    9999        public function load_file( string $translation_file, string $textdomain = 'default', ?string $locale = null ): bool {
    100                 if ( null === $locale ) {
    101                         $locale = $this->current_locale;
    102                 }
     100                $locale ??= $this->current_locale;
    103101
    104102                $translation_file = realpath( $translation_file );
    … …  
    240238         */
    241239        public function is_textdomain_loaded( string $textdomain = 'default', ?string $locale = null ): bool {
    242                 if ( null === $locale ) {
    243                         $locale = $this->current_locale;
    244                 }
     240                $locale ??= $this->current_locale;
    245241
    246242                return isset( $this->loaded_translations[ $locale ][ $textdomain ] ) &&
    … …  
    427423         */
    428424        protected function get_files( string $textdomain = 'default', ?string $locale = null ): array {
    429                 if ( null === $locale ) {
    430                         $locale = $this->current_locale;
    431                 }
     425                $locale ??= $this->current_locale;
    432426
    433427                return $this->loaded_translations[ $locale ][ $textdomain ] ?? array();
    … …  
    445439         */
    446440        public function has_translation( string $singular, string $textdomain = 'default', ?string $locale = null ): bool {
    447                 if ( null === $locale ) {
    448                         $locale = $this->current_locale;
    449                 }
     441                $locale ??= $this->current_locale;
    450442
    451443                return false !== $this->locate_translation( $singular, $textdomain, $locale );
  • trunk/src/wp-includes/post.php

    r63497 r63501  
    14881488        }
    14891489
    1490         if ( null === $args->public ) {
    1491                 $args->public = false;
    1492         }
    1493 
    1494         if ( null === $args->private ) {
    1495                 $args->private = false;
    1496         }
    1497 
    1498         if ( null === $args->protected ) {
    1499                 $args->protected = false;
    1500         }
    1501 
    1502         if ( null === $args->internal ) {
    1503                 $args->internal = false;
    1504         }
    1505 
    1506         if ( null === $args->publicly_queryable ) {
    1507                 $args->publicly_queryable = $args->public;
    1508         }
    1509 
    1510         if ( null === $args->exclude_from_search ) {
    1511                 $args->exclude_from_search = $args->internal;
    1512         }
    1513 
    1514         if ( null === $args->show_in_admin_all_list ) {
    1515                 $args->show_in_admin_all_list = ! $args->internal;
    1516         }
    1517 
    1518         if ( null === $args->show_in_admin_status_list ) {
    1519                 $args->show_in_admin_status_list = ! $args->internal;
    1520         }
    1521 
    1522         if ( null === $args->date_floating ) {
    1523                 $args->date_floating = false;
    1524         }
     1490        $args->public                    ??= false;
     1491        $args->private                   ??= false;
     1492        $args->protected                 ??= false;
     1493        $args->internal                  ??= false;
     1494        $args->publicly_queryable        ??= $args->public;
     1495        $args->exclude_from_search       ??= $args->internal;
     1496        $args->show_in_admin_all_list    ??= ! $args->internal;
     1497        $args->show_in_admin_status_list ??= ! $args->internal;
     1498        $args->date_floating             ??= false;
    15251499
    15261500        if ( false === $args->label ) {
Note: See TracChangeset for help on using the changeset viewer.