Skip to content

Commit 82dbd91

Browse files
authored
Merge pull request #9099 from kenjis/refactor-reduce_multiples
refactor: reduce_multiples() and fix user guide
2 parents 1d3336b + c1687fa commit 82dbd91

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

system/Helpers/text_helper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,10 @@ function reduce_double_slashes(string $str): string
526526
*/
527527
function reduce_multiples(string $str, string $character = ',', bool $trim = false): string
528528
{
529-
$str = preg_replace('#' . preg_quote($character, '#') . '{2,}#', $character, $str);
529+
$pattern = '#' . preg_quote($character, '#') . '{2,}#';
530+
$str = preg_replace($pattern, $character, $str);
530531

531-
return ($trim) ? trim($str, $character) : $str;
532+
return $trim ? trim($str, $character) : $str;
532533
}
533534
}
534535

tests/system/Helpers/TextHelperTest.php

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use InvalidArgumentException;
18+
use PHPUnit\Framework\Attributes\DataProvider;
1819
use PHPUnit\Framework\Attributes\Group;
1920

2021
/**
@@ -82,24 +83,42 @@ public function testReduceDoubleSlashes(): void
8283
}
8384
}
8485

85-
public function testReduceMultiples(): void
86+
#[DataProvider('provideReduceMultiples')]
87+
public function testReduceMultiples(string $str, string $expected): void
8688
{
87-
$strs = [
88-
'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
89-
'Ringo, John, Paul,,' => 'Ringo, John, Paul,',
90-
];
89+
$this->assertSame($expected, reduce_multiples($str));
90+
}
9191

92-
foreach ($strs as $str => $expect) {
93-
$this->assertSame($expect, reduce_multiples($str));
94-
}
95-
$strs = [
96-
'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
97-
'Ringo, John, Paul,,' => 'Ringo, John, Paul',
92+
/**
93+
* @return iterable<string, list<string>>
94+
*/
95+
public static function provideReduceMultiples(): iterable
96+
{
97+
yield from [
98+
// string, expected
99+
'double commas' => ['Fred, Bill,, Joe, Jimmy', 'Fred, Bill, Joe, Jimmy'],
100+
'double commas at last' => ['Ringo, John, Paul,,', 'Ringo, John, Paul,'],
101+
'commas at first and last' => [',Fred, Bill,, Joe, Jimmy,', ',Fred, Bill, Joe, Jimmy,'],
98102
];
103+
}
99104

100-
foreach ($strs as $str => $expect) {
101-
$this->assertSame($expect, reduce_multiples($str, ',', true));
102-
}
105+
#[DataProvider('provideReduceMultiplesWithTrim')]
106+
public function testReduceMultiplesWithTrim(string $str, string $expected): void
107+
{
108+
$this->assertSame($expected, reduce_multiples($str, ',', true));
109+
}
110+
111+
/**
112+
* @return iterable<string, list<string>>
113+
*/
114+
public static function provideReduceMultiplesWithTrim(): iterable
115+
{
116+
yield from [
117+
// string, expected
118+
'double commas' => ['Fred, Bill,, Joe, Jimmy', 'Fred, Bill, Joe, Jimmy'],
119+
'double commas at last' => ['Ringo, John, Paul,,', 'Ringo, John, Paul'],
120+
'commas at first and last' => [',Fred, Bill,, Joe, Jimmy,', 'Fred, Bill, Joe, Jimmy'],
121+
];
103122
}
104123

105124
public function testRandomString(): void

user_guide_src/source/helpers/text_helper.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ The following functions are available:
127127
and handle string inputs. This however makes it just an
128128
alias for ``stripslashes()``.
129129

130-
.. php:function:: reduce_multiples($str[, $character = ''[, $trim = false]])
130+
.. php:function:: reduce_multiples($str[, $character = ','[, $trim = false]])
131131
132132
:param string $str: Text to search in
133133
:param string $character: Character to reduce
@@ -140,7 +140,7 @@ The following functions are available:
140140

141141
.. literalinclude:: text_helper/009.php
142142

143-
If the third parameter is set to true it will remove occurrences of the
143+
If the third parameter is set to ``true``, it will remove occurrences of the
144144
character at the beginning and the end of the string. Example:
145145

146146
.. literalinclude:: text_helper/010.php
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

33
$string = 'Fred, Bill,, Joe, Jimmy';
4-
$string = reduce_multiples($string, ','); // results in "Fred, Bill, Joe, Jimmy"
4+
$string = reduce_multiples($string); // results in "Fred, Bill, Joe, Jimmy"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

33
$string = ',Fred, Bill,, Joe, Jimmy,';
4-
$string = reduce_multiples($string, ', ', true); // results in "Fred, Bill, Joe, Jimmy"
4+
$string = reduce_multiples($string, ',', true); // results in "Fred, Bill, Joe, Jimmy"

0 commit comments

Comments
 (0)