Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Swap CRM_Utils_Array::value for ?? #15003

Closed
wants to merge 1 commit into from

Conversation

colemanw
Copy link
Member

@colemanw colemanw commented Aug 9, 2019

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, then CRM_Utils_Array::value() would return null whereas ?? would return the default. However I think it's unlikely any developer was relying on this quirk.

@civibot
Copy link

civibot bot commented Aug 9, 2019

(Standard links)

@civibot civibot bot added the master label Aug 9, 2019
PHP 7 finally has a built-in language construct that does basically the same thing as this homebrew function.
@colemanw
Copy link
Member Author

colemanw commented Aug 9, 2019

@eileenmcnaughton 750 sloc is quite a large diff to read, but I actually did go through it all to check for problems (and did find a few that I corrected). Note this PR will go stale quickly.

@demeritcowboy
Copy link
Contributor

@colemanw I'm totally for killing this function, but it seems like there'd be a difference if the originally passed second parameter is not actually an array? Maybe you already weeded those out?

@eileenmcnaughton
Copy link
Contributor

@colemanw this feels high risk - am happy to deal with it in chunks but the chance of me missing a mistake in this many changes gives me pause

@colemanw
Copy link
Member Author

colemanw commented Aug 9, 2019

Ok. Test failures show that I missed a few spots and I agree this is too much for one PR. Will close and chunk it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants