From 3803509f008518cdf55a1ce44332495703e43c45 Mon Sep 17 00:00:00 2001 From: "A. B. M. Mahmudul Hasan" Date: Sat, 18 Jul 2026 09:17:16 +0600 Subject: [PATCH] updated std --- src/Array/ArraySingle.php | 13 ++++++------- src/Config/Concerns/BaseConfigTrait.php | 4 ++-- tests/Feature/ArraySingleTest.php | 5 +++++ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Array/ArraySingle.php b/src/Array/ArraySingle.php index abeca58..3780323 100644 --- a/src/Array/ArraySingle.php +++ b/src/Array/ArraySingle.php @@ -439,18 +439,17 @@ public static function maxBy(array $array, callable $callback): mixed */ public static function median(array $array): float|int { - $values = array_values( - array_filter( - $array, - static fn(mixed $value): bool => is_int($value) || is_float($value) || (is_string($value) && is_numeric($value)), - ), - ); + $values = []; + foreach ($array as $value) { + if (is_int($value) || is_float($value) || (is_string($value) && is_numeric($value))) { + $values[] = (float) $value; + } + } if ($values === []) { return 0; } - $values = array_map(static fn(mixed $value): float => (float) $value, $values); sort($values, SORT_NUMERIC); $count = count($values); $mid = intdiv($count, 2); diff --git a/src/Config/Concerns/BaseConfigTrait.php b/src/Config/Concerns/BaseConfigTrait.php index 38412be..1ef1570 100644 --- a/src/Config/Concerns/BaseConfigTrait.php +++ b/src/Config/Concerns/BaseConfigTrait.php @@ -70,7 +70,7 @@ public function append(string $key, mixed $value): bool */ public function changed(string $snapshot = 'default'): bool { - if (!array_key_exists($snapshot, $this->snapshots)) { + if (!isset($this->snapshots[$snapshot])) { return true; } @@ -501,7 +501,7 @@ public function replace(array $items): bool public function restore(string $name = 'default'): bool { $this->assertWritable(); - if (!array_key_exists($name, $this->snapshots)) { + if (!isset($this->snapshots[$name])) { return false; } diff --git a/tests/Feature/ArraySingleTest.php b/tests/Feature/ArraySingleTest.php index 00a1a9c..a937e00 100644 --- a/tests/Feature/ArraySingleTest.php +++ b/tests/Feature/ArraySingleTest.php @@ -44,6 +44,11 @@ expect(ArraySingle::avg($values))->toBe(4); }); +it('ignores non-numeric values when calculating median', function () { + expect(ArraySingle::median([1, '2', 7.5, 'ignore', null]))->toBe(2.0) + ->and(ArraySingle::median(['ignore', null]))->toBe(0); +}); + it('searches an array for a callback condition', function () { $data = [1, 2, 3, 4]; $key = ArraySingle::search($data, fn ($value) => $value === 3);