Repository navigation
Code Modernization: Use the null coalescing assignment operator in comment.php - #16004
noruzzamans wants to merge 1 commit into
Conversation
…mment.php Replaces verbose `! isset()` guard blocks that assign default values in `get_comment_pages_count()` and `wp_new_comment()` with the null coalescing assignment operator (`??=`) in `src/wp-includes/comment.php`: - `$per_page ??= (int) get_query_var( 'comments_per_page' );` - `$threaded ??= get_option( 'thread_comments' );` - `$commentdata['comment_author_IP'] ??= $_SERVER['REMOTE_ADDR'];` - `$commentdata['comment_agent'] ??= $_SERVER['HTTP_USER_AGENT'] ?? '';` **Why this is safe** - `??=` requires PHP 7.4, which is the minimum supported version for WordPress Core. - `??=` and `! isset()` evaluate the exact same condition, applying the default only when the variable or key is null or unset. - Assignment order is preserved. - No behavioral changes; purely internal code simplification. Props noruzzaman. See #65823.
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the comment-default assignment simplification.
No open issues 🎉
What changed in this PR
Modernizes default assignments in WordPress Core’s comment handling by replacing four ! isset() guard blocks with ??=, preserving existing behavior.
Changes:
- Simplifies pagination and threading defaults in
get_comment_pages_count(). - Simplifies author IP and user agent defaults in
wp_new_comment().
| File | Description |
|---|---|
src/wp-includes/comment.php |
Replaces four default-assignment guards with null coalescing assignments. |
Review effort: Balanced
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Description
Replaces verbose
! isset()guard blocks that assign default values with the null coalescing assignment operator (??=) insrc/wp-includes/comment.php:Scope
This covers
get_comment_pages_count()andwp_new_comment()insrc/wp-includes/comment.phpcompletely. All changed guards are simple default assignments to tested variables/keys, reducing boilerplate while keeping the function logic identical. 4 guard blocks replaced, 13 lines removed for 4 added (-9 net lines).Why this is safe
??=requires PHP 7.4, which is the minimum supported version for WordPress Core.??=and! isset()test the exact same condition, so default values are applied in identical scenarios.$_SERVER['HTTP_USER_AGENT'] ?? '') is naturally complemented.Trac ticket: https://core.trac.wordpress.org/ticket/65823