CRM_Utils_String - Deprecate startsWith and endsWith
authorcolemanw <coleman@civicrm.org>
Wed, 4 Oct 2023 12:51:07 +0000 (08:51 -0400)
committercolemanw <coleman@civicrm.org>
Wed, 4 Oct 2023 18:40:20 +0000 (14:40 -0400)
12 files changed:
CRM/Activity/BAO/Activity.php
CRM/Extension/Mapper.php
CRM/Utils/EnglishNumber.php
CRM/Utils/String.php
CRM/Utils/Token.php
Civi/Api4/Utils/ReflectionUtils.php
Civi/Test/HttpTestTrait.php
ext/authx/authx.php
ext/legacycustomsearches/CRM/Utils/QueryFormatter.php
tests/phpunit/CRM/Member/Form/MembershipTest.php
tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php
tests/phpunit/CRM/Utils/StringTest.php

index 5d74041ed6fedefa1a36499adbc6571c88caa947..8405e21c4bfeadadedbda92044973df5cd9b1617 100644 (file)
@@ -2335,8 +2335,8 @@ INNER JOIN  civicrm_option_group grp ON (grp.id = option_group_id AND grp.name =
     $params['rowCount'] = $params['rp'];
     $params['sort'] = $params['sortBy'] ?? NULL;
     $params['caseId'] = NULL;
-    $context = $params['context'] ?? NULL;
-    $showContactOverlay = !CRM_Utils_String::startsWith($context, "dashlet");
+    $context = $params['context'] ?? '';
+    $showContactOverlay = !str_starts_with($context, "dashlet");
     $activityTypeInfo = civicrm_api3('OptionValue', 'get', [
       'option_group_id' => "activity_type",
       'options' => ['limit' => 0],
index 8f0747387e9cf6d52eeddcfa98a39d215a6e1ec0..62fdd94ede8aab4a1ba411d892017613318e9f50 100644 (file)
@@ -383,7 +383,7 @@ class CRM_Extension_Mapper {
   public function getKeysByPath($pattern) {
     $keys = [];
 
-    if (CRM_Utils_String::endsWith($pattern, '*')) {
+    if (str_ends_with($pattern, '*')) {
       $prefix = rtrim($pattern, '*');
       foreach ($this->container->getKeys() as $key) {
         $path = CRM_Utils_File::addTrailingSlash($this->container->getPath($key));
index c95bb6af21b6e4e34fa0fcb9c375d2c4a4e72852..37a8663ccef99eed366f413ca84b3474397e55d4 100644 (file)
@@ -136,14 +136,14 @@ class CRM_Utils_EnglishNumber {
     $strBuf = strtolower(str_replace('-', '', $english));
 
     foreach (self::$intervalsOfTen as $num => $name) {
-      if (CRM_Utils_String::startsWith($strBuf, strtolower($name))) {
+      if (str_starts_with($strBuf, strtolower($name))) {
         $intBuf += 10 * $num;
         $strBuf = substr($strBuf, strlen($name));
         break;
       }
     }
     foreach (array_reverse(self::$lowNumbers, TRUE) as $num => $name) {
-      if (CRM_Utils_String::startsWith($strBuf, strtolower($name))) {
+      if (str_starts_with($strBuf, strtolower($name))) {
         $intBuf += $num;
         $strBuf = substr($strBuf, strlen($name));
         break;
index 920edf7a842b570f6280776269be6e7330ced5e3..a00905b2388c3496f72dd64c51a5259f958b202d 100644 (file)
@@ -876,7 +876,7 @@ class CRM_Utils_String {
   }
 
   /**
-   * Determine if $string starts with $fragment.
+   * @deprecated
    *
    * @param string $string
    *   The long string.
@@ -885,15 +885,12 @@ class CRM_Utils_String {
    * @return bool
    */
   public static function startsWith($string, $fragment) {
-    if ($fragment === '') {
-      return TRUE;
-    }
-    $len = strlen($fragment ?? '');
-    return substr(($string ?? ''), 0, $len) === $fragment;
+    CRM_Core_Error::deprecatedFunctionWarning('str_starts_with');
+    return str_starts_with((string) $string, (string) $fragment);
   }
 
   /**
-   * Determine if $string ends with $fragment.
+   * @deprecated
    *
    * @param string $string
    *   The long string.
@@ -902,11 +899,8 @@ class CRM_Utils_String {
    * @return bool
    */
   public static function endsWith($string, $fragment) {
-    if ($fragment === '') {
-      return TRUE;
-    }
-    $len = strlen($fragment ?? '');
-    return substr(($string ?? ''), -1 * $len) === $fragment;
+    CRM_Core_Error::deprecatedFunctionWarning('str_ends_with');
+    return str_ends_with((string) $string, (string) $fragment);
   }
 
   /**
@@ -920,7 +914,7 @@ class CRM_Utils_String {
     $patterns = (array) $patterns;
     $result = [];
     foreach ($patterns as $pattern) {
-      if (!\CRM_Utils_String::endsWith($pattern, '*')) {
+      if (!str_ends_with($pattern, '*')) {
         if ($allowNew || in_array($pattern, $allStrings)) {
           $result[] = $pattern;
         }
@@ -928,7 +922,7 @@ class CRM_Utils_String {
       else {
         $prefix = rtrim($pattern, '*');
         foreach ($allStrings as $key) {
-          if (\CRM_Utils_String::startsWith($key, $prefix)) {
+          if (str_starts_with($key, $prefix)) {
             $result[] = $key;
           }
         }
index f2ef5ce9957473a8dfccb1288e8a868b8aadd4b6..898f0aae13ccb93ef81d2542850088e1dc8dc8b3 100644 (file)
@@ -1585,7 +1585,7 @@ class CRM_Utils_Token {
       default:
         if (in_array($token, $supportedTokens)) {
           $value = $membership[$token];
-          if (CRM_Utils_String::endsWith($token, '_date')) {
+          if (str_ends_with($token, '_date')) {
             $value = CRM_Utils_Date::customFormat($value);
           }
         }
@@ -1746,7 +1746,7 @@ class CRM_Utils_Token {
         $value = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $token, $value);
       }
     }
-    elseif ($value && CRM_Utils_String::endsWith($token, '_date')) {
+    elseif ($value && str_ends_with($token, '_date')) {
       $value = CRM_Utils_Date::customFormat($value);
     }
     return $value;
index cc9b78d189121e34245fd221c6f5d4e86a771672..446d06462111336b4cab6bdc4d91867387b906e0 100644 (file)
@@ -206,7 +206,7 @@ class ReflectionUtils {
 
       yield from [];
       foreach ($clazz->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) {
-        if (\CRM_Utils_String::startsWith($m->getName(), $prefix)) {
+        if (str_starts_with($m->getName(), $prefix)) {
           yield $m;
         }
       }
index e3384ecbec2a3d763a84710695ed95aaf4705a3a..e74ccd424192a2e17153296adbf272599c0306e3 100644 (file)
@@ -75,7 +75,7 @@ trait HttpTestTrait {
    * @return mixed
    */
   protected function callApi4AjaxSuccess(string $entity, string $action, $params = []) {
-    $method = \CRM_Utils_String::startsWith($action, 'get') ? 'GET' : 'POST';
+    $method = str_starts_with($action, 'get') ? 'GET' : 'POST';
     $response = $this->createGuzzle()->request($method, "civicrm/ajax/api4/$entity/$action", [
       'headers' => ['X-Requested-With' => 'XMLHttpRequest'],
       // This should probably be 'form_params', but 'query' is more representative of frontend.
@@ -99,7 +99,7 @@ trait HttpTestTrait {
    * @return mixed
    */
   protected function callApi4AjaxError(string $entity, string $action, $params = []) {
-    $method = \CRM_Utils_String::startsWith($action, 'get') ? 'GET' : 'POST';
+    $method = str_starts_with($action, 'get') ? 'GET' : 'POST';
     $response = $this->createGuzzle()->request($method, "civicrm/ajax/api4/$entity/$action", [
       'headers' => ['X-Requested-With' => 'XMLHttpRequest'],
       // This should probably be 'form_params', but 'query' is more representative of frontend.
index 01091f9b2bf8eee8fe7e4250fe7de899dbe13d30..2a4e8c0dc82acbd1b6e6b1d36c02a7f5c3326323 100644 (file)
@@ -73,7 +73,7 @@ function _authx_redact(array $keys) {
 function _authx_reload($route, $queryString) {
   parse_str($queryString, $query);
   foreach (array_keys($query) as $key) {
-    if (CRM_Utils_String::startsWith($key, '_authx')) {
+    if (str_starts_with($key, '_authx')) {
       unset($query[$key]);
     }
   }
index e11fdced70cecd0d2d8d1a3622ee1f0039ee6349..beedd4efa42b4cd3c7ae6c8c7f710c3cf74d4899 100644 (file)
@@ -283,7 +283,7 @@ class CRM_Utils_QueryFormatter {
     //Return if searched string ends with an unsupported operator.
     //Or if the string contains an invalid joint occurrence of operators.
     foreach ($operators as $val) {
-      if ($text == '@' || CRM_Utils_String::endsWith($text, $val) || preg_match("/[{$expression}]{2,}/", $text)) {
+      if ($text == '@' || str_ends_with($text, $val) || preg_match("/[{$expression}]{2,}/", $text)) {
         $csid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'CRM_Contact_Form_Search_Custom_FullText', 'value', 'name');
         $url = CRM_Utils_System::url("civicrm/contact/search/custom", "csid={$csid}&reset=1");
         $operators = implode("', '", $operators);
index e9e0599730b2bec885a2a615be9b5c6596129726..96c65ba18aa2984e753e5703a809795347e4f06a 100644 (file)
@@ -1319,7 +1319,7 @@ Expires: ',
     $discountedItems = 0;
     foreach ($lineItemResult['values'] as $lineItem) {
       $this->assertEquals(round($lineItem['line_total'] * .1, 2), $lineItem['tax_amount']);
-      if (CRM_Utils_String::startsWith($lineItem['label'], 'Long Haired Goat')) {
+      if (str_starts_with($lineItem['label'], 'Long Haired Goat')) {
         $this->assertEquals(15.0, $lineItem['line_total']);
         $this->assertEquals('Long Haired Goat - one leg free!', $lineItem['label']);
         $discountedItems++;
index d67e39d640c0e9cd2b7338374237bed77362b000..2c3999d35f4dbd834604a88c7efc2319a686a9cc 100644 (file)
@@ -68,7 +68,7 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase {
 
       // Form an expected array replacing tokens for each contact.
       foreach ($tokens as $key => $val) {
-        if (CRM_Utils_String::endsWith($val, '_date')) {
+        if (str_ends_with($val, '_date')) {
           $formattedDate = CRM_Utils_Date::customFormat($params[$val]);
           $expected[$contactId][$val] = "{$key} - {$formattedDate}";
         }
index efac06977d9181cf00f7b7533cc399771051bdb5..36c5b0140a1f94859ab41110b4e6b295fca75e45 100644 (file)
@@ -191,43 +191,6 @@ class CRM_Utils_StringTest extends CiviUnitTestCase {
     $this->assertSame($expected, $actual);
   }
 
-  /**
-   * Data provider for checking how strings start and end.
-   *
-   * @noinspection SpellCheckingInspection
-   */
-  public function startEndCases(): array {
-    $cases = [];
-    $cases[] = ['startsWith', 'foo', '', TRUE];
-    $cases[] = ['startsWith', 'foo', 'f', TRUE];
-    $cases[] = ['startsWith', 'foo', 'fo', TRUE];
-    $cases[] = ['startsWith', 'foo', 'foo', TRUE];
-    $cases[] = ['startsWith', 'foo', 'fooo', FALSE];
-    $cases[] = ['startsWith', 'foo', 'o', FALSE];
-    $cases[] = ['endsWith', 'foo', 'f', FALSE];
-    $cases[] = ['endsWith', 'foo', '', TRUE];
-    $cases[] = ['endsWith', 'foo', 'o', TRUE];
-    $cases[] = ['endsWith', 'foo', 'oo', TRUE];
-    $cases[] = ['endsWith', 'foo', 'foo', TRUE];
-    $cases[] = ['endsWith', 'foo', 'fooo', FALSE];
-    $cases[] = ['endsWith', 'foo*', '*', TRUE];
-    return $cases;
-  }
-
-  /**
-   * @param string $function
-   *   One of: 'startsWith' or 'endsWith'.
-   * @param $string
-   * @param $fragment
-   * @param $expectedResult
-   *
-   * @dataProvider startEndCases
-   */
-  public function testStartEndWith(string $function, $string, $fragment, $expectedResult): void {
-    $actualResult = CRM_Utils_String::$function($string, $fragment);
-    $this->assertEquals($expectedResult, $actualResult, "Checking $function($string,$fragment)");
-  }
-
   public function wildcardCases(): array {
     $cases = [];
     $cases[] = ['*', ['foo.bar.1', 'foo.bar.2', 'foo.whiz', 'bang.bang']];