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

Swap CRM_Utils_Array::value for empty() in conditionals #15005

Merged
merged 1 commit into from
Aug 12, 2019

Conversation

colemanw
Copy link
Member

@colemanw colemanw commented Aug 9, 2019

Overview

When the output of CRM_Utils_Array::value is immediately cast to boolean by a conditional, then it's better to use empty() as it's faster & more readable.
If the output is being compared to NULL in the conditional then it's better to use isset().

Technical Details

The before/after statements are identical in every scenario I can think of.

Comments

I did a mass regex replace of these 5 years ago with no ill-effects, this is just catching some stragglers that didn't get converted with that first pass.

@civibot
Copy link

civibot bot commented Aug 9, 2019

(Standard links)

@civibot civibot bot added the master label Aug 9, 2019
if ($startOffset == NULL && CRM_Utils_Array::value('height_participant_image', $formattedRow)) {
$startOffset = CRM_Utils_Array::value('height_participant_image', $formattedRow);
if ($startOffset == NULL && !empty($formattedRow['height_participant_image'])) {
$startOffset = $formattedRow['height_participant_image'];
Copy link
Member Author

Choose a reason for hiding this comment

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

This was silly as the conditional already established it was not empty

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean earlier in the formatLabel function? Anyway this seems ok but makes spidey-sense tingle for me for a different reason - out of scope here but for reference I don't see how $startOffset could be NULL, and even if it could be then adding it to $y in line 227 is iffy even if it works.

@colemanw
Copy link
Member Author

colemanw commented Aug 9, 2019

@eileenmcnaughton @demeritcowboy this is the simplest & safest chunk of #15003

@eileenmcnaughton
Copy link
Contributor

test this please

@@ -143,7 +143,7 @@ public static function formRule($values, $files, $self) {
if ($values['financial_account_type_id'] != $financialAccountTypeId) {
$errorMsg['financial_account_type_id'] = ts('Taxable accounts should have Financial Account Type set to Liability.');
}
if (CRM_Utils_Array::value('tax_rate', $values) == NULL) {
if (!isset($values['tax_rate'])) {
$errorMsg['tax_rate'] = ts('Please enter value for tax rate');
Copy link
Contributor

Choose a reason for hiding this comment

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

I started at the bottom and have looked upward to about here. Strictly speaking these two lines aren't identical, it just happens to work out because of the context, and actually the original is probably flawed. If $values['tax_rate'] is integer 0 instead of string 0 then the original is true but the replacement is false. However because it's a form value it's a string, and you do want it to evaluate to false here because 0 is an allowed value. So both statements work in context here but similar ones elsewhere might need to take into account whether it could be integer 0 or string 0.

@@ -132,7 +132,7 @@ function civicrm_api3_payment_cancel($params) {
*/
function civicrm_api3_payment_create($params) {
// Check if it is an update
if (CRM_Utils_Array::value('id', $params)) {
if (!empty($params['id'])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ok

@eileenmcnaughton
Copy link
Contributor

@demeritcowboy how many of these have you checked? There are 75 in this PR I think - I have checked 1 :-)

@demeritcowboy
Copy link
Contributor

I started at the bottom and went up to the null one in financial. I also started to look at the pledge_installment null one. So maybe if you go downwards and I go upwards?

@eileenmcnaughton
Copy link
Contributor

OK - I hate having so many in a PR - 10 would be about my limit - I'll do another 5 now & then come back to it later

@@ -312,9 +312,9 @@ public static function create(&$params) {
}

// Set priority to Normal for Auto-populated activities (for Cases)
if (CRM_Utils_Array::value('priority_id', $params) === NULL &&
if (!isset($params['priority_id']) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

OK - it's not the same but I guess priority_id can't be 0....

Copy link
Member Author

Choose a reason for hiding this comment

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

How is this not the same @eileenmcnaughton? The default return value of CRM_Utils_Array::value is NULL if the variable isn't set, and isset($foo = NULL) returns FALSE.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right - it could be set & equal to NULL or not set - I think here it doesn't matter in that set & equal to null would still be empty

// if not set and not 0
!CRM_Utils_Array::value('id', $params)
empty($params['id'])
Copy link
Contributor

Choose a reason for hiding this comment

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

ok

@@ -205,7 +205,7 @@ public function postProcess() {
'version' => 3,
'key' => $this->_key,
]);
if (!CRM_Utils_Array::value('is_error', $result, FALSE)) {
if (empty($result['is_error'])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ok

@@ -523,7 +523,7 @@ public function parseActionSchedule($values) {
'end_date',
];

if (!CRM_Utils_Array::value('absolute_date', $params)) {
if (empty($params['absolute_date'])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ok

@@ -577,7 +577,7 @@ public function parseActionSchedule($values) {

$params['is_active'] = CRM_Utils_Array::value('is_active', $values, 0);

if (CRM_Utils_Array::value('is_repeat', $values) == 0) {
if (empty($values['is_repeat'])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ok - I guess is_repeat is a boolean - although that sort of thing always feels like a big assumption with our code

Copy link
Member Author

Choose a reason for hiding this comment

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

Well since this statement uses loose == and not strict ===, in this case the two statements are equivalent.

@@ -14,7 +14,7 @@ class CRM_Core_Page_AJAX_RecurringEntity {

public static function updateMode() {
$finalResult = [];
if (CRM_Utils_Array::value('mode', $_REQUEST) && CRM_Utils_Array::value('entityId', $_REQUEST) && CRM_Utils_Array::value('entityTable', $_REQUEST)) {
if (CRM_Utils_Array::value('mode', $_REQUEST) && CRM_Utils_Array::value('entityId', $_REQUEST) && !empty($_REQUEST['entityTable'])) {
$mode = CRM_Utils_Type::escape($_REQUEST['mode'], 'Integer');
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it's just because of the regex used to find these, but is there a reason only the last instance on the line was converted?

Copy link
Member Author

Choose a reason for hiding this comment

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

Applied a slightly different regex to fix this and a couple other spots like it.

@@ -917,7 +917,7 @@ public function mapFormValuesToDB($formParams = []) {
}
}
if ($formParams['repeats_by'] == 2) {
if (CRM_Utils_Array::value('entity_status_1', $formParams) && CRM_Utils_Array::value('entity_status_2', $formParams)) {
if (CRM_Utils_Array::value('entity_status_1', $formParams) && !empty($formParams['entity_status_2'])) {
$dbParams['entity_status'] = $formParams['entity_status_1'] . " " . $formParams['entity_status_2'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Another instance where only the last one on the line was converted.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it.

@@ -915,13 +915,13 @@ public static function formRule($fields, $files, $self) {
$errors['pledge_installments'] = ts('Please enter a valid number of pledge installments.');
}
else {
if (CRM_Utils_Array::value('pledge_installments', $fields) == NULL) {
if (!isset($fields['pledge_installments'])) {
$errors['pledge_installments'] = ts('Pledge Installments is required field.');
Copy link
Contributor

Choose a reason for hiding this comment

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

This is ok. I r-run'd to check. Note that CRM_Utils_Rule::positiveInteger() above lets 0 thru (both int and string), and then as a string it gets thru here both originally and with the replacement, and then it gets caught down below by both the original and replacement on line 924.

@demeritcowboy
Copy link
Contributor

Status: I'm up to CRM/Contribute/Form/Contribution/Main.php coming from the bottom.

@demeritcowboy
Copy link
Contributor

I think we've looked at them all now unless the recent push added some new lines that weren't there before? If not then I don't have anything more to add.

@eileenmcnaughton
Copy link
Contributor

Ok - merging based on @demeritcowboy having gone through them (& me having looked at a smaller number)

@eileenmcnaughton eileenmcnaughton merged commit e60c6c5 into civicrm:master Aug 12, 2019
@colemanw colemanw deleted the empty branch August 12, 2019 21:04
@colemanw
Copy link
Member Author

Thanks for the merge/review @demeritcowboy & @eileenmcnaughton.
I'm not sure how to go further given our review capacity because my other regexes all make thousands of replacements.

@eileenmcnaughton
Copy link
Contributor

@colemanw we've split other high-risk cleanups like this over multiple releases - ie. we removed a handful of the LOWER & dao->free() instances each release for half a dozen or more - which gave us time to see if any of them caused regressions.

I'm less of a fan of these changes as I don't personally have an issue with CRM_Utils_Array::value & am still adding it in some places :-)

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