[REF] Swap CRM_Utils_Array::value for ?? #15003
Closed
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.
Overview
PHP 7 finally has a built-in language construct that does basically the same thing as our homebrew function. Swapping the two is more efficient and easier to read.
Before
Less efficient:
CRM_Utils_Array::value($key, $array, $default)
After
More efficient:
$array[$key] ?? $default
Notes
I did this with a regex but then manually checked the results and tweaked some of them that were particularly weird or that could be further simplified (for example within an
if()
statement the whole thing could be swapped for!empty()
.Technical Details
The difference in functionality between these two constructs is negligible. Technically there is one difference: if the item exists in the array but has a value of
null
, thenCRM_Utils_Array::value()
would returnnull
whereas ?? would return the default. However I think it's unlikely any developer was relying on this quirk.