Passed
Push — 1.0.0 ( 41296f...dcbbd2 )
by Alex
01:37
created

AbstractCollection::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace StraTDeS\VO\Collection;
4
5
abstract class AbstractCollection implements \Countable, \ArrayAccess, \Iterator
6
{
7
    protected array $items;
8
    protected int $position;
9
10 3
    protected function __construct()
11
    {
12 3
        $this->items = [];
13 3
        $this->position = 0;
14 3
    }
15
16 3
    public static function create()
17
    {
18 3
        return new static();
19
    }
20
21 1
    public function items(): array
22
    {
23 1
        return $this->items;
24
    }
25
26 1
    public function count()
27
    {
28 1
        return count($this->items);
29
    }
30
31
    public function offsetExists($offset)
32
    {
33
        return isset($this->items[$offset]);
34
    }
35
36 2
    public function offsetGet($offset)
37
    {
38 2
        return $this->items[$offset];
39
    }
40
41 1
    public function offsetSet($offset, $value)
42
    {
43 1
        $this->checkValueIsValidForCollection($value);
44 1
        $this->items[$offset] = $value;
45 1
    }
46
47
    public function offsetUnset($offset)
48
    {
49
        unset($this->items[$offset]);
50
    }
51
52 3
    private function checkValueIsValidForCollection($value): void
53
    {
54 3
        if (get_class($value) != $this->singleItemClass()) {
55 1
            throw new \InvalidArgumentException("Provided value is not a valid " . $this->singleItemClass());
56
        }
57 2
    }
58
59 1
    public function current()
60
    {
61 1
        return $this->items[$this->position];
62
    }
63
64 1
    public function next()
65
    {
66 1
        $this->position++;
67 1
    }
68
69
    public function key()
70
    {
71
        return $this->position;
72
    }
73
74 1
    public function valid()
75
    {
76 1
        return isset($this->items[$this->position]);
77
    }
78
79 1
    public function rewind()
80
    {
81 1
        $this->position = 0;
82 1
    }
83
84 2
    public function add($item) {
85 2
        $this->checkValueIsValidForCollection($item);
86
87 1
        $this->items[] = $item;
88
89 1
        return $this;
90
    }
91
92
    protected abstract function singleItemClass(): string;
93
}
94