Skip to content

refactor: reduce_multiples() and fix user guide #9099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading