[wp-trac] [WordPress Trac] #58874: Code Modernization: Consider using the null coalescing operator.

WordPress Trac noreply at wordpress.org
Sat Jul 22 12:28:49 UTC 2023


#58874: Code Modernization: Consider using the null coalescing operator.
-----------------------------------+---------------------
 Reporter:  costdev                |       Owner:  (none)
     Type:  enhancement            |      Status:  new
 Priority:  normal                 |   Milestone:  6.4
Component:  General                |     Version:
 Severity:  normal                 |  Resolution:
 Keywords:  has-patch 2nd-opinion  |     Focuses:
-----------------------------------+---------------------

Comment (by jrf):

 I agree with @swissspidy that this should be discussed on Make first,
 probably as part of a larger Make post about the PHP 7.0 syntaxes now
 available.

 I also wonder why this should be changed for existing code (as it make
 backports harder). What about just ''allowing it'' for ''new'' code and
 for code ''which is being patched anyway'' ?
 That way, the impact on backports will be much more limited, while we can
 still use the new shiny in code actually being worked on. 😁

 Other considerations for this ticket:
 * Should this also be changed/applied when the original ternary is used as
 a parameter for a function call ?
 * Should this also be changed/applied when the original ternary is used as
 part of a condition in a control structure ?

 For the first I imagine it may impact readability more in a negative way,
 for the second I see a risk of issues with operator precedence.

 And while we are discussing this, WP, more often than not, uses a slightly
 different pattern for the same:
 {{{#!php
 <?php
 $var = 'default';
 if ( isset( $bar ) ) {
     $var = $bar;
 }
 }}}

 ...which could be re-written to:
 {{{#!php
 <?php
 $var = $bar ?? 'default';
 }}}

 I don't see any mention of that pattern in this ticket, while I expect
 that to be far more common.

 Changing the code "naturally" (as in update code which is being worked on
 when it is being worked on) would be more likely to also catch that
 pattern and update it.

 Note: I'm not against this change or against using the null coalesce
 operator, I just think we need to think this through a bit more carefully.

 > Also means updates to WPCS are required

 @swissspidy Good point. As far as I can see, WPCS (3.0.0) should be fine
 as the null coalesce operator is already taken into account in various
 places and where it wasn't, we've made some updates already for this to be
 included in WPCS 3.0.0.
 We may, of course, have overlooked some situations in which it should be
 taken into account, in which case, please submit a bug report.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58874#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list