Skip to content

Tests: Use the null coalescing operators in the test suite - #14616

Open
Soean wants to merge 1 commit into
WordPress:trunkfrom
Soean:tests/null-coalescing-operators
Open

Soean wants to merge 1 commit into
WordPress:trunkfrom
Soean:tests/null-coalescing-operators

Conversation

@Soean

@Soean Soean commented Oct 21, 2026

Copy link
Copy Markdown
Member

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.

Follow-up to r63609, r63633.

Trac ticket: https://core.trac.wordpress.org/ticket/65819


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

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.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props soean.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The test-only replacements preserve fallback and initialization behavior and are compatible with the PHP 7.4 minimum.

No open issues 🎉

What changed in this PR

Modernizes WordPress test code with null coalescing operators while preserving existing behavior.

Changes:

  • Replaces value-or-fallback ternaries with ??.
  • Replaces self-referencing initialization assignments with ??=.
File Description
tests/​phpunit/​tests/​load/​wpStartScrapingEditedFileErrors.php Simplifies request-state capture.
tests/​phpunit/​tests/​load/​wpIsXmlRequest.php Simplifies header-state capture.
tests/​phpunit/​tests/​load/​wpIsMaintenanceMode.php Simplifies scrape-request capture.
tests/​phpunit/​tests/​load/​wpIsJsonRequest.php Simplifies header-state capture.
tests/​phpunit/​tests/​load/​wpGetServerProtocol.php Simplifies protocol-state capture.
tests/​phpunit/​tests/​link/​getSearchLink.php Simplifies search-query fallback.
tests/​phpunit/​tests/​error-protection/​wpRecoveryModeLinkService.php Simplifies page-state capture.
tests/​phpunit/​tests/​dependencies/​wpPrefetchAdminAssets.php Simplifies first-match index assignments.
tests/​phpunit/​tests/​ai-client/​wpAiClientEmbeddingBuilder.php Simplifies default metadata initialization.
tests/​phpunit/​tests/​admin/​includes/​plugin/​pluginPausedState.php Simplifies paused-plugin state capture.
tests/​phpunit/​includes/​secrets/​class-mock-store.php Simplifies record lookup fallback.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants