Issues (52)

src/Collections/BaseCollection.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Level23\Druid\Collections;
6
7
use Countable;
8
use ArrayAccess;
9
use ArrayIterator;
10
use IteratorAggregate;
11
use ReturnTypeWillChange;
0 ignored issues
show
The type ReturnTypeWillChange 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...
12
use InvalidArgumentException;
13
14
/**
15
 * @template T
16
 * @implements ArrayAccess<int, T>
17
 * @implements IteratorAggregate<int, T>
18
 */
19
abstract class BaseCollection implements IteratorAggregate, ArrayAccess, Countable
20
{
21
    /**
22
     * @var array<int, T>
23
     */
24
    protected array $items;
25
26
    /**
27
     * @return \ArrayIterator<int, T>
28
     */
29 1
    public function getIterator(): ArrayIterator
30
    {
31 1
        return new ArrayIterator($this->items);
32
    }
33
34
    /**
35
     * Add an item to our collection.
36
     *
37
     * @param T ...$item
38
     */
39 45
    public function add(...$item): void
40
    {
41 45
        $type = $this->getType();
42
43 45
        foreach ($item as $obj) {
44 45
            if (!$obj instanceof $type) {
45 1
                throw new InvalidArgumentException('We only accept instances of type ' . $type);
46
            }
47
48 45
            $this->items[] = $obj;
49
        }
50
    }
51
52
    /**
53
     * Return an array representation of our items
54
     *
55
     * @return array<int, int|string|array<mixed>>
56
     */
57
    abstract public function toArray(): array;
58
59
    /**
60
     * We only accept objects of this type.
61
     *
62
     * @return string
63
     */
64
    abstract public function getType(): string;
65
66
    /**
67
     * Whether an offset exists
68
     *
69
     * @link  https://php.net/manual/en/arrayaccess.offsetexists.php
70
     *
71
     * @param int $offset   <p>
72
     *                      An offset to check for.
73
     *                      </p>
74
     *
75
     * @return boolean true on success or false on failure.
76
     * </p>
77
     * <p>
78
     * The return value will be cast to boolean if non-boolean was returned.
79
     * @since 5.0.0
80
     */
81 1
    public function offsetExists($offset): bool
82
    {
83 1
        return isset($this->items[$offset]);
84
    }
85
86
    /**
87
     * Offset to retrieve
88
     *
89
     * @link  https://php.net/manual/en/arrayaccess.offsetget.php
90
     *
91
     * @param int $offset   <p>
92
     *                      The offset to retrieve.
93
     *                      </p>
94
     *
95
     * @return null|T
96
     * @since 5.0.0
97
     */
98 17
    #[ReturnTypeWillChange]
99
    public function offsetGet($offset)
100
    {
101 17
        return $this->items[$offset] ?? null;
102
    }
103
104
    /**
105
     * Offset to set
106
     *
107
     * @link  https://php.net/manual/en/arrayaccess.offsetset.php
108
     *
109
     * @param int|null $offset <p>
110
     *                         The offset to assign the value to.
111
     *                         </p>
112
     * @param T        $value  <p>
113
     *                         The value to set.
114
     *                         </p>
115
     *
116
     * @return void
117
     * @since 5.0.0
118
     */
119 3
    #[ReturnTypeWillChange]
120
    public function offsetSet($offset, $value): void
121
    {
122 3
        $type = $this->getType();
123 3
        if (!$value instanceof $type) {
124 1
            throw new InvalidArgumentException('We only accept ' . $type . ' as values!');
125
        }
126
127 2
        if (is_null($offset)) {
128 1
            $this->items[] = $value;
129
        } else {
130 2
            $this->items[$offset] = $value;
131
        }
132
    }
133
134
    /**
135
     * Offset to unset
136
     *
137
     * @link  https://php.net/manual/en/arrayaccess.offsetunset.php
138
     *
139
     * @param int $offset   <p>
140
     *                      The offset to unset.
141
     *                      </p>
142
     *
143
     * @return void
144
     * @since 5.0.0
145
     */
146 1
    #[ReturnTypeWillChange]
147
    public function offsetUnset($offset): void
148
    {
149 1
        unset($this->items[$offset]);
150
    }
151
152
    /**
153
     * Count elements of an object
154
     *
155
     * @link  https://php.net/manual/en/countable.count.php
156
     * @return int The custom count as an integer.
157
     * </p>
158
     * <p>
159
     * The return value is cast to an integer.
160
     * @since 5.1.0
161
     */
162 28
    public function count(): int
163
    {
164 28
        return count($this->items);
165
    }
166
}