From 16fd31ae79d9ce630d8631d52f2ec1be1e51e1ef Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 3 Jul 2022 17:15:25 +1000 Subject: [PATCH] [REF][PHP8.1] Another set of fixes for passing in NULL values to php string or preg replace functions Update to Dave's suggestion for default value --- CRM/Activity/BAO/Activity.php | 2 +- CRM/Case/XMLRepository.php | 2 +- CRM/Contact/BAO/Contact.php | 4 ++-- CRM/Core/Action.php | 2 +- CRM/Core/BAO/CustomField.php | 4 ++-- CRM/Core/BAO/SchemaHandler.php | 6 +++--- CRM/Utils/Date.php | 2 +- CRM/Utils/String.php | 4 ++-- CRM/Utils/System.php | 6 +++--- Civi/Api4/Service/Spec/SpecFormatter.php | 2 +- Civi/Api4/Utils/FormattingUtil.php | 8 ++++---- api/v3/Generic.php | 4 ++-- 12 files changed, 23 insertions(+), 23 deletions(-) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index b2febd9bca..965e91d291 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -523,7 +523,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { // if the subject contains a ‘[case #…]’ string, file that activity on the related case (CRM-5916) $matches = []; - $subjectToMatch = $params['subject'] ?? NULL; + $subjectToMatch = $params['subject'] ?? ''; if (preg_match('/\[case #([0-9a-h]{7})\]/', $subjectToMatch, $matches)) { $key = CRM_Core_DAO::escapeString(CIVICRM_SITE_KEY); $hash = $matches[1]; diff --git a/CRM/Case/XMLRepository.php b/CRM/Case/XMLRepository.php index 9317db9b23..000ccdb52f 100644 --- a/CRM/Case/XMLRepository.php +++ b/CRM/Case/XMLRepository.php @@ -148,7 +148,7 @@ class CRM_Case_XMLRepository { */ public function findXmlFile($caseType) { // first check custom templates directory - $fileName = NULL; + $fileName = ''; if (!$fileName || !file_exists($fileName)) { $caseTypesViaHook = $this->getCaseTypesViaHook(); diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index c0928f3e66..afb00ab123 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -163,10 +163,10 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact implements Civi\Co // Note also orgs will get ellipsified, but if we do that here then // some existing tests on individual fail. // Also api v3 will enforce org naming length by failing, v4 will truncate. - if (mb_strlen($contact->display_name, 'UTF-8') > 128) { + if (mb_strlen(($contact->display_name ?? ''), 'UTF-8') > 128) { $contact->display_name = mb_substr($contact->display_name, 0, 128, 'UTF-8'); } - if (mb_strlen($contact->sort_name, 'UTF-8') > 128) { + if (mb_strlen(($contact->sort_name ?? ''), 'UTF-8') > 128) { $contact->sort_name = mb_substr($contact->sort_name, 0, 128, 'UTF-8'); } diff --git a/CRM/Core/Action.php b/CRM/Core/Action.php index fc1dcd7c4a..1267425c59 100644 --- a/CRM/Core/Action.php +++ b/CRM/Core/Action.php @@ -373,7 +373,7 @@ class CRM_Core_Action { */ public static function &replace(&$str, &$values) { foreach ($values as $n => $v) { - $str = str_replace("%%$n%%", $v, $str); + $str = str_replace("%%$n%%", ($v ?? ''), ($str ?? '')); } return $str; } diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index fab2821bfe..bfde273fb1 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -555,7 +555,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $fields = []; while (($dao->fetch()) != NULL) { - $regexp = preg_replace('/[.,;:!?]/', '', NULL); + $regexp = preg_replace('/[.,;:!?]/', '', ''); $fields[$dao->id]['id'] = $dao->id; $fields[$dao->id]['label'] = $dao->label; // This seems broken, but not in a new way. @@ -2112,7 +2112,7 @@ WHERE id IN ( %1, %2 ) //don't insert only value separator as default value, CRM-4579 $defaultValue = self::getOptionGroupDefault($params['option_group_id'], !empty($params['serialize'])); - if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultValue))) { + if ($defaultValue !== NULL && !CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultValue))) { $params['default_value'] = $defaultValue; } } diff --git a/CRM/Core/BAO/SchemaHandler.php b/CRM/Core/BAO/SchemaHandler.php index bda6dc09fa..0ee0b27c4c 100644 --- a/CRM/Core/BAO/SchemaHandler.php +++ b/CRM/Core/BAO/SchemaHandler.php @@ -171,14 +171,14 @@ class CRM_Core_BAO_SchemaHandler { // Add index if field is searchable if it does not reference a foreign key // (skip indexing FK fields because it would be redundant to have 2 indexes) - if (!empty($params['searchable']) && empty($params['fk_table_name']) && substr($existingIndex, 0, 5) !== 'INDEX') { + if (!empty($params['searchable']) && empty($params['fk_table_name']) && substr($existingIndex ?? '', 0, 5) !== 'INDEX') { $sql .= $separator; $sql .= str_repeat(' ', 8); $sql .= $prefix; $sql .= "INDEX_{$params['name']} ( {$params['name']} )"; } // Drop search index if field is no longer searchable - elseif (empty($params['searchable']) && substr($existingIndex, 0, 5) === 'INDEX') { + elseif (empty($params['searchable']) && substr($existingIndex ?? '', 0, 5) === 'INDEX') { $sql .= $separator; $sql .= str_repeat(' ', 8); $sql .= "DROP INDEX $existingIndex"; @@ -737,7 +737,7 @@ MODIFY {$columnName} varchar( $length ) $existingIndex = $dao->Key_name; } $fkSql = self::buildForeignKeySQL($params, ",\n", "ADD ", $params['table_name']); - if (substr($existingIndex, 0, 2) === 'FK' && !$fkSql) { + if (substr(($existingIndex ?? ''), 0, 2) === 'FK' && !$fkSql) { $sql .= "$separator DROP FOREIGN KEY {$existingIndex},\nDROP INDEX {$existingIndex}"; $separator = ",\n"; } diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index a08e6b7d68..f38274517d 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -1936,7 +1936,7 @@ class CRM_Utils_Date { $mysqlDate = 'null'; } - if (trim($date)) { + if (trim($date ?? '')) { $mysqlDate = date($format, strtotime($date . ' ' . $time)); } diff --git a/CRM/Utils/String.php b/CRM/Utils/String.php index 82d36093ad..939ad2270a 100644 --- a/CRM/Utils/String.php +++ b/CRM/Utils/String.php @@ -878,7 +878,7 @@ class CRM_Utils_String { if ($fragment === '') { return TRUE; } - $len = strlen($fragment); + $len = strlen($fragment ?? ''); return substr($string, 0, $len) === $fragment; } @@ -895,7 +895,7 @@ class CRM_Utils_String { if ($fragment === '') { return TRUE; } - $len = strlen($fragment); + $len = strlen($fragment ?? ''); return substr($string, -1 * $len) === $fragment; } diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php index d92b359d7e..1d8a67cb67 100644 --- a/CRM/Utils/System.php +++ b/CRM/Utils/System.php @@ -207,7 +207,7 @@ class CRM_Utils_System { * * @param array|string $query * - * @return string + * @return string|null */ public static function makeQueryString($query) { if (is_array($query)) { @@ -255,12 +255,12 @@ class CRM_Utils_System { $query = self::makeQueryString($query); // Legacy handling for when the system passes around html escaped strings - if (strstr($query, '&')) { + if (strstr(($query ?? ''), '&')) { $query = html_entity_decode($query); } // Extract fragment from path or query if munged together - if ($query && strstr($query, '#')) { + if ($query && strstr(($query ?? ''), '#')) { list($path, $fragment) = explode('#', $query); } if ($path && strstr($path, '#')) { diff --git a/Civi/Api4/Service/Spec/SpecFormatter.php b/Civi/Api4/Service/Spec/SpecFormatter.php index ad7a79b3c7..b8bd38fc13 100644 --- a/Civi/Api4/Service/Spec/SpecFormatter.php +++ b/Civi/Api4/Service/Spec/SpecFormatter.php @@ -257,7 +257,7 @@ class SpecFormatter { if (property_exists($query, $ret)) { // Note: our schema is inconsistent about whether `description` fields allow html, // but it's usually assumed to be plain text, so we strip_tags() to standardize it. - $options[$optionIndex[$query->id]][$ret] = $ret === 'description' ? strip_tags($query->$ret) : $query->$ret; + $options[$optionIndex[$query->id]][$ret] = $ret === 'description' ? strip_tags($query->$ret ?? '') : $query->$ret; } } } diff --git a/Civi/Api4/Utils/FormattingUtil.php b/Civi/Api4/Utils/FormattingUtil.php index 5d6991622e..da1129ad4f 100644 --- a/Civi/Api4/Utils/FormattingUtil.php +++ b/Civi/Api4/Utils/FormattingUtil.php @@ -89,7 +89,7 @@ class FormattingUtil { */ public static function formatInputValue(&$value, ?string $fieldName, array $fieldSpec, &$operator = NULL, $index = NULL) { // Evaluate pseudoconstant suffix - $suffix = strpos($fieldName, ':'); + $suffix = strpos(($fieldName ?? ''), ':'); if ($suffix) { $options = self::getPseudoconstantList($fieldSpec, $fieldName, [], $operator ? 'get' : 'create'); $value = self::replacePseudoconstant($options, $value, TRUE); @@ -145,7 +145,7 @@ class FormattingUtil { public static function formatDateValue($format, $value, &$operator = NULL, $index = NULL) { // Non-relative dates (or if no search operator) if (!$operator || !array_key_exists($value, \CRM_Core_OptionGroup::values('relative_date_filters'))) { - return date($format, strtotime($value)); + return date($format, strtotime($value ?? '')); } if (isset($index) && !strstr($operator, 'BETWEEN')) { throw new \API_Exception("Relative dates cannot be in an array using the $operator operator."); @@ -196,7 +196,7 @@ class FormattingUtil { $contactTypePaths = []; foreach ($result as $key => $value) { $fieldExpr = SqlExpression::convert($selectAliases[$key] ?? $key); - $fieldName = \CRM_Utils_Array::first($fieldExpr->getFields()); + $fieldName = \CRM_Utils_Array::first($fieldExpr->getFields() ?? ''); $baseName = $fieldName ? \CRM_Utils_Array::first(explode(':', $fieldName)) : NULL; $field = $fields[$fieldName] ?? $fields[$baseName] ?? NULL; $dataType = $field['data_type'] ?? ($fieldName == 'id' ? 'Integer' : NULL); @@ -209,7 +209,7 @@ class FormattingUtil { $dataType = NULL; } // Evaluate pseudoconstant suffixes - $suffix = strrpos($fieldName, ':'); + $suffix = strrpos(($fieldName ?? ''), ':'); $fieldOptions = NULL; if ($suffix) { $fieldOptions = self::getPseudoconstantList($field, $fieldName, $result, $action); diff --git a/api/v3/Generic.php b/api/v3/Generic.php index 9188bcff50..ce0740035a 100644 --- a/api/v3/Generic.php +++ b/api/v3/Generic.php @@ -167,11 +167,11 @@ function civicrm_api3_generic_getfields($apiRequest, $unique = TRUE) { } else { // not implemented MagicFunctionProvider - $helper = NULL; + $helper = ''; } } catch (\Civi\API\Exception\NotImplementedException $e) { - $helper = NULL; + $helper = ''; } if (function_exists($helper)) { // alter -- 2.25.1