Repository navigation
Conversation
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.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
🟢 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces
isset()andarray_key_exists()ternaries that return the tested value or a fallback ofnullor''with the null coalescing operator (??), and self-referencing$x = $x ?? $yassignments with the null coalescing assignment operator (??=).Why this is safe
??andisset()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 anullvalue. Every convertedarray_key_exists()site falls back tonull, so both forms returnnullin that case as well.??=requires PHP 7.4, which is already the minimum supported version.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.