From 6fff13241ab8c03712cd489962a9bbadf9159622 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Thu, 30 Mar 2023 12:47:13 +0100 Subject: [PATCH] Optimize isList --- src/Assert.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Assert.php b/src/Assert.php index a427420..dfb96a3 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -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.' + ); } }