From fd088c3c0cae9bbcc2413c8955058b7a30be465d Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 16 Oct 2024 18:04:21 +0200 Subject: [PATCH] Optimize isList (#285) --- src/Assert.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Assert.php b/src/Assert.php index a427420..cbf8ed8 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -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.' + ); } }