| Conditions | 9 |
| Paths | 8 |
| Total Lines | 29 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 90 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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 | } |
||
| 47 |