Passed
Pull Request — master (#40)
by Artem
02:33 queued 49s
created

src/Model/AbstractCollection.php (1 issue)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoman4eg\Nalog\Model;
5
6
/**
7
 * @author Artem Dubinin <[email protected]>
8
 *
9
 * @template T
10
 *
11
 * @template-implements \ArrayAccess<int, T>
12
 * @template-implements \Iterator<int, T>
13
 */
14
abstract class AbstractCollection implements \ArrayAccess, \Countable, \Iterator
15
{
16
    /** @var array<int, T> */
17
    private array $items = [];
18
19
    private int $key;
20
21
    private int $count;
22
23
    /**
24
     * @return null|T
0 ignored issues
show
The type Shoman4eg\Nalog\Model\T was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
     */
26
    public function current()
27
    {
28
        return $this->offsetGet($this->key);
29
    }
30
31
    public function next(): void
32
    {
33
        ++$this->key;
34
    }
35
36
    public function key(): ?int
37
    {
38
        if (!$this->valid()) {
39
            return null;
40
        }
41
42
        return $this->key;
43
    }
44
45
    public function valid(): bool
46
    {
47
        return $this->key < $this->count;
48
    }
49
50
    public function rewind(): void
51
    {
52
        $this->key = 0;
53
    }
54
55
    public function offsetExists($offset): bool
56
    {
57
        return isset($this->items[$offset]);
58
    }
59
60
    /**
61
     * @param int $offset
62
     *
63
     * @return T
64
     */
65
    public function offsetGet($offset)
66
    {
67
        if (!$this->offsetExists($offset)) {
68
            throw new \RuntimeException(sprintf('Key "%s" does not exist in collection', $offset));
69
        }
70
71
        return $this->items[$offset];
72
    }
73
74
    public function offsetSet($offset, $value): void
75
    {
76
        throw new \RuntimeException('Cannot set value on READ ONLY collection');
77
    }
78
79
    public function offsetUnset($offset): void
80
    {
81
        throw new \RuntimeException('Cannot unset value on READ ONLY collection');
82
    }
83
84
    public function count(): int
85
    {
86
        return $this->count;
87
    }
88
89
    protected function setItems(array $items): void
90
    {
91
        if ($this->items !== []) {
92
            throw new \LogicException('AbstractCollection::setItems can only be called once.');
93
        }
94
95
        $this->items = array_values($items);
96
        $this->count = count($items);
97
        $this->key = 0;
98
    }
99
}
100