Passed
Push — master ( e74319...b3cfef )
by Smoren
01:49
created

Util::isArraySequential()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Smoren\ArrayView;
4
5
use Smoren\ArrayView\Exceptions\IndexError;
6
7
class Util
8
{
9
    public static function normalizeIndex(int $index, int $containerLength, bool $throwError = true): int
10
    {
11
        $dist = $index >= 0 ? $index : abs($index) - 1;
12
        if ($throwError && $dist >= $containerLength) {
13
            throw new IndexError("Index {$index} is out of range.");
14
        }
15
        return $index < 0 ? $containerLength + $index : $index;
16
    }
17
18
    public static function isArraySequential(array $source): bool
19
    {
20
        if (!function_exists('array_is_list')) {
21
            return array_keys($source) === range(0, count($source) - 1);
22
        }
23
        return array_is_list($source);
24
    }
25
}
26