Passed
Pull Request — 1.0.0 (#3)
by
unknown
10:07
created

StringCollection::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StraTDeS\VO\Collection;
6
7
8
use ArrayAccess;
9
use Countable;
10
use InvalidArgumentException;
11
use Iterator;
12
use StraTDeS\VO\Single\String;
0 ignored issues
show
Bug introduced by
The type StraTDeS\VO\Single\String 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...
13
use TypeError;
14
15
class StringCollection implements Countable, ArrayAccess, Iterator
16
{
17
    protected array $items;
18
    protected int $position;
19
20
    protected function __construct()
21
    {
22
        $this->items = [];
23
        $this->position = 0;
24
    }
25
26
    public static function create(): self
27
    {
28
        return new static();
29
    }
30
31
    public function items(): array
32
    {
33
        return $this->items;
34
    }
35
36
    public function current(): string
37
    {
38
        return $this->items[$this->position];
39
    }
40
41
    public function next(): void
42
    {
43
        $this->position++;
44
    }
45
46
    public function key(): int
47
    {
48
        return $this->position;
49
    }
50
51
    public function valid(): bool
52
    {
53
        return isset($this->items[$this->position]);
54
    }
55
56
    public function rewind(): void
57
    {
58
        $this->position = 0;
59
    }
60
61
    public function offsetExists($offset): bool
62
    {
63
        return isset($this->items[$offset]);
64
    }
65
66
    public function offsetGet($offset): string
67
    {
68
        return $this->items[$offset];
69
    }
70
71
    public function offsetSet($offset, $value): void
72
    {
73
        $this->checkValueIsValidForCollection($value);
74
        $this->checkOffsetIsValidForCollection($offset);
75
76
        $this->items[$offset] = $value;
77
    }
78
79
    public function offsetUnset($offset): void
80
    {
81
        unset($this->items[$offset]);
82
    }
83
84
    public function count(): int
85
    {
86
        return count($this->items);
87
    }
88
89
    protected function checkValueIsValidForCollection($value): void
90
    {
91
        if (is_string($value) !== true) {
92
            throw new TypeError('Provided value has to be string');
93
        }
94
    }
95
96
    protected function checkOffsetIsValidForCollection($offset): void
97
    {
98
        if (is_int($offset) !== true) {
99
            throw new TypeError('Provided offset has to be int');
100
        }
101
102
        if ($offset < 0) {
103
            throw new InvalidArgumentException('Provided offset has to be greater than -1');
104
        }
105
    }
106
107
    public function add($item): self
108
    {
109
        $this->checkValueIsValidForCollection($item);
110
111
        $this->items[] = $item;
112
113
        return $this;
114
    }
115
}