AbstractCollection::offsetUnset()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * Part of the Fusion.Collection package.
5
 *
6
 * @license MIT
7
 */
8
9
declare(strict_types=1);
10
11
namespace Fusion\Collection\Contracts;
12
13
use ArrayAccess;
14
use Fusion\Collection\Exceptions\CollectionException;
15
use Iterator;
16
17
/**
18
 * Implements core functionality for collections.
19
 *
20
 * @since 1.0.0
21
 */
22
abstract class AbstractCollection implements CollectionCoreInterface, ArrayAccess, Iterator
23
{
24
    /** @var array */
25
    protected array $collection = [];
26
27
    /** @var CollectionValidationInterface */
28
    protected CollectionValidationInterface $validator;
29
30
    /** {@inheritdoc} */
31 1
    public function clear(): void
32
    {
33 1
        $this->collection = [];
34 1
    }
35
36
    /** {@inheritdoc} */
37 3
    public function remove($item): void
38
    {
39 3
        foreach ($this->collection as $key => $value) {
40 3
            if ($item === $value) {
41 2
                $this->removeAt($key);
42
            }
43
        }
44 3
    }
45
46
    /** {@inheritdoc} */
47 6
    public function removeAt($key): void
48
    {
49 6
        if ($this->offsetExists($key)) {
50 6
            if (is_int($key)) {
51 3
                array_splice($this->collection, $key, 1);
52
            } else {
53 3
                if (is_string($key)) {
54 3
                    unset($this->collection[$key]);
55
                }
56
            }
57
        }
58 6
    }
59
60
    /**
61
     * Checks if an offset exists in the collection.
62
     *
63
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
64
     *
65
     * @param mixed $offset
66
     *
67
     * @return bool
68
     */
69 35
    public function offsetExists($offset): bool
70
    {
71 35
        return array_key_exists($offset, $this->collection);
72
    }
73
74
    /**
75
     * Retrieves a value at the given offset.
76
     *
77
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
78
     *
79
     * @param mixed $offset
80
     *
81
     * @return mixed
82
     */
83 14
    public function offsetGet($offset)
84
    {
85 14
        return $this->collection[$offset];
86
    }
87
88
    /**
89
     * Sets a value at the given offset.
90
     *
91
     * This method will throw a `CollectionException` if the offset does not exist.
92
     *
93
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
94
     *
95
     * @param mixed $offset
96
     * @param mixed $value
97
     *
98
     * @return void
99
     *
100
     * @throws CollectionException
101
     */
102 27
    public function offsetSet($offset, $value): void
103
    {
104 27
        $this->validator->validateNonNullValue($value);
105 24
        $this->collection[$offset] = $value;
106 24
    }
107
108
    /**
109
     * Removes a value at the given offset.
110
     *
111
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
112
     *
113
     * @param mixed $offset
114
     *
115
     * @return void
116
     */
117 3
    public function offsetUnset($offset): void
118
    {
119 3
        if ($this->offsetExists($offset)) {
120 3
            $this->removeAt($offset);
121
        }
122 3
    }
123
124
    /**
125
     * Returns the current element in the collection.
126
     *
127
     * @link http://php.net/manual/en/iterator.current.php
128
     *
129
     * @return mixed
130
     */
131 4
    public function current()
132
    {
133 4
        return current($this->collection);
134
    }
135
136
    /**
137
     * Move forward to the next element in the collection.
138
     *
139
     * @link http://php.net/manual/en/iterator.next.php
140
     *
141
     * @return void
142
     */
143 4
    public function next(): void
144
    {
145 4
        next($this->collection);
146 4
    }
147
148
    /**
149
     * Return the key of the current element in the collection.
150
     *
151
     * @link http://php.net/manual/en/iterator.key.php
152
     *
153
     * @return mixed
154
     */
155 2
    public function key()
156
    {
157 2
        return key($this->collection);
158
    }
159
160
    /**
161
     * Checks if the current element position is valid.
162
     *
163
     * @link http://php.net/manual/en/iterator.valid.php
164
     *
165
     * @return bool
166
     */
167 3
    public function valid(): bool
168
    {
169 3
        return key($this->collection) !== null;
170
    }
171
172
    /**
173
     * Rewind the collection's position to the first index.
174
     *
175
     * @link http://php.net/manual/en/iterator.rewind.php
176
     *
177
     * @return void
178
     */
179 2
    public function rewind(): void
180
    {
181 2
        reset($this->collection);
182 2
    }
183
184
    /**
185
     * Returns of count of the elements in a collection.
186
     *
187
     * @link http://php.net/manual/en/countable.count.php
188
     *
189
     * @return int
190
     */
191 26
    public function count(): int
192
    {
193 26
        return count($this->collection);
194
    }
195
}