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] do not needlessly pass as reference, enforce valid param #19478

Merged
merged 1 commit into from
Feb 23, 2021

Conversation

eileenmcnaughton
Copy link
Contributor

Overview

[REF] do not needlessly pass as reference, enforce valid param

Before

Unnecessary pass-by-reference, array type not enforced

After

pass by ref removed, strict typing on array, simplifed return structure (do not use elseif after a return)

Technical Details

Quietly skipping an invalid param feels like a recipe for weirdness

Comments

This function isn't that heavily used & I couldn't find any cases of calling functions doing anything weird - it would fail hard & fast if there were an issue - probably during tests

@civibot
Copy link

civibot bot commented Feb 1, 2021

(Standard links)

@civibot civibot bot added the master label Feb 1, 2021
@eileenmcnaughton
Copy link
Contributor Author

@colemanw are you OK to merge this?

@colemanw
Copy link
Member

@eileenmcnaughton I'd be happier if the function had a unit test :)

@eileenmcnaughton
Copy link
Contributor Author

@colemanw I guess our tests go through this

@colemanw colemanw merged commit 8d3f19d into civicrm:master Feb 23, 2021
@colemanw colemanw deleted the ref branch February 23, 2021 22:24
@colemanw
Copy link
Member

Ok

@demeritcowboy
Copy link
Contributor

demeritcowboy commented Feb 24, 2021

This is causing all the PR's to fail tests (CRM_Core_BAO_MessageTemplateTest::testContactTokens). An easy way to see what's happening is to run cv ev "$x = array('foo' => '0'); var_dump(CRM_Utils_Array::retrieveValueRecursive($x, 'foo'))"

BEFORE: NULL
AFTER: '0'

The second is more correct, and for tokens used in smarty if people are doing something like branching based on that value then typically it would evaluate the same either way since most people write smarty as {if $thing} which evaluates '0' as false. And to use a token value in such a way you'd have to {capture} it which flattens the typing anyway.

So the question is the other places this function is used that isn't covered by tests and whether there'd be a difference there.

@demeritcowboy
Copy link
Contributor

demeritcowboy commented Feb 24, 2021

It's not used much outside tokens. Maybe here is the only place it would make a difference, if you're importing blanks instead of '0's, but it might be an improvement, i.e. before blanks might overwrite (haven't tested):

$getValue = CRM_Utils_Array::retrieveValueRecursive($contact, $key);
}
if ($key == 'contact_source') {
$params['source'] = $params[$key];
unset($params[$key]);
}
if ($modeFill && isset($getValue)) {

@eileenmcnaughton
Copy link
Contributor Author

Oh - the green light was BEFORE I added that test & it was when I was working on that code that I found the function so confusing I guess

@demeritcowboy
Copy link
Contributor

So I am seeing a difference with import in fill mode before and after the patch. Suppose you have a contact that does not have DO NOT PHONE checked.

BEFORE: Importing a "1" in the import file checks the DO NOT PHONE setting as expected.
AFTER: Importing a "1" does NOT check the setting.

The difference seems to be that before the patch it treats the existing value as NULL and so goes ahead and imports it. After the patch it treats the existing value as "0" and so according to fill mode it shouldn't import it.

Blanks and "0" behave the same before and after the patch when importing against a contact the DOES have the setting checked, but then that's expected if the effect is on how it treats the existing value.

elseif ($value = CRM_Utils_Array::value($key, $params)) {
return $value;
public static function retrieveValueRecursive(array $params, string $key) {
if (isset($params[$key])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@demeritcowboy so we need to change this to if (!empty() ) to get no change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick run says yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - I'll put that up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eileenmcnaughton added a commit to eileenmcnaughton/civicrm-core that referenced this pull request Feb 24, 2021
It seems the tests ran before the test that covered this
was merged giving a false positive

See civicrm#19478 (comment) for rationale
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