Passed
Push — master ( 246025...18c029 )
by Fabio
05:31
created

TArrayHelper::array_is_list()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
nc 4
nop 1
dl 0
loc 13
rs 9.6111
c 1
b 0
f 0
1
<?php
2
/**
3
 * TArrayHelper class file
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 */
9
10
namespace Prado\Util\Helpers;
11
12
use Traversable;
13
14
/**
15
 * TArrayHelper class.
16
 *
17
 * This is the class to assist in array access.
18
 *
19
 * @author Brad Anderson <[email protected]>
20
 * @since 4.2.3
21
 */
22
class TArrayHelper
23
{
24
	/**
25
	 * Determines if the given array is a list. An array is considered a list if its
26
	 * keys consist of consecutive numbers from 0 to count($array)-1.
27
	 * @param array|Traversable $array The array to check.
28
	 * @return bool is the array a list.
29
	 * @link https://www.php.net/manual/en/function.array-is-list.php
30
	 */
31
	public static function array_is_list(array|Traversable $array): bool
32
	{
33
		if (function_exists('array_is_list') && !($array instanceof Traversable)) {
34
			return array_is_list($array);
35
		}
36
		$i = -1;
37
		foreach ($array as $k => $v) {
38
			++$i;
39
			if ($k !== $i) {
40
				return false;
41
			}
42
		}
43
		return true;
44
	}
45
}
46