Skip to content

Commit 20f8658

Browse files
leverage native php 8.1 array_is_list (#41347)
1 parent c427100 commit 20f8658

File tree

6 files changed

+18
-12
lines changed

6 files changed

+18
-12
lines changed

src/Illuminate/Collections/Arr.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,7 @@ public static function hasAny($array, $keys)
405405
*/
406406
public static function isAssoc(array $array)
407407
{
408-
$keys = array_keys($array);
409-
410-
return array_keys($keys) !== $keys;
408+
return ! array_is_list($array);
411409
}
412410

413411
/**
@@ -420,7 +418,7 @@ public static function isAssoc(array $array)
420418
*/
421419
public static function isList($array)
422420
{
423-
return ! self::isAssoc($array);
421+
return array_is_list($array);
424422
}
425423

426424
/**
@@ -681,7 +679,7 @@ public static function sortRecursive($array, $options = SORT_REGULAR, $descendin
681679
}
682680
}
683681

684-
if (static::isAssoc($array)) {
682+
if (! array_is_list($array)) {
685683
$descending
686684
? krsort($array, $options)
687685
: ksort($array, $options);

src/Illuminate/Database/Eloquent/Relations/Relation.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Illuminate\Database\Eloquent\ModelNotFoundException;
1111
use Illuminate\Database\MultipleRecordsFoundException;
1212
use Illuminate\Database\Query\Expression;
13-
use Illuminate\Support\Arr;
1413
use Illuminate\Support\Traits\ForwardsCalls;
1514
use Illuminate\Support\Traits\Macroable;
1615

@@ -445,7 +444,7 @@ public static function morphMap(array $map = null, $merge = true)
445444
*/
446445
protected static function buildMorphMapFromModels(array $models = null)
447446
{
448-
if (is_null($models) || Arr::isAssoc($models)) {
447+
if (is_null($models) || ! array_is_list($models)) {
449448
return $models;
450449
}
451450

src/Illuminate/Redis/Connections/PhpRedisConnection.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Closure;
66
use Illuminate\Contracts\Redis\Connection as ConnectionContract;
7-
use Illuminate\Support\Arr;
87
use Redis;
98
use RedisException;
109

@@ -241,7 +240,7 @@ public function zadd($key, ...$dictionary)
241240
*/
242241
public function zrangebyscore($key, $min, $max, $options = [])
243242
{
244-
if (isset($options['limit']) && Arr::isAssoc($options['limit'])) {
243+
if (isset($options['limit']) && ! array_is_list($options['limit'])) {
245244
$options['limit'] = [
246245
$options['limit']['offset'],
247246
$options['limit']['count'],
@@ -262,7 +261,7 @@ public function zrangebyscore($key, $min, $max, $options = [])
262261
*/
263262
public function zrevrangebyscore($key, $min, $max, $options = [])
264263
{
265-
if (isset($options['limit']) && Arr::isAssoc($options['limit'])) {
264+
if (isset($options['limit']) && ! array_is_list($options['limit'])) {
266265
$options['limit'] = [
267266
$options['limit']['offset'],
268267
$options['limit']['count'],

src/Illuminate/Routing/RouteRegistrar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected function compileAction($action)
210210
}
211211

212212
if (is_array($action) &&
213-
! Arr::isAssoc($action) &&
213+
array_is_list($action) &&
214214
Reflector::isCallable($action)) {
215215
if (strncmp($action[0], '\\', 1)) {
216216
$action[0] = '\\'.$action[0];

src/Illuminate/Validation/NestedRules.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function compile($attribute, $value, $data = null)
4040
Arr::undot(Arr::wrap($data))
4141
);
4242

43-
if (is_array($rules) && Arr::isAssoc($rules)) {
43+
if (is_array($rules) && ! array_is_list($rules)) {
4444
$nested = [];
4545

4646
foreach ($rules as $key => $rule) {

tests/Support/SupportArrTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,16 @@ public function testIsAssoc()
452452
$this->assertTrue(Arr::isAssoc([1 => 'a', 2 => 'b']));
453453
$this->assertFalse(Arr::isAssoc([0 => 'a', 1 => 'b']));
454454
$this->assertFalse(Arr::isAssoc(['a', 'b']));
455+
456+
$this->assertFalse(Arr::isAssoc([]));
457+
$this->assertFalse(Arr::isAssoc([1, 2, 3]));
458+
$this->assertFalse(Arr::isAssoc(['foo', 2, 3]));
459+
$this->assertFalse(Arr::isAssoc([0 => 'foo', 'bar']));
460+
461+
$this->assertTrue(Arr::isAssoc([1 => 'foo', 'bar']));
462+
$this->assertTrue(Arr::isAssoc([0 => 'foo', 'bar' => 'baz']));
463+
$this->assertTrue(Arr::isAssoc([0 => 'foo', 2 => 'bar']));
464+
$this->assertTrue(Arr::isAssoc(['foo' => 'bar', 'baz' => 'qux']));
455465
}
456466

457467
public function testIsList()

0 commit comments

Comments
 (0)