From 3310f2247e7f5d3901c55c8b5527bb1e752d089f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Wed, 21 Oct 2026 08:59:26 +0200 Subject: [PATCH] Tests: Use the null coalescing operators in the test suite. Replaces `isset()` and `array_key_exists()` ternaries that return the tested value or `null` (or an empty string) with the null coalescing operator (`??`), and self-referencing `$x = $x ?? $y` assignments with the null coalescing assignment operator (`??=`). For `array_key_exists()`, the only case in which the two forms differ is a key that exists with a `null` value, and both then return `null`, so every converted site is equivalent. Ternaries with a non-null fallback after `array_key_exists()` or with compound conditions, such as an additional `is_string()` check, are left as-is. Follow-up to r63609, r63633. See #65819. --- tests/phpunit/includes/secrets/class-mock-store.php | 2 +- .../phpunit/tests/admin/includes/plugin/pluginPausedState.php | 2 +- tests/phpunit/tests/ai-client/wpAiClientEmbeddingBuilder.php | 2 +- tests/phpunit/tests/dependencies/wpPrefetchAdminAssets.php | 4 ++-- .../tests/error-protection/wpRecoveryModeLinkService.php | 2 +- tests/phpunit/tests/link/getSearchLink.php | 2 +- tests/phpunit/tests/load/wpGetServerProtocol.php | 2 +- tests/phpunit/tests/load/wpIsJsonRequest.php | 4 ++-- tests/phpunit/tests/load/wpIsMaintenanceMode.php | 2 +- tests/phpunit/tests/load/wpIsXmlRequest.php | 4 ++-- tests/phpunit/tests/load/wpStartScrapingEditedFileErrors.php | 2 +- 11 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/includes/secrets/class-mock-store.php b/tests/phpunit/includes/secrets/class-mock-store.php index 75eada46b6f18..784a1db0280d0 100644 --- a/tests/phpunit/includes/secrets/class-mock-store.php +++ b/tests/phpunit/includes/secrets/class-mock-store.php @@ -40,7 +40,7 @@ public function get( string $name, bool $network = false ) { $key = $this->key( $name, $network ); - return array_key_exists( $key, $this->records ) ? $this->records[ $key ] : null; + return $this->records[ $key ] ?? null; } public function set( string $name, array $record, bool $network = false ) { diff --git a/tests/phpunit/tests/admin/includes/plugin/pluginPausedState.php b/tests/phpunit/tests/admin/includes/plugin/pluginPausedState.php index 89782b80be4d4..9523ee367b236 100644 --- a/tests/phpunit/tests/admin/includes/plugin/pluginPausedState.php +++ b/tests/phpunit/tests/admin/includes/plugin/pluginPausedState.php @@ -40,7 +40,7 @@ public function set_up() { require_once ABSPATH . 'wp-admin/includes/plugin.php'; - $this->orig_paused_plugins = isset( $GLOBALS['_paused_plugins'] ) ? $GLOBALS['_paused_plugins'] : null; + $this->orig_paused_plugins = $GLOBALS['_paused_plugins'] ?? null; $GLOBALS['_paused_plugins'] = array(); diff --git a/tests/phpunit/tests/ai-client/wpAiClientEmbeddingBuilder.php b/tests/phpunit/tests/ai-client/wpAiClientEmbeddingBuilder.php index 8cd40d36620ef..d4a80723f111d 100644 --- a/tests/phpunit/tests/ai-client/wpAiClientEmbeddingBuilder.php +++ b/tests/phpunit/tests/ai-client/wpAiClientEmbeddingBuilder.php @@ -157,7 +157,7 @@ private function create_mock_embedding_model( $result, ?ModelMetadata $metadata = null ): ModelInterface { - $metadata = $metadata ?? $this->create_test_embedding_model_metadata(); + $metadata ??= $this->create_test_embedding_model_metadata(); $provider_metadata = new ProviderMetadata( 'mock', diff --git a/tests/phpunit/tests/dependencies/wpPrefetchAdminAssets.php b/tests/phpunit/tests/dependencies/wpPrefetchAdminAssets.php index f8927c8d3aa88..8659033685ef4 100644 --- a/tests/phpunit/tests/dependencies/wpPrefetchAdminAssets.php +++ b/tests/phpunit/tests/dependencies/wpPrefetchAdminAssets.php @@ -656,10 +656,10 @@ public function test_login_skips_scripts_printed_in_footer(): void { if ( 'SCRIPT' === $processor->get_tag() ) { $src = $processor->get_attribute( 'src' ); if ( is_string( $src ) && in_array( basename( (string) wp_parse_url( $src, PHP_URL_PATH ) ), array( 'jquery.js', 'jquery.min.js' ), true ) ) { - $jquery_index = $jquery_index ?? $tag_index; + $jquery_index ??= $tag_index; } } elseif ( 'LINK' === $processor->get_tag() && 'prefetch' === $processor->get_attribute( 'rel' ) ) { - $first_prefetch_index = $first_prefetch_index ?? $tag_index; + $first_prefetch_index ??= $tag_index; } } diff --git a/tests/phpunit/tests/error-protection/wpRecoveryModeLinkService.php b/tests/phpunit/tests/error-protection/wpRecoveryModeLinkService.php index fbfc98ee2bf07..583b4421a2f9e 100644 --- a/tests/phpunit/tests/error-protection/wpRecoveryModeLinkService.php +++ b/tests/phpunit/tests/error-protection/wpRecoveryModeLinkService.php @@ -50,7 +50,7 @@ class Tests_Error_Protection_wpRecoveryModeLinkService extends WP_UnitTestCase { public function set_up() { parent::set_up(); - $this->orig_pagenow = isset( $GLOBALS['pagenow'] ) ? $GLOBALS['pagenow'] : null; + $this->orig_pagenow = $GLOBALS['pagenow'] ?? null; $this->orig_get = $_GET; $_GET = array(); diff --git a/tests/phpunit/tests/link/getSearchLink.php b/tests/phpunit/tests/link/getSearchLink.php index 59bb427a5bd5b..0beac34b4e42b 100644 --- a/tests/phpunit/tests/link/getSearchLink.php +++ b/tests/phpunit/tests/link/getSearchLink.php @@ -17,7 +17,7 @@ class Tests_Link_GetSearchLink extends WP_UnitTestCase { public static function set_up_before_class() { parent::set_up_before_class(); - self::$original_search_query = isset( $GLOBALS['wp_query']->query_vars['s'] ) ? $GLOBALS['wp_query']->query_vars['s'] : ''; + self::$original_search_query = $GLOBALS['wp_query']->query_vars['s'] ?? ''; } public function tear_down() { diff --git a/tests/phpunit/tests/load/wpGetServerProtocol.php b/tests/phpunit/tests/load/wpGetServerProtocol.php index 681efed60fa50..3d7f18f241d67 100644 --- a/tests/phpunit/tests/load/wpGetServerProtocol.php +++ b/tests/phpunit/tests/load/wpGetServerProtocol.php @@ -19,7 +19,7 @@ class Tests_Load_WpGetServerProtocol extends WP_UnitTestCase { public function set_up() { parent::set_up(); - $this->original_server_protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : null; + $this->original_server_protocol = $_SERVER['SERVER_PROTOCOL'] ?? null; } public function tear_down() { diff --git a/tests/phpunit/tests/load/wpIsJsonRequest.php b/tests/phpunit/tests/load/wpIsJsonRequest.php index ed0140ad36152..c138a7ca9d1e3 100644 --- a/tests/phpunit/tests/load/wpIsJsonRequest.php +++ b/tests/phpunit/tests/load/wpIsJsonRequest.php @@ -26,8 +26,8 @@ class Tests_Load_WpIsJsonRequest extends WP_UnitTestCase { public function set_up() { parent::set_up(); - $this->original_http_accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? $_SERVER['HTTP_ACCEPT'] : null; - $this->original_content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : null; + $this->original_http_accept = $_SERVER['HTTP_ACCEPT'] ?? null; + $this->original_content_type = $_SERVER['CONTENT_TYPE'] ?? null; } public function tear_down() { diff --git a/tests/phpunit/tests/load/wpIsMaintenanceMode.php b/tests/phpunit/tests/load/wpIsMaintenanceMode.php index 88b9516bbcc6b..ab63300e0866f 100644 --- a/tests/phpunit/tests/load/wpIsMaintenanceMode.php +++ b/tests/phpunit/tests/load/wpIsMaintenanceMode.php @@ -37,7 +37,7 @@ public function set_up() { $this->original_installing = wp_installing(); foreach ( array( 'wp_scrape_key', 'wp_scrape_nonce' ) as $key ) { - $this->original_scrape_request[ $key ] = array_key_exists( $key, $_REQUEST ) ? $_REQUEST[ $key ] : null; + $this->original_scrape_request[ $key ] = $_REQUEST[ $key ] ?? null; } $this->delete_maintenance_file(); diff --git a/tests/phpunit/tests/load/wpIsXmlRequest.php b/tests/phpunit/tests/load/wpIsXmlRequest.php index 3cf35bff2bd6f..2a59de544e9c5 100644 --- a/tests/phpunit/tests/load/wpIsXmlRequest.php +++ b/tests/phpunit/tests/load/wpIsXmlRequest.php @@ -26,8 +26,8 @@ class Tests_Load_WpIsXmlRequest extends WP_UnitTestCase { public function set_up() { parent::set_up(); - $this->original_http_accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? $_SERVER['HTTP_ACCEPT'] : null; - $this->original_content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : null; + $this->original_http_accept = $_SERVER['HTTP_ACCEPT'] ?? null; + $this->original_content_type = $_SERVER['CONTENT_TYPE'] ?? null; } public function tear_down() { diff --git a/tests/phpunit/tests/load/wpStartScrapingEditedFileErrors.php b/tests/phpunit/tests/load/wpStartScrapingEditedFileErrors.php index 91fdb5b52249c..18b437e1c2abd 100644 --- a/tests/phpunit/tests/load/wpStartScrapingEditedFileErrors.php +++ b/tests/phpunit/tests/load/wpStartScrapingEditedFileErrors.php @@ -20,7 +20,7 @@ public function set_up() { parent::set_up(); foreach ( array( 'wp_scrape_key', 'wp_scrape_nonce' ) as $key ) { - $this->original_request[ $key ] = array_key_exists( $key, $_REQUEST ) ? $_REQUEST[ $key ] : null; + $this->original_request[ $key ] = $_REQUEST[ $key ] ?? null; unset( $_REQUEST[ $key ] ); } }