Skip to content

Commit

Permalink
Optimize isList
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-tekiela committed Mar 30, 2023
1 parent 11cb219 commit d550750
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 @@ -1828,17 +1828,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 d550750

Please sign in to comment.