Skip to content

Commit

Permalink
Optimize isList
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-tekiela committed Oct 16, 2024
1 parent 3148f0b commit 6fff132
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -1875,17 +1875,24 @@ public static function isList($array, $message = '')
);
}

if ($array === \array_values($array)) {
return;
}

$nextKey = -1;
foreach ($array as $k => $v) {
if ($k !== ++$nextKey) {
if (\function_exists('array_is_list')) {
if (!\array_is_list($array)) {
static::reportInvalidArgument(
$message ?: 'Expected list - non-associative array.'
);
}
return;
}

if ([] === $array) {
return;
}

$keys = array_keys($array);
if (array_keys($keys) !== $keys) {
static::reportInvalidArgument(
$message ?: 'Expected list - non-associative array.'
);
}
}

Expand Down

0 comments on commit 6fff132

Please # to comment.