Skip to content

Commit

Permalink
Optimize isList (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-tekiela authored Oct 16, 2024
1 parent 3148f0b commit fd088c3
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -1875,17 +1875,25 @@ 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() === $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 fd088c3

Please sign in to comment.