From 223f2865d53f5be6fbc8c3ab34a6019abe0b5adc Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 2 Aug 2024 10:32:14 +0900 Subject: [PATCH 1/8] test: refactor test code with DataProvider --- tests/system/Helpers/TextHelperTest.php | 39 ++++++++++++++++--------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index e3acd7bb9c50..7709d027e156 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,34 @@ 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', + 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,'], ]; + } - 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)); + } + + 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'], + ]; } public function testRandomString(): void From d40b3e7de2a33db342638765ecb4be636b850da1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 2 Aug 2024 10:33:20 +0900 Subject: [PATCH 2/8] refactor: reduce_multiples() To suppress PHPStan false positive error. --- system/Helpers/text_helper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index ae6b6f111c81..d2f9af9dc6ba 100644 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -526,7 +526,8 @@ 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; } From 0c4c6565ee05ace8c245dcc217f611d9997d7a84 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 2 Aug 2024 10:39:21 +0900 Subject: [PATCH 3/8] test: add test cases --- tests/system/Helpers/TextHelperTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index 7709d027e156..c82b865876e3 100644 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -93,8 +93,9 @@ 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,'], + '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,'], ]; } @@ -108,8 +109,9 @@ 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'], + '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'], ]; } From 892cbacdf9f8b6f258dc3820060acf4e9d91b45d Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 2 Aug 2024 10:43:04 +0900 Subject: [PATCH 4/8] docs: fix incorrect default value and sample code for reduce_multiples() --- user_guide_src/source/helpers/text_helper.rst | 2 +- user_guide_src/source/helpers/text_helper/010.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/helpers/text_helper.rst b/user_guide_src/source/helpers/text_helper.rst index fb4e942338c4..0f181a155334 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 diff --git a/user_guide_src/source/helpers/text_helper/010.php b/user_guide_src/source/helpers/text_helper/010.php index fd79b8f639c1..7c69ac5a95a0 100644 --- a/user_guide_src/source/helpers/text_helper/010.php +++ b/user_guide_src/source/helpers/text_helper/010.php @@ -1,4 +1,4 @@ Date: Fri, 2 Aug 2024 10:44:03 +0900 Subject: [PATCH 5/8] docs: decorate text --- user_guide_src/source/helpers/text_helper.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/helpers/text_helper.rst b/user_guide_src/source/helpers/text_helper.rst index 0f181a155334..de836253b190 100644 --- a/user_guide_src/source/helpers/text_helper.rst +++ b/user_guide_src/source/helpers/text_helper.rst @@ -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 From d30c0613a503aea1eeba2e9acc147ce681d7a2f1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 2 Aug 2024 10:44:42 +0900 Subject: [PATCH 6/8] docs: remove uneeded 2nd param in sample code --- user_guide_src/source/helpers/text_helper/009.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 @@ Date: Fri, 2 Aug 2024 10:57:51 +0900 Subject: [PATCH 7/8] test: add @return --- tests/system/Helpers/TextHelperTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index c82b865876e3..3ba265f3a1b5 100644 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -89,6 +89,9 @@ public function testReduceMultiples(string $str, string $expected): void $this->assertSame($expected, reduce_multiples($str)); } + /** + * @return iterable> + */ public static function provideReduceMultiples(): iterable { yield from [ @@ -105,6 +108,9 @@ public function testReduceMultiplesWithTrim(string $str, string $expected): void $this->assertSame($expected, reduce_multiples($str, ',', true)); } + /** + * @return iterable> + */ public static function provideReduceMultiplesWithTrim(): iterable { yield from [ From c1687fa333e81f23bb28d88fd182bc9dcccdb049 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 2 Aug 2024 11:07:30 +0900 Subject: [PATCH 8/8] refactor: remove unneeded () --- system/Helpers/text_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index d2f9af9dc6ba..3de105736f57 100644 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -529,7 +529,7 @@ function reduce_multiples(string $str, string $character = ',', bool $trim = fal $pattern = '#' . preg_quote($character, '#') . '{2,}#'; $str = preg_replace($pattern, $character, $str); - return ($trim) ? trim($str, $character) : $str; + return $trim ? trim($str, $character) : $str; } }