diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index ae6b6f111c81..3de105736f57 100644 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -526,9 +526,10 @@ function reduce_double_slashes(string $str): string */ function reduce_multiples(string $str, string $character = ',', bool $trim = false): string { - $str = preg_replace('#' . preg_quote($character, '#') . '{2,}#', $character, $str); + $pattern = '#' . preg_quote($character, '#') . '{2,}#'; + $str = preg_replace($pattern, $character, $str); - return ($trim) ? trim($str, $character) : $str; + return $trim ? trim($str, $character) : $str; } } diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index e3acd7bb9c50..3ba265f3a1b5 100644 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -15,6 +15,7 @@ use CodeIgniter\Test\CIUnitTestCase; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; /** @@ -82,24 +83,42 @@ public function testReduceDoubleSlashes(): void } } - public function testReduceMultiples(): void + #[DataProvider('provideReduceMultiples')] + public function testReduceMultiples(string $str, string $expected): void { - $strs = [ - 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy', - 'Ringo, John, Paul,,' => 'Ringo, John, Paul,', - ]; + $this->assertSame($expected, reduce_multiples($str)); + } - foreach ($strs as $str => $expect) { - $this->assertSame($expect, reduce_multiples($str)); - } - $strs = [ - 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy', - 'Ringo, John, Paul,,' => 'Ringo, John, Paul', + /** + * @return iterable> + */ + public static function provideReduceMultiples(): iterable + { + yield from [ + // string, expected + 'double commas' => ['Fred, Bill,, Joe, Jimmy', 'Fred, Bill, Joe, Jimmy'], + 'double commas at last' => ['Ringo, John, Paul,,', 'Ringo, John, Paul,'], + 'commas at first and last' => [',Fred, Bill,, Joe, Jimmy,', ',Fred, Bill, Joe, Jimmy,'], ]; + } - foreach ($strs as $str => $expect) { - $this->assertSame($expect, reduce_multiples($str, ',', true)); - } + #[DataProvider('provideReduceMultiplesWithTrim')] + public function testReduceMultiplesWithTrim(string $str, string $expected): void + { + $this->assertSame($expected, reduce_multiples($str, ',', true)); + } + + /** + * @return iterable> + */ + public static function provideReduceMultiplesWithTrim(): iterable + { + yield from [ + // string, expected + 'double commas' => ['Fred, Bill,, Joe, Jimmy', 'Fred, Bill, Joe, Jimmy'], + 'double commas at last' => ['Ringo, John, Paul,,', 'Ringo, John, Paul'], + 'commas at first and last' => [',Fred, Bill,, Joe, Jimmy,', 'Fred, Bill, Joe, Jimmy'], + ]; } public function testRandomString(): void diff --git a/user_guide_src/source/helpers/text_helper.rst b/user_guide_src/source/helpers/text_helper.rst index fb4e942338c4..de836253b190 100644 --- a/user_guide_src/source/helpers/text_helper.rst +++ b/user_guide_src/source/helpers/text_helper.rst @@ -127,7 +127,7 @@ The following functions are available: and handle string inputs. This however makes it just an alias for ``stripslashes()``. -.. php:function:: reduce_multiples($str[, $character = ''[, $trim = false]]) +.. php:function:: reduce_multiples($str[, $character = ','[, $trim = false]]) :param string $str: Text to search in :param string $character: Character to reduce @@ -140,7 +140,7 @@ The following functions are available: .. literalinclude:: text_helper/009.php - If the third parameter is set to true it will remove occurrences of the + If the third parameter is set to ``true``, it will remove occurrences of the character at the beginning and the end of the string. Example: .. literalinclude:: text_helper/010.php diff --git a/user_guide_src/source/helpers/text_helper/009.php b/user_guide_src/source/helpers/text_helper/009.php index 60f35bc49ead..073efcee9319 100644 --- a/user_guide_src/source/helpers/text_helper/009.php +++ b/user_guide_src/source/helpers/text_helper/009.php @@ -1,4 +1,4 @@