Skip to content

Commit

Permalink
[REF][PHP8.1] Batch 4 of fixing issues found in unit tests where pass…
Browse files Browse the repository at this point in the history
…ing NULL values triggers deprecations

Apply fixes as per Demeritcowboy
  • Loading branch information
seamuslee001 committed Jul 20, 2022
1 parent 2c3d19f commit deb4b42
Show file tree
Hide file tree
Showing 21 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -2555,7 +2555,7 @@ public static function getValues($params, &$values) {
// communication Prefferance
$preffComm = $comm = [];
$comm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
$contact->preferred_communication_method
($contact->preferred_communication_method ?? '')
);
foreach ($comm as $value) {
$preffComm[$value] = 1;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Edit/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public static function setDefaultValues(&$defaults, &$form) {
*/
public static function storeRequiredCustomDataInfo(&$form, $groupTree) {
if (in_array(CRM_Utils_System::getClassName($form), ['CRM_Contact_Form_Contact', 'CRM_Contact_Form_Inline_Address'])) {
$requireOmission = NULL;
$requireOmission = '';
foreach ($groupTree as $csId => $csVal) {
// only process Address entity fields
if ($csVal['extends'] !== 'Address') {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ public static function parseStreetAddress($streetAddress, $locale = NULL) {
// the DB to fatal
$fields = CRM_Core_BAO_Address::fields();
foreach ($fields as $fieldname => $field) {
if (!empty($field['maxlength']) && strlen(CRM_Utils_Array::value($fieldname, $parseFields)) > $field['maxlength']) {
if (!empty($field['maxlength']) && strlen(($parseFields[$fieldname]) ?? '')) > $field['maxlength']) {
return $emptyParseFields;
}
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ private static function formatDisplayValue($value, $field, $entityId = NULL) {
// In such cases we could just get intval($value) and fetch matching
// option again, but this would not work if key is float like 5.6.
// So we need to truncate trailing zeros to make it work as expected.
if ($display === '' && strpos($value, '.') !== FALSE) {
if ($display === '' && strpos(($value ?? ''), '.') !== FALSE) {
// Use round() to truncate trailing zeros, e.g:
// 10.00 -> 10, 10.60 -> 10.6, 10.69 -> 10.69.
$value = (string) round($value, 5);
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/OptionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function add(&$params, $ids = []) {
if (empty($params['name']) && empty($params['id'])) {
$params['name'] = CRM_Utils_String::titleToVar(strtolower($params['title']));
}
elseif (!empty($params['name']) && strpos($params['name'], ' ')) {
elseif (!empty($params['name']) && strpos(($params['name']), ' ')) {
$params['name'] = CRM_Utils_String::titleToVar(strtolower($params['name']));
}
elseif (!empty($params['name'])) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/DAO/AllCoreTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static function isCoreTable($tableName) {
* @return string
*/
public static function getCanonicalClassName($baoName) {
return str_replace('_BAO_', '_DAO_', $baoName);
return str_replace('_BAO_', '_DAO_', ($baoName ?? ''));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function setNextUrl(string $pathPart) {
}

$session = CRM_Core_Session::singleton();
$searchFormName = strtolower($this->get('searchFormName'));
$searchFormName = strtolower($this->get('searchFormName') ?? '');
if ($searchFormName === 'search') {
$session->replaceUserContext(CRM_Utils_System::url('civicrm/' . $pathPart . '/search', $urlParams));
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/Smarty/plugins/modifier.crmNumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* For alternate decimal point and thousands separator, delimit values with single quotes in the template.
* EXAMPLE: {$number|crmNumberFormat:2:',':' '} for French notation - 1234.56 becomes 1 234,56
*/
function smarty_modifier_crmNumberFormat($number, $decimals = NULL, $dec_point = NULL, $thousands_sep = NULL) {
function smarty_modifier_crmNumberFormat($number, $decimals = 0, $dec_point = NULL, $thousands_sep = NULL) {
if (is_numeric($number)) {
// Both dec_point AND thousands_sep are required if one is not specified
// then use the config defaults
Expand Down
2 changes: 1 addition & 1 deletion CRM/Financial/BAO/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static function create(array $params): CRM_Financial_DAO_FinancialTrxn {
'is_email_receipt' => $params['is_send_contribution_notification'],
'trxn_date' => $params['trxn_date'],
'payment_instrument_id' => $paymentTrxnParams['payment_instrument_id'],
'payment_processor_id' => $paymentTrxnParams['payment_processor_id'] ?? NULL,
'payment_processor_id' => $paymentTrxnParams['payment_processor_id'] ?? '',
]);
// Get the trxn
$trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
Expand Down
2 changes: 1 addition & 1 deletion CRM/Financial/BAO/PaymentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static function getCreditCards($paymentProcessorID = NULL) {
$processor = new CRM_Financial_DAO_PaymentProcessor();
$processor->id = $paymentProcessorID;
$processor->find(TRUE);
$cards = json_decode($processor->accepted_credit_cards, TRUE);
$cards = json_decode(($processor->accepted_credit_cards ?? ''), TRUE);
return $cards;
}
return [];
Expand Down
2 changes: 1 addition & 1 deletion CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,7 @@ protected function validateCustomField($customFieldID, $value, array $fieldMetaD
*/
protected function getFieldEntity(string $fieldName) {
if ($fieldName === 'do_not_import') {
return NULL;
return '';
}
if (in_array($fieldName, ['email_greeting_id', 'postal_greeting_id', 'addressee_id'], TRUE)) {
return 'Contact';
Expand Down
8 changes: 4 additions & 4 deletions CRM/Logging/ReportDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ protected function convertDiffsToRows() {
}

// special-case for multiple values. Also works for CRM-7251: preferred_communication_method
if ((substr($from, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR &&
substr($from, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) ||
(substr($to, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR &&
substr($to, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)
if ((substr(($from ?? ''), 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR &&
substr(($from ?? ''), -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) ||
(substr(($to ?? ''), 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR &&
substr(($to ?? ''), -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)
) {
$froms = $tos = [];
foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($from, CRM_Core_DAO::VALUE_SEPARATOR)) as $val) {
Expand Down
8 changes: 4 additions & 4 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1529,10 +1529,10 @@ public function addToDeveloperTab($sql) {
$this->sqlArray[] = $sql;
foreach ($this->sqlArray as $sql) {
foreach (['LEFT JOIN'] as $term) {
$sql = str_replace($term, '<br> ' . $term, $sql);
$sql = str_replace($term, '<br> ' . $term, ($sql ?? ''));
}
foreach (['FROM', 'WHERE', 'GROUP BY', 'ORDER BY', 'LIMIT', ';'] as $term) {
$sql = str_replace($term, '<br><br>' . $term, $sql);
$sql = str_replace($term, '<br><br>' . $term, ($sql ?? ''));
}
$this->sqlFormattedArray[] = $sql;
$this->assign('sql', implode(';<br><br><br><br>', $this->sqlFormattedArray));
Expand Down Expand Up @@ -3443,7 +3443,7 @@ public function filterStat(&$statistics) {
if (!empty($this->_params["{$fieldName}_relative"])) {
[$from, $to] = CRM_Utils_Date::getFromTo($this->_params["{$fieldName}_relative"], NULL, NULL);
}
if (strlen($to) === 10) {
if (strlen($to ?? '') === 10) {
// If we just have the date we assume the end of that day.
$to .= ' 23:59:59';
}
Expand Down Expand Up @@ -3949,7 +3949,7 @@ public function buildACLClause($tableAlias = 'contact_a') {
public function buildPermissionClause() {
$ret = [];
foreach ($this->selectedTables() as $tableName) {
$baoName = str_replace('_DAO_', '_BAO_', CRM_Core_DAO_AllCoreTables::getClassForTable($tableName));
$baoName = str_replace('_DAO_', '_BAO_', (CRM_Core_DAO_AllCoreTables::getClassForTable($tableName) ?? ''));
if ($baoName && class_exists($baoName) && !empty($this->_columns[$tableName]['alias'])) {
$tableAlias = $this->_columns[$tableName]['alias'];
$clauses = array_filter($baoName::getSelectWhereClause($tableAlias));
Expand Down
14 changes: 7 additions & 7 deletions CRM/Utils/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public static function customFormat($dateString, $format = NULL, $dateParts = NU
$day = (int) substr(($dateString ?? ''), 6, 2);
}

if (strlen($dateString) > 10) {
if (strlen(($dateString ?? '')) > 10) {
$format = $config->dateformatDatetime;
}
elseif ($day > 0) {
Expand Down Expand Up @@ -481,12 +481,12 @@ public static function customFormatTs($timestamp, $format = NULL, $dateParts = N
* date/datetime in ISO format
*/
public static function mysqlToIso($mysql) {
$year = substr($mysql, 0, 4);
$month = substr($mysql, 4, 2);
$day = substr($mysql, 6, 2);
$hour = substr($mysql, 8, 2);
$minute = substr($mysql, 10, 2);
$second = substr($mysql, 12, 2);
$year = substr(($mysql ?? ''), 0, 4);
$month = substr(($mysql ?? ''), 4, 2);
$day = substr(($mysql ?? ''), 6, 2);
$hour = substr(($mysql ?? ''), 8, 2);
$minute = substr(($mysql ?? ''), 10, 2);
$second = substr(($mysql ?? ''), 12, 2);

$iso = '';
if ($year) {
Expand Down
9 changes: 6 additions & 3 deletions CRM/Utils/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ public static function makeFileName($name) {
$uniqID = md5(uniqid(rand(), TRUE));
$info = pathinfo($name);
$basename = substr($info['basename'],
0, -(strlen(CRM_Utils_Array::value('extension', $info)) + (CRM_Utils_Array::value('extension', $info) == '' ? 0 : 1))
0, -(strlen(CRM_Utils_Array::value('extension', $info, '')) + (CRM_Utils_Array::value('extension', $info, '') == '' ? 0 : 1))
);
if (!self::isExtensionSafe(CRM_Utils_Array::value('extension', $info))) {
if (!self::isExtensionSafe(CRM_Utils_Array::value('extension', $info, ''))) {
// munge extension so it cannot have an embbeded dot in it
// The maximum length of a filename for most filesystems is 255 chars.
// We'll truncate at 240 to give some room for the extension.
Expand Down Expand Up @@ -1058,7 +1058,7 @@ public static function getIconFromMimeType($mimeType) {
}
$iconClasses = Civi::$statics[__CLASS__]['mimeIcons'];
foreach ($iconClasses as $text => $icon) {
if (strpos($mimeType, $text) === 0) {
if (strpos(($mimeType ?? ''), $text) === 0) {
return $icon;
}
}
Expand Down Expand Up @@ -1128,6 +1128,9 @@ public static function getExtensionFromPath($path) {
* In php8 the return value from is_dir() is always bool but in php7 it can be null.
*/
public static function isDir(?string $dir) {
if ($dir === NULL) {
return FALSE;
}
set_error_handler(function($errno, $errstr) {
// If this is open_basedir-related, convert it to an exception so we
// can catch it.
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public static function setMimeParams($message, $params = NULL) {
public static function formatRFC822Email($name, $email, $useQuote = FALSE) {
$result = NULL;

$name = trim($name);
$name = trim($name ?? '');

// strip out double quotes if present at the beginning AND end
if (substr($name, 0, 1) == '"' &&
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static function makeQueryString($query) {
if (is_array($query)) {
$buf = '';
foreach ($query as $key => $value) {
$buf .= ($buf ? '&' : '') . urlencode($key) . '=' . urlencode($value);
$buf .= ($buf ? '&' : '') . urlencode($key ?? '') . '=' . urlencode($value ?? '');
}
$query = $buf;
}
Expand Down
2 changes: 1 addition & 1 deletion api/v3/CustomValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function civicrm_api3_custom_value_get($params) {
// Convert multi-value strings to arrays
$sp = CRM_Core_DAO::VALUE_SEPARATOR;
foreach ($result as $id => $value) {
if (strpos($value, $sp) !== FALSE) {
if (strpos(($value ?? ''), $sp) !== FALSE) {
$value = explode($sp, trim($value, $sp));
}

Expand Down
2 changes: 1 addition & 1 deletion api/v3/Mailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ function civicrm_api3_mailing_send_test($params) {
$job = civicrm_api3('MailingJob', 'create', $testEmailParams);
CRM_Mailing_BAO_Mailing::getRecipients($testEmailParams['mailing_id']);
$testEmailParams['job_id'] = $job['id'];
$testEmailParams['emails'] = array_key_exists('test_email', $testEmailParams) ? explode(',', strtolower($testEmailParams['test_email'])) : NULL;
$testEmailParams['emails'] = array_key_exists('test_email', $testEmailParams) ? explode(',', strtolower($testEmailParams['test_email'] ?? '')) : NULL;
if (!empty($params['test_email'])) {
$query = CRM_Utils_SQL_Select::from('civicrm_email e')
->select(['e.id', 'e.contact_id', 'e.email'])
Expand Down
2 changes: 1 addition & 1 deletion api/v3/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function civicrm_api3_profile_submit($params) {
$contactEntities = ['contact', 'individual', 'organization', 'household'];
$locationEntities = ['email', 'address', 'phone', 'website', 'im'];

$entity = strtolower(CRM_Utils_Array::value('entity', $field));
$entity = strtolower(CRM_Utils_Array::value('entity', $field, ''));
if ($entity && !in_array($entity, array_merge($contactEntities, $locationEntities))) {
switch ($entity) {
case 'note':
Expand Down
4 changes: 2 additions & 2 deletions api/v3/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ function _civicrm_api3_validate_constraint($fieldValue, $fieldName, $fieldInfo,
*/
function _civicrm_api3_validate_unique_key(&$params, &$fieldName) {
[$fieldValue, $op] = _civicrm_api3_field_value_check($params, $fieldName);
if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
if (strpos(($op ?? ''), 'NULL') !== FALSE || strpos(($op ?? ''), 'EMPTY') !== FALSE) {
return;
}
$existing = civicrm_api($params['entity'], 'get', [
Expand Down Expand Up @@ -2217,7 +2217,7 @@ function _civicrm_api3_resolve_contactID($contactIdExpr) {
*/
function _civicrm_api3_validate_html(&$params, &$fieldName, $fieldInfo) {
[$fieldValue, $op] = _civicrm_api3_field_value_check($params, $fieldName);
if (strpos($op, 'NULL') || strpos($op, 'EMPTY')) {
if (strpos(($op ?? ''), 'NULL') || strpos(($op ?? ''), 'EMPTY')) {
return;
}
}
Expand Down

0 comments on commit deb4b42

Please sign in to comment.