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

Util::normalizeIndex()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 9.6111
cc 5
nc 6
nop 3
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