Utils::isSequentialArray()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 29
ccs 0
cts 22
cp 0
rs 8.0555
c 1
b 0
f 0
cc 9
nc 8
nop 2
crap 90
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Air;
6
7
class Utils
8
{
9
    /**
10
     * Test if a value is a sequential array.
11
     *
12
     * @param mixed    $val            The tested value.
13
     * @param int|null $expectedLength If non null, only return true if the array length is exactly this value.
14
     * @return bool Whether the tested value is a sequential array.
15
     */
16
    public static function isSequentialArray($val, ?int $expectedLength = null): bool
17
    {
18
        if (!is_array($val)) {
19
            return false;
20
        }
21
22
        $cnt = count($val);
23
        if ($cnt === 0) {
24
            return $expectedLength === null || $expectedLength === 0;
25
        }
26
27
        // non-empty sequential array must have zero index, fast exit for 99% assoc arrays
28
        if (!array_key_exists(0, $val)) {
29
            return false;
30
        }
31
32
        // fast exit for length assertion
33
        if ($expectedLength !== null && $expectedLength !== $cnt) {
34
            return false;
35
        }
36
37
        $idx = 0;
38
        foreach ($val as $k => $v) {
39
            if ($k !== $idx++) {
40
                return false;
41
            }
42
        }
43
44
        return true;
45
    }
46
}
47