From 87bc9ee47e45446ef04ce92a2d3fd2d73fd9a3f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Mon, 14 Sep 2026 09:30:10 +0200 Subject: [PATCH] Build/Test Tools: Use the null coalescing assignment operator in the test suite --- tests/phpstan/HookDocBlock.php | 12 +++------ tests/phpunit/includes/abstract-testcase.php | 4 +-- .../class-wp-phpunit-timing-metrics.php | 4 +-- .../phpunit/includes/class-wp-test-stream.php | 12 +++------ .../class-wp-unittest-factory-for-thing.php | 10 +++---- tests/phpunit/includes/utils.php | 12 +++------ .../tests/admin/plugin-dependencies/base.php | 12 +++------ tests/phpunit/tests/blocks/register.php | 10 +++---- .../blocks/registerCoreBlockStyleHandles.php | 8 ++---- .../font-face/wp-font-face-tests-dataset.php | 20 ++++++-------- .../tests/rest-api/rest-schema-validation.php | 18 +++++-------- tests/phpunit/tests/url.php | 26 +++++++------------ .../phpunit/tests/wp-token-map/wpTokenMap.php | 4 +-- 13 files changed, 48 insertions(+), 104 deletions(-) diff --git a/tests/phpstan/HookDocBlock.php b/tests/phpstan/HookDocBlock.php index 6d7cca2f69f11..b32c24af6d55f 100644 --- a/tests/phpstan/HookDocBlock.php +++ b/tests/phpstan/HookDocBlock.php @@ -216,9 +216,7 @@ public function getReferencedHookDocsHash(): string { // depend on where the checkout lives. $key = $this->getRootRelativePath( $target ); - if ( ! isset( $docs[ $key ] ) ) { - $docs[ $key ] = $this->getHookDocs( $target ); - } + $docs[ $key ] ??= $this->getHookDocs( $target ); } } @@ -562,9 +560,7 @@ private function findHookDoc( string $file, array $matcher ): ?string { * @return HookDocs */ private function getHookDocs( string $file ): array { - if ( ! isset( $this->fileHookDocs[ $file ] ) ) { - $this->fileHookDocs[ $file ] = self::loadHookDocs( $file ); - } + $this->fileHookDocs[ $file ] ??= self::loadHookDocs( $file ); return $this->fileHookDocs[ $file ]; } @@ -663,9 +659,7 @@ private static function parseHookDocs( string $code ): array { $name_expr = $args[0]->value; if ( $name_expr instanceof String_ ) { - if ( ! isset( $docs['exact'][ $name_expr->value ] ) ) { - $docs['exact'][ $name_expr->value ] = $doc->getText(); - } + $docs['exact'][ $name_expr->value ] ??= $doc->getText(); continue; } diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 7116d3076266b..3cb53bc093c34 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -1406,9 +1406,7 @@ public function go_to( $url ) { } else { $req = $url; } - if ( ! isset( $parts['query'] ) ) { - $parts['query'] = ''; - } + $parts['query'] ??= ''; $_SERVER['REQUEST_URI'] = $req; unset( $_SERVER['PATH_INFO'] ); diff --git a/tests/phpunit/includes/class-wp-phpunit-timing-metrics.php b/tests/phpunit/includes/class-wp-phpunit-timing-metrics.php index 3d7b6e61d693f..e6217bca87077 100644 --- a/tests/phpunit/includes/class-wp-phpunit-timing-metrics.php +++ b/tests/phpunit/includes/class-wp-phpunit-timing-metrics.php @@ -100,9 +100,7 @@ public static function from_file( $file, $testcase_callback = null ) { throw new RuntimeException( 'The JUnit timing report contains no testcases.' ); } - if ( null === $suite_time ) { - $suite_time = array_sum( $test_times ); - } + $suite_time ??= array_sum( $test_times ); sort( $test_times, SORT_NUMERIC ); diff --git a/tests/phpunit/includes/class-wp-test-stream.php b/tests/phpunit/includes/class-wp-test-stream.php index d884a5168310d..5d6507c303baf 100644 --- a/tests/phpunit/includes/class-wp-test-stream.php +++ b/tests/phpunit/includes/class-wp-test-stream.php @@ -60,9 +60,7 @@ private function open( $url ) { throw new Exception( 'Cannot use an empty bucket name' ); } - if ( ! isset( WP_Test_Stream::$data[ $this->bucket ] ) ) { - WP_Test_Stream::$data[ $this->bucket ] = array(); - } + WP_Test_Stream::$data[ $this->bucket ] ??= array(); $this->data_ref =& WP_Test_Stream::$data[ $this->bucket ][ $this->file ]; @@ -101,9 +99,7 @@ public function stream_read( $count ) { * @see streamWrapper::stream_write */ public function stream_write( $data ) { - if ( ! isset( $this->data_ref ) ) { - $this->data_ref = ''; - } + $this->data_ref ??= ''; $left = substr( $this->data_ref, 0, $this->position ); $right = substr( $this->data_ref, $this->position + strlen( $data ) ); @@ -186,9 +182,7 @@ public function stream_eof() { public function stream_metadata( $path, $option, $value ) { $this->open( $path ); if ( STREAM_META_TOUCH === $option ) { - if ( ! isset( $this->data_ref ) ) { - $this->data_ref = ''; - } + $this->data_ref ??= ''; return true; } return false; diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php index 89cefcaf3b306..f0595aded73e0 100644 --- a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php @@ -62,9 +62,7 @@ abstract public function update_object( $object_id, $fields ); * @return int|WP_Error The object ID on success, WP_Error object on failure. */ public function create( $args = array(), $generation_definitions = null ) { - if ( is_null( $generation_definitions ) ) { - $generation_definitions = $this->default_generation_definitions; - } + $generation_definitions ??= $this->default_generation_definitions; $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks ); $object_id = $this->create_object( $generated_args ); @@ -155,10 +153,8 @@ public function create_many( $count, $args = array(), $generation_definitions = * @return array|WP_Error Combined array on success. WP_Error when default value is incorrect. */ public function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) { - $callbacks = array(); - if ( is_null( $generation_definitions ) ) { - $generation_definitions = $this->default_generation_definitions; - } + $callbacks = array(); + $generation_definitions ??= $this->default_generation_definitions; // Use the same incrementor for all fields belonging to this object. $gen = new WP_UnitTest_Generator_Sequence(); diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index 02254903fa745..989376d4e983b 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -110,10 +110,8 @@ public function current_filter() { if ( is_callable( 'current_filter' ) ) { $current_filter = current_filter(); } else { - $current_filter = array_key_last( $wp_actions ); - if ( null === $current_filter ) { - $current_filter = false; - } + $current_filter = array_key_last( $wp_actions ); + $current_filter ??= false; } return $current_filter; @@ -393,10 +391,8 @@ public function start_handler( $parser, $name, $attributes ) { public function data_handler( $parser, $data ) { $index = count( $this->data ) - 1; - if ( ! isset( $this->data[ $index ]['content'] ) ) { - $this->data[ $index ]['content'] = ''; - } - $this->data[ $index ]['content'] .= $data; + $this->data[ $index ]['content'] ??= ''; + $this->data[ $index ]['content'] .= $data; } public function end_handler( $parser, $name ) { diff --git a/tests/phpunit/tests/admin/plugin-dependencies/base.php b/tests/phpunit/tests/admin/plugin-dependencies/base.php index badd4cfe63c92..bcec6f7da65d8 100644 --- a/tests/phpunit/tests/admin/plugin-dependencies/base.php +++ b/tests/phpunit/tests/admin/plugin-dependencies/base.php @@ -92,9 +92,7 @@ private function reset_static_properties() { * @param mixed $value The new value. */ public function set_property_value( $property, $value ) { - if ( ! isset( self::$reflected_members[ $property ] ) ) { - self::$reflected_members[ $property ] = new ReflectionProperty( self::$instance, $property ); - } + self::$reflected_members[ $property ] ??= new ReflectionProperty( self::$instance, $property ); if ( PHP_VERSION_ID < 80100 ) { self::$reflected_members[ $property ]->setAccessible( true ); @@ -112,9 +110,7 @@ public function set_property_value( $property, $value ) { * @return mixed The value of the property. */ public function get_property_value( $property ) { - if ( ! isset( self::$reflected_members[ $property ] ) ) { - self::$reflected_members[ $property ] = new ReflectionProperty( self::$instance, $property ); - } + self::$reflected_members[ $property ] ??= new ReflectionProperty( self::$instance, $property ); if ( PHP_VERSION_ID < 80100 ) { self::$reflected_members[ $property ]->setAccessible( true ); @@ -136,9 +132,7 @@ public function get_property_value( $property ) { * @return mixed The result of the method call. */ protected function call_method( $method, ...$args ) { - if ( ! isset( self::$reflected_members[ $method ] ) ) { - self::$reflected_members[ $method ] = new ReflectionMethod( self::$instance, $method ); - } + self::$reflected_members[ $method ] ??= new ReflectionMethod( self::$instance, $method ); if ( PHP_VERSION_ID < 80100 ) { self::$reflected_members[ $method ]->setAccessible( true ); diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 81cebf27c798c..1901d0ccf6702 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -721,13 +721,9 @@ public function test_register_block_style_handle_uses_correct_core_stylesheet( $ $block_name = str_replace( 'core/', '', $metadata['name'] ); // Normalize metadata similar to `register_block_type_from_metadata()`. - $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) ); - if ( ! isset( $metadata['style'] ) ) { - $metadata['style'] = "wp-block-$block_name"; - } - if ( ! isset( $metadata['editorStyle'] ) ) { - $metadata['editorStyle'] = "wp-block-{$block_name}-editor"; - } + $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) ); + $metadata['style'] ??= "wp-block-$block_name"; + $metadata['editorStyle'] ??= "wp-block-{$block_name}-editor"; // Ensure block assets are separately registered. add_filter( 'should_load_separate_core_block_assets', '__return_true' ); diff --git a/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php b/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php index cb798b20ba527..cba79c28b1515 100644 --- a/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php +++ b/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php @@ -181,12 +181,8 @@ private function get_block_data() { $data = array(); foreach ( $core_blocks_meta as $name => $schema ) { - if ( ! isset( $schema['style'] ) ) { - $schema['style'] = "wp-block-$name"; - } - if ( ! isset( $schema['editorStyle'] ) ) { - $schema['editorStyle'] = "wp-block-{$name}-editor"; - } + $schema['style'] ??= "wp-block-$name"; + $schema['editorStyle'] ??= "wp-block-{$name}-editor"; $data[ $name ] = $schema; } diff --git a/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php b/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php index c0d7f9e328016..480785643c2a1 100644 --- a/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php +++ b/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php @@ -386,12 +386,10 @@ public static function get_custom_font_families( $key = '' ) { ), ); - if ( null === $data ) { - $data = array( - 'input' => $custom_theme_json_fonts, - 'expected' => $expected_font_faces, - ); - } + $data ??= array( + 'input' => $custom_theme_json_fonts, + 'expected' => $expected_font_faces, + ); return $data[ $key ] ?? $data; } @@ -473,12 +471,10 @@ public static function get_custom_style_variations( $key = '' ) { @font-face{font-family:"DM Sans";font-style:italic;font-weight:500;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Medium-Italic.woff2') format('woff2');font-stretch:normal;} CSS; - if ( null === $data ) { - $data = array( - 'expected' => $expected_font_families, - 'expected_styles' => $expected_styles, - ); - } + $data ??= array( + 'expected' => $expected_font_families, + 'expected_styles' => $expected_styles, + ); return $data[ $key ] ?? $data; } diff --git a/tests/phpunit/tests/rest-api/rest-schema-validation.php b/tests/phpunit/tests/rest-api/rest-schema-validation.php index f83c4817718c4..bee72dbf29a37 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-validation.php +++ b/tests/phpunit/tests/rest-api/rest-schema-validation.php @@ -1697,18 +1697,14 @@ public function data_unique_items() { continue; } // type is required for our implementation - if ( ! isset( $suite['schema']['type'] ) ) { - $suite['schema']['type'] = 'array'; - } + $suite['schema']['type'] ??= 'array'; // items is required for our implementation - if ( ! isset( $suite['schema']['items'] ) ) { - $suite['schema']['items'] = array( - 'type' => $all_types, - 'items' => array( - 'type' => $all_types, - ), - ); - } + $suite['schema']['items'] ??= array( + 'type' => $all_types, + 'items' => array( + 'type' => $all_types, + ), + ); foreach ( $suite['tests'] as $test ) { $tests[] = array( $test, $suite ); } diff --git a/tests/phpunit/tests/url.php b/tests/phpunit/tests/url.php index 3734891eb19ac..27d5d5122135b 100644 --- a/tests/phpunit/tests/url.php +++ b/tests/phpunit/tests/url.php @@ -364,11 +364,9 @@ public function test_get_adjacent_post() { $post_id = self::factory()->post->create( array( 'post_date' => gmdate( 'Y-m-d H:i:s', $now - 1 ) ) ); $post_id2 = self::factory()->post->create( array( 'post_date' => gmdate( 'Y-m-d H:i:s', $now ) ) ); - if ( ! isset( $GLOBALS['post'] ) ) { - $GLOBALS['post'] = null; - } - $orig_post = $GLOBALS['post']; - $GLOBALS['post'] = get_post( $post_id2 ); + $GLOBALS['post'] ??= null; + $orig_post = $GLOBALS['post']; + $GLOBALS['post'] = get_post( $post_id2 ); $p = get_adjacent_post(); $this->assertInstanceOf( 'WP_Post', $p ); @@ -416,10 +414,8 @@ public function test_get_adjacent_post_should_return_private_posts_belonging_to_ ) ); - if ( ! isset( $GLOBALS['post'] ) ) { - $GLOBALS['post'] = null; - } - $orig_post = $GLOBALS['post']; + $GLOBALS['post'] ??= null; + $orig_post = $GLOBALS['post']; $GLOBALS['post'] = get_post( $p2 ); @@ -456,10 +452,8 @@ public function test_get_adjacent_post_should_return_private_posts_belonging_to_ ) ); - if ( ! isset( $GLOBALS['post'] ) ) { - $GLOBALS['post'] = null; - } - $orig_post = $GLOBALS['post']; + $GLOBALS['post'] ??= null; + $orig_post = $GLOBALS['post']; $GLOBALS['post'] = get_post( $p2 ); @@ -502,10 +496,8 @@ public function test_get_adjacent_post_should_not_return_private_posts_belonging ) ); - if ( ! isset( $GLOBALS['post'] ) ) { - $GLOBALS['post'] = null; - } - $orig_post = $GLOBALS['post']; + $GLOBALS['post'] ??= null; + $orig_post = $GLOBALS['post']; $GLOBALS['post'] = get_post( $p3 ); diff --git a/tests/phpunit/tests/wp-token-map/wpTokenMap.php b/tests/phpunit/tests/wp-token-map/wpTokenMap.php index 85311eeb76e7c..5cdfccfbb5624 100644 --- a/tests/phpunit/tests/wp-token-map/wpTokenMap.php +++ b/tests/phpunit/tests/wp-token-map/wpTokenMap.php @@ -383,9 +383,7 @@ private function get_html5_test_dataset() { private static function get_html5_token_map() { static $html5_token_map = null; - if ( ! isset( $html5_token_map ) ) { - $html5_token_map = WP_Token_Map::from_array( self::get_test_input_array( 'HTML5' ) ); - } + $html5_token_map ??= WP_Token_Map::from_array( self::get_test_input_array( 'HTML5' ) ); return $html5_token_map; }