Skip to content

Commit

Permalink
Merge pull request #9099 from kenjis/refactor-reduce_multiples
Browse files Browse the repository at this point in the history
refactor: reduce_multiples() and fix user guide
  • Loading branch information
kenjis committed Aug 2, 2024
2 parents 1d3336b + c1687fa commit 82dbd91
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
5 changes: 3 additions & 2 deletions system/Helpers/text_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
47 changes: 33 additions & 14 deletions tests/system/Helpers/TextHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CodeIgniter\Test\CIUnitTestCase;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down Expand Up @@ -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<string, list<string>>
*/
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<string, list<string>>
*/
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
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/helpers/text_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/helpers/text_helper/009.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

$string = 'Fred, Bill,, Joe, Jimmy';
$string = reduce_multiples($string, ','); // results in "Fred, Bill, Joe, Jimmy"
$string = reduce_multiples($string); // results in "Fred, Bill, Joe, Jimmy"
2 changes: 1 addition & 1 deletion user_guide_src/source/helpers/text_helper/010.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

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

0 comments on commit 82dbd91

Please sign in to comment.