Skip to content

Commit ed530c9

Browse files
committed
Add missing Arr tests and fix issues identified
- Fix bug in `Arr::sortDesc()` where keys are not preserved correctly - In `Arr::trim()`, remove keys from the array if removing empty values - In `Arr::toScalars()`, preserve `null` values
1 parent 3e9892c commit ed530c9

File tree

5 files changed

+618
-44
lines changed

5 files changed

+618
-44
lines changed

src/Utility/Arr.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public static function sortDesc(
197197
bool $preserveKeys = false,
198198
int $flags = \SORT_REGULAR
199199
): array {
200-
if (!$preserveKeys) {
200+
if ($preserveKeys) {
201201
arsort($array, $flags);
202202
return $array;
203203
}
@@ -560,8 +560,8 @@ public static function implode(string $separator, iterable $array): string
560560
}
561561

562562
/**
563-
* Remove whitespace from the beginning and end of each value in an array
564-
* of strings and Stringables before optionally removing empty strings
563+
* Remove whitespace from the beginning and end of each value in an array of
564+
* strings and Stringables before optionally removing empty strings
565565
*
566566
* @template TKey of array-key
567567
* @template TValue of int|float|string|bool|Stringable|null
@@ -570,7 +570,7 @@ public static function implode(string $separator, iterable $array): string
570570
* @param string|null $characters Optionally specify characters to remove
571571
* instead of whitespace.
572572
*
573-
* @return array<TKey,string>
573+
* @return ($removeEmpty is false ? array<TKey,string> : list<string>)
574574
*/
575575
public static function trim(
576576
iterable $array,
@@ -582,7 +582,10 @@ public static function trim(
582582
$characters === null
583583
? trim((string) $value)
584584
: trim((string) $value, $characters);
585-
if ($removeEmpty && $value === '') {
585+
if ($removeEmpty) {
586+
if ($value !== '') {
587+
$trimmed[] = $value;
588+
}
586589
continue;
587590
}
588591
$trimmed[$key] = $value;
@@ -641,7 +644,7 @@ public static function upper(iterable $array): array
641644
public static function toScalars(iterable $array): array
642645
{
643646
foreach ($array as $key => $value) {
644-
if (!is_scalar($value)) {
647+
if ($value !== null && !is_scalar($value)) {
645648
if (Test::isStringable($value)) {
646649
$value = (string) $value;
647650
} elseif ($value instanceof Jsonable) {

tests/fixtures/Concern/Immutable/MyImmutableClass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MyImmutableClass
5757

5858
public function __construct()
5959
{
60-
$this->Obj = new \stdClass();
60+
$this->Obj = new stdClass();
6161
$this->Coll = new ImmutableCollection();
6262
}
6363

tests/unit/Concern/ImmutableTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Lkrms\Tests\Concern\Immutable\MyImmutableClass;
66
use Lkrms\Tests\TestCase;
7+
use stdClass;
78

89
final class ImmutableTest extends TestCase
910
{
@@ -44,7 +45,7 @@ public function testWithPropertyValue(): void
4445
->with('A', 1) // Changes to $f should be no-ops
4546
->with('Obj', $obj);
4647

47-
$g = $f->with('Coll', $f->Coll->set('g', new \stdClass()));
48+
$g = $f->with('Coll', $f->Coll->set('g', new stdClass()));
4849

4950
$this->assertNotSame($a, $b);
5051
$this->assertNotEquals($a, $b);
@@ -85,7 +86,7 @@ public function testWithPropertyValue(): void
8586
];
8687
$A->Obj->A = 'aa';
8788
$A->Obj->B = 'bb';
88-
$A->Coll = $A->Coll->set('g', new \stdClass());
89+
$A->Coll = $A->Coll->set('g', new stdClass());
8990

9091
$this->assertEquals($A, $g);
9192
}

0 commit comments

Comments
 (0)