From 2aa91919863b11d4f102b6ad65dcfdd951ea66db Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 14 Mar 2020 20:20:06 -0400 Subject: [PATCH] Cleanup uses of CRM_Utils_Array::value related to numbers Some manual cleanup around the areas of code where CRM_Utils_Array::value is used for a number defaulting to 0 --- CRM/Activity/BAO/Activity.php | 6 +++--- CRM/Admin/Page/AJAX.php | 2 +- CRM/Contact/Form/Search/Criteria.php | 2 +- CRM/Contact/Import/Form/DataSource.php | 2 +- CRM/Contribute/BAO/ContributionRecur.php | 2 +- CRM/Core/BAO/Block.php | 2 +- CRM/Core/BAO/Dashboard.php | 2 +- CRM/Core/Menu.php | 1 - CRM/Core/Smarty/plugins/function.crmKey.php | 5 +---- CRM/Event/Form/Participant.php | 2 +- CRM/Utils/Chart.php | 2 +- api/v3/Contact.php | 2 +- api/v3/DashboardContact.php | 2 +- api/v3/Job.php | 2 +- api/v3/utils.php | 22 ++++++++------------- tests/phpunit/api/v3/MembershipTypeTest.php | 2 +- 16 files changed, 24 insertions(+), 34 deletions(-) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index 06f0e91b62bd..8e96e39a6ce1 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -757,7 +757,7 @@ public static function getActivities($params) { ]; $activities[$id]['activity_type_name'] = CRM_Core_PseudoConstant::getName('CRM_Activity_BAO_Activity', 'activity_type_id', $activity['activity_type_id']); $activities[$id]['activity_type'] = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $activity['activity_type_id']); - $activities[$id]['target_contact_count'] = CRM_Utils_Array::value('target_contact_count', $activity, 0); + $activities[$id]['target_contact_count'] = $activity['target_contact_count'] ?? 0; if (!empty($activity['target_contact_count'])) { $displayedTarget = civicrm_api3('ActivityContact', 'get', [ 'activity_id' => $id, @@ -1356,7 +1356,7 @@ public static function sendSMS( $smsProviderParams['To'] = ''; } - $doNotSms = CRM_Utils_Array::value('do_not_sms', $contact, 0); + $doNotSms = $contact['do_not_sms'] ?? 0; if ($doNotSms) { $errMsgs[] = PEAR::raiseError('Contact Does not accept SMS', NULL, PEAR_ERROR_RETURN); @@ -2462,7 +2462,7 @@ protected static function getActivityParamsForDashboardFunctions($params) { 'activity_date_time' => $params['activity_date_time'] ?? NULL, 'check_permissions' => 1, 'options' => [ - 'offset' => CRM_Utils_Array::value('offset', $params, 0), + 'offset' => $params['offset'] ?? 0, ], ]; diff --git a/CRM/Admin/Page/AJAX.php b/CRM/Admin/Page/AJAX.php index 79e2ec3b8ba4..2072b2b68716 100644 --- a/CRM/Admin/Page/AJAX.php +++ b/CRM/Admin/Page/AJAX.php @@ -341,7 +341,7 @@ public static function recipientListing() { * Used by jstree to incrementally load tags */ public static function getTagTree() { - $parent = CRM_Utils_Type::escape(CRM_Utils_Array::value('parent_id', $_GET, 0), 'Integer'); + $parent = CRM_Utils_Type::escape(($_GET['parent_id'] ?? 0), 'Integer'); $substring = CRM_Utils_Type::escape(CRM_Utils_Array::value('str', $_GET), 'String'); $result = []; diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index 89010bee1545..d4d711296cb7 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -412,7 +412,7 @@ public static function location(&$form) { 'street_unit' => [ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL, NULL], ]; - $parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', $addressOptions, 0); + $parseStreetAddress = $addressOptions['street_address_parsing'] ?? 0; $form->assign('parseStreetAddress', $parseStreetAddress); foreach ($elements as $name => $v) { list($title, $attributes, $select, $multiSelect) = $v; diff --git a/CRM/Contact/Import/Form/DataSource.php b/CRM/Contact/Import/Form/DataSource.php index e7cc48bd6cef..97ffa8453d88 100644 --- a/CRM/Contact/Import/Form/DataSource.php +++ b/CRM/Contact/Import/Form/DataSource.php @@ -57,7 +57,7 @@ public function preProcess() { $errorFiles = ['sqlImport.errors', 'sqlImport.conflicts', 'sqlImport.duplicates', 'sqlImport.mismatch']; // check for post max size avoid when called twice - $snippet = CRM_Utils_Array::value('snippet', $_GET, 0); + $snippet = $_GET['snippet'] ?? 0; if (empty($snippet)) { CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); } diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index 3a9a441c5d04..188c31ef4bcf 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -181,7 +181,7 @@ public static function getPaymentProcessorID($recurID) { 'id' => $recurID, 'return' => ['payment_processor_id'], ]); - return (int) CRM_Utils_Array::value('payment_processor_id', $recur, 0); + return (int) ($recur['payment_processor_id'] ?? 0); } /** diff --git a/CRM/Core/BAO/Block.php b/CRM/Core/BAO/Block.php index edee3b3d8a4e..86210384041c 100644 --- a/CRM/Core/BAO/Block.php +++ b/CRM/Core/BAO/Block.php @@ -399,7 +399,7 @@ public static function handlePrimary(&$params, $class) { } // contact_id in params might be empty or the string 'null' so cast to integer - $contactId = (int) CRM_Utils_Array::value('contact_id', $params); + $contactId = (int) ($params['contact_id'] ?? 0); // If id is set & we haven't been passed a contact_id, retrieve it if (!empty($params['id']) && !isset($params['contact_id'])) { $entity = new $class(); diff --git a/CRM/Core/BAO/Dashboard.php b/CRM/Core/BAO/Dashboard.php index a6388e15c24a..7fe98ea1dd97 100644 --- a/CRM/Core/BAO/Dashboard.php +++ b/CRM/Core/BAO/Dashboard.php @@ -496,7 +496,7 @@ public static function addContactDashletToDashboard(&$params) { foreach ($params as $dashboardIDs) { $contactID = CRM_Utils_Array::value('contact_id', $dashboardIDs); $dashboardID = CRM_Utils_Array::value('dashboard_id', $dashboardIDs); - $column = CRM_Utils_Array::value('column_no', $dashboardIDs, 0); + $column = $dashboardIDs['column_no'] ?? 0; $columns[$column][$dashboardID] = 0; } self::saveDashletChanges($columns, $contactID); diff --git a/CRM/Core/Menu.php b/CRM/Core/Menu.php index e1bfa5a40ff5..20593b0b286e 100644 --- a/CRM/Core/Menu.php +++ b/CRM/Core/Menu.php @@ -373,7 +373,6 @@ public static function buildAdminLinks(&$menu) { $values[$item['adminGroup']] = array(); $values[$item['adminGroup']]['fields'] = array(); } - $weight = CRM_Utils_Array::value('weight', $item, 0); $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value; $values[$item['adminGroup']]['component_id'] = $item['component_id']; } diff --git a/CRM/Core/Smarty/plugins/function.crmKey.php b/CRM/Core/Smarty/plugins/function.crmKey.php index 894b252f7a2f..63ec93388ce0 100644 --- a/CRM/Core/Smarty/plugins/function.crmKey.php +++ b/CRM/Core/Smarty/plugins/function.crmKey.php @@ -29,8 +29,5 @@ * the generated key */ function smarty_function_crmKey($params, &$smarty) { - return CRM_Core_Key::get( - $params['name'], - CRM_Utils_Array::value('addSequence', $params, 0) - ); + return CRM_Core_Key::get($params['name'], $params['addSequence'] ?? FALSE); } diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index 9a955b3cba99..ff9b315d5538 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -421,7 +421,7 @@ public function preProcess() { // when custom data is included in this page if (!empty($_POST['hidden_custom'])) { - $eventId = (int) CRM_Utils_Array::value('event_id', $_POST); + $eventId = (int) ($_POST['event_id'] ?? 0); // Custom data of type participant role // Note: Some earlier commits imply $_POST['role_id'] could be a comma separated string, // not sure if that ever really happens diff --git a/CRM/Utils/Chart.php b/CRM/Utils/Chart.php index c8745fa0a5f0..36fa6ea729fd 100644 --- a/CRM/Utils/Chart.php +++ b/CRM/Utils/Chart.php @@ -205,7 +205,7 @@ public static function chart($rows, $chart, $interval) { } // rotate the x labels. - $chartData['xLabelAngle'] = CRM_Utils_Array::value('xLabelAngle', $rows, 0); + $chartData['xLabelAngle'] = $rows['xLabelAngle'] ?? 0; if (!empty($rows['tip'])) { $chartData['tip'] = $rows['tip']; } diff --git a/api/v3/Contact.php b/api/v3/Contact.php index 9fd042bf3324..d79910a4c732 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -858,7 +858,7 @@ function civicrm_api3_contact_getquick($params) { } $actualSelectElements = implode(', ', $actualSelectElements); $from = implode(' ', $from); - $limit = (int) CRM_Utils_Array::value('limit', $params); + $limit = (int) ($params['limit'] ?? 0); $limit = $limit > 0 ? $limit : Civi::settings()->get('search_autocomplete_count'); // add acl clause here diff --git a/api/v3/DashboardContact.php b/api/v3/DashboardContact.php index e867a9bf4824..ea47c4f70473 100644 --- a/api/v3/DashboardContact.php +++ b/api/v3/DashboardContact.php @@ -73,7 +73,7 @@ function _civicrm_api3_dashboard_contact_create_spec(&$params) { function _civicrm_api3_dashboard_contact_check_params(&$params) { $dashboard_id = CRM_Utils_Array::value('dashboard_id', $params); if ($dashboard_id) { - $allDashlets = CRM_Core_BAO_Dashboard::getDashlets(TRUE, CRM_Utils_Array::value('check_permissions', $params, 0)); + $allDashlets = CRM_Core_BAO_Dashboard::getDashlets(TRUE, $params['check_permissions'] ?? 0); if (!isset($allDashlets[$dashboard_id])) { return civicrm_api3_create_error('Invalid or inaccessible dashboard ID'); } diff --git a/api/v3/Job.php b/api/v3/Job.php index 20a1e15d1f36..c3dd38b069a4 100644 --- a/api/v3/Job.php +++ b/api/v3/Job.php @@ -678,7 +678,7 @@ function civicrm_api3_job_group_rebuild($params) { throw new API_Exception('Could not acquire lock, another GroupRebuild process is running'); } - $limit = CRM_Utils_Array::value('limit', $params, 0); + $limit = $params['limit'] ?? 0; CRM_Contact_BAO_GroupContactCache::loadAll(NULL, $limit); $lock->release(); diff --git a/api/v3/utils.php b/api/v3/utils.php index 65fe0128da64..d7f6598d2f30 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -776,25 +776,21 @@ function _civicrm_api3_apply_filters_to_dao($filterField, $filterValue, &$dao) { function _civicrm_api3_get_options_from_params($params, $queryObject = FALSE, $entity = '', $action = '') { $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity); $is_count = FALSE; - $sort = CRM_Utils_Array::value('sort', $params, 0); - $sort = CRM_Utils_Array::value('option.sort', $params, $sort); - $sort = CRM_Utils_Array::value('option_sort', $params, $sort); - $offset = CRM_Utils_Array::value('offset', $params, 0); - $offset = CRM_Utils_Array::value('option.offset', $params, $offset); // dear PHP thought it would be a good idea to transform a.b into a_b in the get/post - $offset = CRM_Utils_Array::value('option_offset', $params, $offset); + $sort = $params['option_sort'] ?? $params['option.sort'] ?? $params['sort'] ?? 0; + $offset = $params['option_offset'] ?? $params['option.offset'] ?? $params['offset'] ?? 0; $limit = CRM_Utils_Array::value('rowCount', $params, 25); $limit = CRM_Utils_Array::value('option.limit', $params, $limit); $limit = CRM_Utils_Array::value('option_limit', $params, $limit); - if (is_array(CRM_Utils_Array::value('options', $params))) { + if (isset($params['options']) && is_array($params['options'])) { // is count is set by generic getcount not user - $is_count = CRM_Utils_Array::value('is_count', $params['options']); - $offset = CRM_Utils_Array::value('offset', $params['options'], $offset); - $limit = CRM_Utils_Array::value('limit', $params['options'], $limit); - $sort = CRM_Utils_Array::value('sort', $params['options'], $sort); + $is_count = $params['options']['is_count'] ?? FALSE; + $offset = $params['options']['offset'] ?? $offset; + $limit = CRM_Utils_Array::value('limit', $params['options'], $limit); + $sort = $params['options']['sort'] ?? $sort; } $returnProperties = []; @@ -869,9 +865,7 @@ function _civicrm_api3_get_options_from_params($params, $queryObject = FALSE, $e elseif ($n === 'id') { $inputParams[$lowercase_entity . '_id'] = $v; } - elseif (in_array($n, $otherVars)) { - } - else { + elseif (!in_array($n, $otherVars)) { $inputParams[$n] = $v; if ($v && !is_array($v) && stristr($v, 'SELECT')) { throw new API_Exception('invalid string'); diff --git a/tests/phpunit/api/v3/MembershipTypeTest.php b/tests/phpunit/api/v3/MembershipTypeTest.php index bd47aa24c01d..bf78045c5589 100644 --- a/tests/phpunit/api/v3/MembershipTypeTest.php +++ b/tests/phpunit/api/v3/MembershipTypeTest.php @@ -287,7 +287,7 @@ public function testEnableMembershipTypeOnContributionPage() { $membetype = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($type); $fieldParams['option_id'] = [1 => $priceFieldValue['id']]; $fieldParams['option_label'][$rowCount] = CRM_Utils_Array::value('name', $membetype); - $fieldParams['option_amount'][$rowCount] = CRM_Utils_Array::value('minimum_fee', $membetype, 0); + $fieldParams['option_amount'][$rowCount] = $membetype['minimum_fee'] ?? 0; $fieldParams['option_weight'][$rowCount] = CRM_Utils_Array::value('weight', $membetype); $fieldParams['option_description'][$rowCount] = CRM_Utils_Array::value('description', $membetype); $fieldParams['option_financial_type_id'][$rowCount] = CRM_Utils_Array::value('financial_type_id', $membetype);