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

[REF] CRM/Utils - Use str_contains instead of strpos #32156

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CRM/Utils/Check/Component/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= '<tr>
<td>' . $job['name'] . ' </td>
Expand Down Expand Up @@ -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', '<')) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
4 changes: 2 additions & 2 deletions CRM/Utils/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '-')
);
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/GuzzleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Hook/UnitTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions CRM/Utils/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected static function getValue($name, $method) {
}
// CRM-18384 - decode incorrect keys generated when &amp; 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];
Expand Down
4 changes: 2 additions & 2 deletions CRM/Utils/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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, '{');
}

/**
Expand Down
15 changes: 5 additions & 10 deletions CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/System/Backdrop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/System/Drupal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
5 changes: 1 addition & 4 deletions CRM/Utils/System/DrupalBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
4 changes: 2 additions & 2 deletions CRM/Utils/System/Joomla.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
}
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down