Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ArrayAccessHelper
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 offsetExistsUInt
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 offsetGetUInt
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Collection;
6
7use PeServer\Core\Throws\IndexOutOfRangeException;
8use PeServer\Core\TypeUtility;
9use TypeError;
10
11abstract class ArrayAccessHelper
12{
13    #region function
14
15    /**
16     * ArrayAccess::offsetExists で non-negative-int に限定
17     *
18     * @param mixed $offset
19     * @return bool $offsetが有効
20     * @phpstan-assert-if-true non-negative-int $offset
21     */
22    public static function offsetExistsUInt(mixed $offset): bool
23    {
24        if (!is_int($offset)) {
25            return false;
26        }
27
28        if ($offset < 0) {
29            return false;
30        }
31
32        return true;
33    }
34
35    /**
36     * ArrayAccess::offsetGet で non-negative-int に限定(失敗時は例外)
37     *
38     * @param mixed $offset
39     * @throws TypeError 型がもうダメ。
40     * @throws IndexOutOfRangeException 範囲外。
41     * @phpstan-assert non-negative-int $offset
42     */
43    public static function offsetGetUInt(mixed $offset): void
44    {
45        if (!is_int($offset)) {
46            throw new TypeError(TypeUtility::getType($offset));
47        }
48
49        if ($offset < 0) {
50            throw new IndexOutOfRangeException((string)$offset);
51        }
52    }
53
54    #endregion
55}