#63430 closed enhancement (fixed)
Coding Standards: replace isset() ternary with null coalescing
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.0 | Priority: | normal |
| Severity: | minor | Version: | |
| Component: | General | Keywords: | has-patch has-unit-tests |
| Focuses: | coding-standards | Cc: |
Description
When reviewing the source code, I noticed some ternary expressions of the form isset( $var ) ? $var : null.
Since PHP 7.0 introduced the null coalescing operator, and WordPress now requires at least PHP 7.2.24, we can safely replace these ternaries with the more concise $var ?? null syntax.
To ensure the changeset is correct, I'd prefer break the changes to bulk of files instead of fixing entire project in one PR.
I've changed manually with the help with IDE, and verify that the meaning of code is exactly the same.
Change History (39)
This ticket was mentioned in PR #8791 on WordPress/wordpress-develop by @seanwei.
9 months ago
#1
- Keywords has-patch added
@getsyash commented on PR #8791:
9 months ago
#2
Updating isset( $var ) ? $var : null to the null coalescing operator ($var ?? null) definitely makes the code cleaner and more modern, especially now that WordPress supports PHP 7.2.24 and above.
A few thoughts:
- It would be helpful to include test coverage (if applicable) or indicate if existing tests already verify these code paths, just to be safe.
- Consider checking for any edge cases where isset() might have side effects (e.g. when used on arrays or in contexts with indirect references), though in most cases this replacement should be safe.
#4
@
9 months ago
Hi there, welcome to WordPress Trac!
Thanks for the ticket, just noting that this was previously also suggested and discussed in #58874.
@krupalpanchal commented on PR #8791:
9 months ago
#5
Looks good! But, is this PR contains all isset() from the whole the WordPress code? Have to check this.
This ticket was mentioned in Slack in #core by sean_wei. View the logs.
8 months ago
This ticket was mentioned in Slack in #core by wildworks. View the logs.
4 months ago
#9
@
4 months ago
This ticket was featured on today's 6.9 Bug Scrub.
It looks like there are a huge number of areas that need to be updated. Try searching using the following regular expression in your code editor.
isset\(\s*([^)]+?)\s*\)\s*\?\s*\1\s*:\s*([^\n;]+)
In my environment, 701 items and 208 files were found.
Updating all of these in a single PR may be a bit risky, so we may want to consider how to split the work up.
#10
@
4 months ago
- Milestone changed from 6.9 to Future Release
Punting to Future Release due to the impending 6.9 beta1 and lack of recent progress.
I would also question the value of doing this in general and how it aligns with the core stance on refactoring
#12
@
7 weeks ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from new to closed
In 61403:
This ticket was mentioned in PR #10654 on WordPress/wordpress-develop by @westonruter.
7 weeks ago
#13
- Keywords has-unit-tests added
This is a follow-up up to r61403.
I used PHPStorm's regex search and replace with the pattern isset\(\s*(.+?)\s*\)\s*\?\s*\1(\s*): and the replacement $1$2??.
Trac ticket: https://core.trac.wordpress.org/ticket/63430
#14
@
7 weeks ago
Now that [61403] has been committed to fix #63430, which is a subset of what this ticket is all about, I've followed up with PR 10654. I did a regex replacement to update the remaining instances of
isset()ternaries to use the null coalescing operator as well. So it's basically it's a refresh of PR 4886. If there aren't any objections, I'll commit.
#16
@
5 weeks ago
- Resolution fixed deleted
- Status changed from closed to reopened
Re-opening because there are still many instances of the ternary which can be replaced, although this is also more-or-less a duplicate of #58874.
This ticket was mentioned in PR #10704 on WordPress/wordpress-develop by @Soean.
4 weeks ago
#35
Trac ticket: https://core.trac.wordpress.org/ticket/63430
#36
@
4 weeks ago
@westonruter I found some remaining isset() that can be replaced by ??: https://github.com/WordPress/wordpress-develop/pull/10704
@westonruter commented on PR #10704:
4 weeks ago
#39
Committed in r61464.
When reviewing the source code, I noticed some ternary expressions of the form
isset( $var ) ? $var : null.Since PHP 7.0 introduced the (null coalescing operator)https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op, and WordPress now requires at least PHP 7.2.24, we can safely replace these ternaries with the more concise
$var ?? nullsyntax.To ensure the changeset is correct, I'd prefer break the changes to bulk of files instead of fixing entire project in one PR.
I've changed manually with the help with IDE, and verify that the meaning of code is exactly the same.
Trac ticket: https://core.trac.wordpress.org/ticket/63430