Skip to content

Commit 16bdb6d

Browse files
Tests: Use the null coalescing operators in the test suite.
Replaces `isset()` and `array_key_exists()` ternaries that return the tested value or a fallback of `null` or `''` with the null coalescing operator (`??`), and self-referencing `$x = $x ?? $y` assignments with the null coalescing assignment operator (`??=`). **Why this is safe** - `??` and `isset()` test the same condition, so the fallback is used in exactly the same cases. - `array_key_exists()` and `??` only differ for a key that exists with a `null` value. Every converted `array_key_exists()` site falls back to `null`, so both forms return `null` in that case as well. - `??=` requires PHP 7.4, which is already the minimum supported version. - No behavior change; this is test-only code. Developed in #14616. Follow-up to r63609, r63633. Props Soean, huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64675 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 92c1741 commit 16bdb6d

11 files changed

Lines changed: 14 additions & 14 deletions

File tree

‎tests/phpunit/includes/secrets/class-mock-store.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function get( string $name, bool $network = false ) {
4040

4141
$key = $this->key( $name, $network );
4242

43-
return array_key_exists( $key, $this->records ) ? $this->records[ $key ] : null;
43+
return $this->records[ $key ] ?? null;
4444
}
4545

4646
public function set( string $name, array $record, bool $network = false ) {

‎tests/phpunit/tests/admin/includes/plugin/pluginPausedState.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function set_up() {
4040

4141
require_once ABSPATH . 'wp-admin/includes/plugin.php';
4242

43-
$this->orig_paused_plugins = isset( $GLOBALS['_paused_plugins'] ) ? $GLOBALS['_paused_plugins'] : null;
43+
$this->orig_paused_plugins = $GLOBALS['_paused_plugins'] ?? null;
4444

4545
$GLOBALS['_paused_plugins'] = array();
4646

‎tests/phpunit/tests/ai-client/wpAiClientEmbeddingBuilder.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private function create_mock_embedding_model(
157157
$result,
158158
?ModelMetadata $metadata = null
159159
): ModelInterface {
160-
$metadata = $metadata ?? $this->create_test_embedding_model_metadata();
160+
$metadata ??= $this->create_test_embedding_model_metadata();
161161

162162
$provider_metadata = new ProviderMetadata(
163163
'mock',

‎tests/phpunit/tests/dependencies/wpPrefetchAdminAssets.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,10 @@ public function test_login_skips_scripts_printed_in_footer(): void {
656656
if ( 'SCRIPT' === $processor->get_tag() ) {
657657
$src = $processor->get_attribute( 'src' );
658658
if ( is_string( $src ) && in_array( basename( (string) wp_parse_url( $src, PHP_URL_PATH ) ), array( 'jquery.js', 'jquery.min.js' ), true ) ) {
659-
$jquery_index = $jquery_index ?? $tag_index;
659+
$jquery_index ??= $tag_index;
660660
}
661661
} elseif ( 'LINK' === $processor->get_tag() && 'prefetch' === $processor->get_attribute( 'rel' ) ) {
662-
$first_prefetch_index = $first_prefetch_index ?? $tag_index;
662+
$first_prefetch_index ??= $tag_index;
663663
}
664664
}
665665

‎tests/phpunit/tests/error-protection/wpRecoveryModeLinkService.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Tests_Error_Protection_wpRecoveryModeLinkService extends WP_UnitTestCase {
5050
public function set_up() {
5151
parent::set_up();
5252

53-
$this->orig_pagenow = isset( $GLOBALS['pagenow'] ) ? $GLOBALS['pagenow'] : null;
53+
$this->orig_pagenow = $GLOBALS['pagenow'] ?? null;
5454
$this->orig_get = $_GET;
5555
$_GET = array();
5656

‎tests/phpunit/tests/link/getSearchLink.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Tests_Link_GetSearchLink extends WP_UnitTestCase {
1717

1818
public static function set_up_before_class() {
1919
parent::set_up_before_class();
20-
self::$original_search_query = isset( $GLOBALS['wp_query']->query_vars['s'] ) ? $GLOBALS['wp_query']->query_vars['s'] : '';
20+
self::$original_search_query = $GLOBALS['wp_query']->query_vars['s'] ?? '';
2121
}
2222

2323
public function tear_down() {

‎tests/phpunit/tests/load/wpGetServerProtocol.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Tests_Load_WpGetServerProtocol extends WP_UnitTestCase {
1919
public function set_up() {
2020
parent::set_up();
2121

22-
$this->original_server_protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : null;
22+
$this->original_server_protocol = $_SERVER['SERVER_PROTOCOL'] ?? null;
2323
}
2424

2525
public function tear_down() {

‎tests/phpunit/tests/load/wpIsJsonRequest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class Tests_Load_WpIsJsonRequest extends WP_UnitTestCase {
2626
public function set_up() {
2727
parent::set_up();
2828

29-
$this->original_http_accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? $_SERVER['HTTP_ACCEPT'] : null;
30-
$this->original_content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : null;
29+
$this->original_http_accept = $_SERVER['HTTP_ACCEPT'] ?? null;
30+
$this->original_content_type = $_SERVER['CONTENT_TYPE'] ?? null;
3131
}
3232

3333
public function tear_down() {

‎tests/phpunit/tests/load/wpIsMaintenanceMode.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function set_up() {
3737
$this->original_installing = wp_installing();
3838

3939
foreach ( array( 'wp_scrape_key', 'wp_scrape_nonce' ) as $key ) {
40-
$this->original_scrape_request[ $key ] = array_key_exists( $key, $_REQUEST ) ? $_REQUEST[ $key ] : null;
40+
$this->original_scrape_request[ $key ] = $_REQUEST[ $key ] ?? null;
4141
}
4242

4343
$this->delete_maintenance_file();

‎tests/phpunit/tests/load/wpIsXmlRequest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class Tests_Load_WpIsXmlRequest extends WP_UnitTestCase {
2626
public function set_up() {
2727
parent::set_up();
2828

29-
$this->original_http_accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? $_SERVER['HTTP_ACCEPT'] : null;
30-
$this->original_content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : null;
29+
$this->original_http_accept = $_SERVER['HTTP_ACCEPT'] ?? null;
30+
$this->original_content_type = $_SERVER['CONTENT_TYPE'] ?? null;
3131
}
3232

3333
public function tear_down() {

0 commit comments

Comments
 (0)