From a2449c0cbcf3a43c4d9610be4f47fefb724124fd Mon Sep 17 00:00:00 2001 From: colemanw Date: Wed, 19 Feb 2025 16:14:38 -0500 Subject: [PATCH] [REF] CRM/Utils - Use str_contains instead of strpos Updates code to use preferred newer PHP function --- CRM/Utils/Check/Component/Env.php | 4 ++-- CRM/Utils/Color.php | 2 +- CRM/Utils/Date.php | 4 ++-- CRM/Utils/File.php | 2 +- CRM/Utils/GuzzleMiddleware.php | 2 +- CRM/Utils/Hook/UnitTests.php | 2 +- CRM/Utils/Http.php | 2 +- CRM/Utils/Mail.php | 4 ++-- CRM/Utils/Money.php | 2 +- CRM/Utils/Request.php | 2 +- CRM/Utils/String.php | 4 ++-- CRM/Utils/System.php | 15 +++++---------- CRM/Utils/System/Backdrop.php | 2 +- CRM/Utils/System/Drupal.php | 2 +- CRM/Utils/System/DrupalBase.php | 5 +---- CRM/Utils/System/Joomla.php | 4 ++-- CRM/Utils/Token.php | 2 +- CRM/Utils/Type.php | 2 +- 18 files changed, 27 insertions(+), 35 deletions(-) diff --git a/CRM/Utils/Check/Component/Env.php b/CRM/Utils/Check/Component/Env.php index 50607a3b74fd..9b9ac26d32d4 100644 --- a/CRM/Utils/Check/Component/Env.php +++ b/CRM/Utils/Check/Component/Env.php @@ -784,7 +784,7 @@ public function checkScheduledJobLogErrors() { 'job_id' => $job['id'], 'options' => ['sort' => "id desc", 'limit' => 1], ])['values'][0]['description'] ?? NULL; - if (!empty($lastExecutionMessage) && strpos($lastExecutionMessage, 'Failure') !== FALSE) { + if (!empty($lastExecutionMessage) && str_contains($lastExecutionMessage, 'Failure')) { $viewLogURL = CRM_Utils_System::url('civicrm/admin/joblog', "jid={$job['id']}&reset=1"); $html .= ' ' . $job['name'] . ' @@ -1045,7 +1045,7 @@ public function checkMysqlUtf8mb4() { } // Ensure that the MySQL driver supports utf8mb4 encoding. $version = mysqli_get_client_info(); - if (strpos($version, 'mysqlnd') !== FALSE) { + if (str_contains($version, 'mysqlnd')) { // The mysqlnd driver supports utf8mb4 starting at version 5.0.9. $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version); if (version_compare($version, '5.0.9', '<')) { diff --git a/CRM/Utils/Color.php b/CRM/Utils/Color.php index d38981b7470f..688b92ca6aed 100644 --- a/CRM/Utils/Color.php +++ b/CRM/Utils/Color.php @@ -100,7 +100,7 @@ public static function getHighlight($color) { * @return string|null */ public static function nameToHex($colorName) { - if (strpos($colorName, '#') !== FALSE || strpos($colorName, '(') !== FALSE) { + if (str_contains($colorName, '#') || str_contains($colorName, '(')) { return NULL; } if (empty(Civi::$statics[__CLASS__]['names'])) { diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index 0c5c109d3e99..9e53c98d1117 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -2285,8 +2285,8 @@ public static function convertDateToLocalTime($dateObject, $format = 'YmdHis') { public static function datePickerValueWithTimeHasDate($value) { // If there's no : (time) or a : and a - (date) then return true return ( - strpos($value, ':') === FALSE - || strpos($value, ':') !== FALSE && strpos($value, '-') !== FALSE + !str_contains($value, ':') + || str_contains($value, ':') && str_contains($value, '-') ); } diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index fbfd9d8b26b7..27ea8857aa62 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -1173,7 +1173,7 @@ public static function isDir(?string $dir) { set_error_handler(function($errno, $errstr) { // If this is open_basedir-related, convert it to an exception so we // can catch it. - if (strpos($errstr, 'open_basedir restriction in effect') !== FALSE) { + if (str_contains($errstr, 'open_basedir restriction in effect')) { throw new \ErrorException($errstr, $errno); } // Continue with normal error handling so other errors still happen. diff --git a/CRM/Utils/GuzzleMiddleware.php b/CRM/Utils/GuzzleMiddleware.php index c8b76cce88ca..3ac394761207 100644 --- a/CRM/Utils/GuzzleMiddleware.php +++ b/CRM/Utils/GuzzleMiddleware.php @@ -168,7 +168,7 @@ protected static function filterUri(\Psr\Http\Message\UriInterface $oldUri) { // Copy the old ?query-params and #fragment-params on top of $newBase. $copyParams = function ($newBase) use ($oldUri) { if ($oldUri->getQuery()) { - $newBase .= strpos($newBase, '?') !== FALSE ? '&' : '?'; + $newBase .= str_contains($newBase, '?') ? '&' : '?'; $newBase .= $oldUri->getQuery(); } if ($oldUri->getFragment()) { diff --git a/CRM/Utils/Hook/UnitTests.php b/CRM/Utils/Hook/UnitTests.php index 1e5d3248d1d4..b1620de7107b 100644 --- a/CRM/Utils/Hook/UnitTests.php +++ b/CRM/Utils/Hook/UnitTests.php @@ -55,7 +55,7 @@ public function setMock($mockObject): void { */ public function setHook(string $hook, $callable): void { $this->adhocHooks[$hook] = $callable; - if (strpos($hook, 'token') !== FALSE) { + if (str_contains($hook, 'token')) { unset(Civi::$statics['CRM_Contact_Tokens']['hook_tokens']); } } diff --git a/CRM/Utils/Http.php b/CRM/Utils/Http.php index a92485c08ffd..6f4aab5e6068 100644 --- a/CRM/Utils/Http.php +++ b/CRM/Utils/Http.php @@ -49,7 +49,7 @@ public static function parseCacheControl($value) { $parts = preg_split('/, */', $value); foreach ($parts as $part) { - if (strpos($part, '=') !== FALSE) { + if (str_contains($part, '=')) { list ($key, $value) = explode('=', $part, 2); $result[$key] = $value; } diff --git a/CRM/Utils/Mail.php b/CRM/Utils/Mail.php index e6ef56979f85..f82ad65cdf3f 100644 --- a/CRM/Utils/Mail.php +++ b/CRM/Utils/Mail.php @@ -359,7 +359,7 @@ public static function setEmailHeaders($params): array { } // quote FROM, if comma is detected AND is not already quoted. CRM-7053 - if (strpos($headers['From'], ',') !== FALSE) { + if (str_contains($headers['From'], ',')) { $from = explode(' <', $headers['From']); $headers['From'] = self::formatRFC822Email( $from[0], @@ -529,7 +529,7 @@ public static function formatRFC822Email($name, $email, $useQuote = FALSE) { ['\<', '\"', '\>'], $name ); - if (strpos($name, ',') !== FALSE || + if (str_contains($name, ',') || $useQuote ) { // quote the string if it has a comma diff --git a/CRM/Utils/Money.php b/CRM/Utils/Money.php index 8fccb8872e91..706985ab7254 100644 --- a/CRM/Utils/Money.php +++ b/CRM/Utils/Money.php @@ -91,7 +91,7 @@ public static function format($amount, $currency = NULL, $format = NULL, $onlyNu // amount is already converted properly, // so don't mess with it again. // @todo deprecate handling for the html tags because .... WTF - if (strpos($amount, '<') === FALSE) { + if (!str_contains($amount, '<')) { $amount = self::replaceCurrencySeparators($amount); } diff --git a/CRM/Utils/Request.php b/CRM/Utils/Request.php index 0c60dd710d7e..cdb4128cdb64 100644 --- a/CRM/Utils/Request.php +++ b/CRM/Utils/Request.php @@ -131,7 +131,7 @@ protected static function getValue($name, $method) { } // CRM-18384 - decode incorrect keys generated when & is present in url foreach (($method ?? []) as $key => $value) { - if (strpos($key, 'amp;') !== FALSE) { + if (str_contains($key, 'amp;')) { $method[str_replace('amp;', '', $key)] = $method[$key]; if (isset($method[$name])) { return $method[$name]; diff --git a/CRM/Utils/String.php b/CRM/Utils/String.php index 71deee8b36ac..d38c0436043c 100644 --- a/CRM/Utils/String.php +++ b/CRM/Utils/String.php @@ -467,7 +467,7 @@ public static function extractName($string, &$params) { $name = str_replace('\'', '', $name); // check for comma in name - if (strpos($name, ',') !== FALSE) { + if (str_contains($name, ',')) { // name has a comma - assume lname, fname [mname] $names = explode(',', $name); @@ -997,7 +997,7 @@ public static function pluralize($str) { * @return bool */ public static function stringContainsTokens(string $string) { - return strpos($string, '{') !== FALSE; + return str_contains($string, '{'); } /** diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php index 7a1a7b12396d..40ccc0dff69d 100644 --- a/CRM/Utils/System.php +++ b/CRM/Utils/System.php @@ -134,9 +134,7 @@ public static function getLinksUrl($urlVar, $includeReset = FALSE, $includeForce if ($name != $urlVar) { $name = rawurldecode($name); // check for arrays in parameters: site.php?foo[]=1&foo[]=2&foo[]=3 - if ((strpos($name, '[') !== FALSE) && - (strpos($name, ']') !== FALSE) - ) { + if (str_contains($name, '[') && str_contains($name, ']')) { $arrays[] = $qs[$i]; } else { @@ -1001,7 +999,7 @@ public static function validCallback($callback) { } if (!array_key_exists($callback, self::$_callbacks)) { - if (strpos($callback, '::') !== FALSE) { + if (str_contains($callback, '::')) { [$className, $methodName] = explode('::', $callback); $fileName = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; // ignore errors if any @@ -1555,11 +1553,8 @@ public static function baseCMSURL() { else { // Drupal setting global $civicrm_root; - if (strpos($civicrm_root, - DIRECTORY_SEPARATOR . 'sites' . - DIRECTORY_SEPARATOR . 'all' . - DIRECTORY_SEPARATOR . 'modules' - ) === FALSE + if (!str_contains($civicrm_root, + DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'all' . DIRECTORY_SEPARATOR . 'modules') ) { $startPos = strpos($civicrm_root, DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR @@ -1807,7 +1802,7 @@ public static function executeScheduledJobs() { * @return string|FALSE */ public static function evalUrl($url) { - if (!$url || strpos($url, '{') === FALSE) { + if (!$url || !str_contains($url, '{')) { return $url; } else { diff --git a/CRM/Utils/System/Backdrop.php b/CRM/Utils/System/Backdrop.php index f5228e43d801..2466d2c3dda6 100644 --- a/CRM/Utils/System/Backdrop.php +++ b/CRM/Utils/System/Backdrop.php @@ -567,7 +567,7 @@ public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE } // For Backdrop multi-site CRM-11313 - if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) { + if ($realPath && !str_contains($realPath, 'sites/all/modules/')) { preg_match('@sites/([^/]*)/modules@s', $realPath, $matches); if (!empty($matches[1])) { $_SERVER['HTTP_HOST'] = $matches[1]; diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php index 4cdc1fae9d80..22926b4f08dd 100644 --- a/CRM/Utils/System/Drupal.php +++ b/CRM/Utils/System/Drupal.php @@ -480,7 +480,7 @@ public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE define('DRUPAL_ROOT', $cmsPath); // For drupal multi-site CRM-11313 - if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) { + if ($realPath && !str_contains($realPath, 'sites/all/modules/')) { preg_match('@sites/([^/]*)/modules@s', $realPath, $matches); if (!empty($matches[1])) { $_SERVER['HTTP_HOST'] = $matches[1]; diff --git a/CRM/Utils/System/DrupalBase.php b/CRM/Utils/System/DrupalBase.php index c3a4d0531c9d..416ef5fcf972 100644 --- a/CRM/Utils/System/DrupalBase.php +++ b/CRM/Utils/System/DrupalBase.php @@ -619,10 +619,7 @@ public function getUserObject($userID) { */ public function parseDrupalSiteNameFromRoot($civicrm_root) { $siteName = NULL; - if (strpos($civicrm_root, - DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'all' . DIRECTORY_SEPARATOR . 'modules' - ) === FALSE - ) { + if (!str_contains($civicrm_root, DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'all' . DIRECTORY_SEPARATOR . 'modules')) { $startPos = strpos($civicrm_root, DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR ); diff --git a/CRM/Utils/System/Joomla.php b/CRM/Utils/System/Joomla.php index e7410d0aac5c..7d366e53c688 100644 --- a/CRM/Utils/System/Joomla.php +++ b/CRM/Utils/System/Joomla.php @@ -303,7 +303,7 @@ public function url( // Get Itemid using JInput::get() $input = Joomla\CMS\Factory::getApplication()->input; $itemIdNum = $input->get("Itemid"); - if ($itemIdNum && (strpos($path, 'civicrm/payment/ipn') === FALSE)) { + if ($itemIdNum && (!str_contains($path, 'civicrm/payment/ipn'))) { $Itemid = "{$separator}Itemid=" . $itemIdNum; } } @@ -1005,7 +1005,7 @@ public function getCiviSourceStorage():array { } // For Joomla CiviCRM Core files always live within the admistrator folder and $base_url is different on the frontend compared to the backend. - if (strpos($baseURL, 'administrator') === FALSE) { + if (!str_contains($baseURL, 'administrator')) { $userFrameworkResourceURL = $baseURL . "administrator/components/com_civicrm/civicrm/"; } else { diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 5b81d99fc07f..a493b1d44323 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1344,7 +1344,7 @@ protected static function _buildContributionTokens() { if (!empty($token['name'])) { $tokens[$token['name']] = []; } - elseif (is_string($token) && strpos($token, ':') !== FALSE) { + elseif (is_string($token) && str_contains($token, ':')) { $tokens[$token] = []; } } diff --git a/CRM/Utils/Type.php b/CRM/Utils/Type.php index ea610939de19..34b138c095a9 100644 --- a/CRM/Utils/Type.php +++ b/CRM/Utils/Type.php @@ -248,7 +248,7 @@ public static function escape($data, $type, $abort = TRUE) { case 'Country': case 'StateProvince': // Handle multivalued data in delimited or array format - if (is_array($data) || (strpos($data, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE)) { + if (is_array($data) || (str_contains($data, CRM_Core_DAO::VALUE_SEPARATOR))) { $valid = TRUE; foreach (CRM_Utils_Array::explodePadded($data) as $item) { if (!CRM_Utils_Rule::positiveInteger($item)) {