Skip to content

Commit dc701e3

Browse files
authored
refactor(support): add validation and fix edge cases in array helpers (#1779)
1 parent 05165bd commit dc701e3

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

packages/support/src/Arr/IsIterable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function key(): string|int|null
2424

2525
public function valid(): bool
2626
{
27-
return $this->current() !== false;
27+
return $this->key() !== null;
2828
}
2929

3030
public function rewind(): void

packages/support/src/Arr/functions.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,12 @@ function combine(iterable $array, iterable $values): array
516516
$array = to_array($array);
517517
$values = to_array($values);
518518

519+
if (count($array) !== count($values)) {
520+
throw new InvalidArgumentException(
521+
sprintf('Cannot combine arrays of different lengths (%d keys vs %d values)', count($array), count($values)),
522+
);
523+
}
524+
519525
return array_combine($array, $values);
520526
}
521527

@@ -792,6 +798,10 @@ function get_by_key(iterable $array, int|string $key, mixed $default = null): mi
792798
: explode('.', $key);
793799

794800
foreach ($keys as $key) {
801+
if (! is_array($value) && ! $value instanceof \ArrayAccess) {
802+
return $default;
803+
}
804+
795805
if (! isset($value[$key])) {
796806
return $default;
797807
}
@@ -843,11 +853,12 @@ function has_key(iterable $array, int|string $key): bool
843853
*/
844854
function contains(iterable $array, mixed $search): bool
845855
{
856+
$array = to_array($array);
846857
$search = $search instanceof Closure
847858
? $search
848859
: static fn (mixed $value) => $value === $search;
849860

850-
return namespace\first(to_array($array), $search) !== null;
861+
return array_any($array, static fn ($value, $key) => $search($value, $key));
851862
}
852863

853864
/**

0 commit comments

Comments
 (0)