GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 843868...15d4ba )
by Baptiste
02:12
created

ObjectStorage   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 313
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 4
Metric Value
wmc 31
c 4
b 0
f 4
lcom 1
cbo 1
dl 0
loc 313
ccs 88
cts 88
cp 1
rs 9.8

26 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A toPrimitive() 0 5 1
A merge() 0 8 1
A attach() 0 7 1
A contains() 0 4 1
A detach() 0 7 1
A getHash() 0 4 1
A getInfo() 0 4 1
A removeAll() 0 7 1
A removeAllExcept() 0 7 1
A setInfo() 0 8 1
A count() 0 4 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A rewind() 0 4 1
A valid() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 6 1
A offsetUnset() 0 6 1
A serialize() 0 4 1
A unserialize() 0 7 1
A filter() 0 16 3
A each() 0 10 2
A map() 0 14 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\LogicException;
7
8
class ObjectStorage implements PrimitiveInterface, \Countable, \Iterator, \ArrayAccess, \Serializable
9
{
10
    private $objects;
11
12 19
    public function __construct(\SplObjectStorage $objects = null)
13
    {
14 19
        $this->objects = $objects ? clone $objects : new \SplObjectStorage;
15 19
        $this->objects->rewind();
16 19
    }
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 4
    public function toPrimitive(): \SplObjectStorage
22
    {
23
        //so the inner SplObjectStorage object can't be modified
24 4
        return clone $this->objects;
25
    }
26
27
    /**
28
     * Merge storages
29
     *
30
     * @param self $storage
31
     *
32
     * @return self
33
     */
34 1
    public function merge(self $storage): self
35
    {
36 1
        $objects = new \SplObjectStorage;
37 1
        $objects->addAll($this->objects);
38 1
        $objects->addAll($storage->objects);
39
40 1
        return new self($objects);
41
    }
42
43
    /**
44
     * Attach a new element to the storage
45
     *
46
     * @param object $object
47
     * @param mixed $data
48
     *
49
     * @return self
50
     */
51 16
    public function attach($object, $data = null): self
52
    {
53 16
        $objects = clone $this->objects;
54 16
        $objects->attach($object, $data);
55
56 16
        return new self($objects);
57
    }
58
59
    /**
60
     * Check if the object is in this storage
61
     *
62
     * @param object $object
63
     *
64
     * @return bool
65
     */
66 4
    public function contains($object): bool
67
    {
68 4
        return $this->objects->contains($object);
69
    }
70
71
    /**
72
     * Remove the given object from the storage
73
     *
74
     * @param object $object
75
     *
76
     * @return self
77
     */
78 1
    public function detach($object): self
79
    {
80 1
        $objects = clone $this->objects;
81 1
        $objects->detach($object);
82
83 1
        return new self($objects);
84
    }
85
86
    /**
87
     * Get the internal has for the given object
88
     *
89
     * @param object $object
90
     *
91
     * @return string
92
     */
93 1
    public function getHash($object): string
94
    {
95 1
        return $this->objects->getHash($object);
96
    }
97
98
    /**
99
     * Return the info associated to the current object pointed by the internal pointer
100
     *
101
     * @return mixed
102
     */
103 1
    public function getInfo()
104
    {
105 1
        return $this->objects->getInfo();
106
    }
107
108
    /**
109
     * Remove all the elements contained in the given storage from the current one
110
     *
111
     * @param self $storage
112
     *
113
     * @return self
114
     */
115 1
    public function removeAll(self $storage): self
116
    {
117 1
        $objects = clone $this->objects;
118 1
        $objects->removeAll($storage->objects);
119
120 1
        return new self($objects);
121
    }
122
123
    /**
124
     * Remove all elements not contained in the given storage
125
     *
126
     * @param self $storage
127
     *
128
     * @return self
129
     */
130 1
    public function removeAllExcept(self $storage): self
131
    {
132 1
        $objects = clone $this->objects;
133 1
        $objects->removeAllExcept($storage->objects);
134
135 1
        return new self($objects);
136
    }
137
138
    /**
139
     * Associate data to the current object
140
     *
141
     * @param mixed $data
142
     *
143
     * @return self
144
     */
145 1
    public function setInfo($data): self
146
    {
147 1
        $current = $this->objects->current();
148 1
        $objects = clone $this->objects;
149 1
        $objects[$current] = $data;
150
151 1
        return new self($objects);
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 9
    public function count(): int
158
    {
159 9
        return $this->objects->count();
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 4
    public function current()
166
    {
167 4
        return $this->objects->current();
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173 3
    public function next()
174
    {
175 3
        $this->objects->next();
176 3
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181 1
    public function key()
182
    {
183 1
        return $this->objects->key();
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189 4
    public function rewind()
190
    {
191 4
        $this->objects->rewind();
192 4
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197 1
    public function valid(): bool
198
    {
199 1
        return $this->objects->valid();
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205 1
    public function offsetExists($object): bool
206
    {
207 1
        return $this->objects->offsetExists($object);
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213 1
    public function offsetGet($object)
214
    {
215 1
        return $this->objects->offsetGet($object);
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221 1
    public function offsetSet($object, $data = null)
222
    {
223 1
        throw new LogicException(
224 1
            'You can\'t modify an immutable object storage'
225
        );
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231 1
    public function offsetUnset($object)
232
    {
233 1
        throw new LogicException(
234 1
            'You can\'t modify an immutable object storage'
235
        );
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241 1
    public function serialize(): string
242
    {
243 1
        return $this->objects->serialize();
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249 1
    public function unserialize($serialized): self
250
    {
251 1
        $objects = clone $this->objects;
252 1
        $objects->unserialize($serialized);
253
254 1
        return new self($objects);
255
    }
256
257
    /**
258
     * Apply the given filter on the collection
259
     *
260
     * @param Closure $filterer
261
     *
262
     * @return self
263
     */
264 1
    public function filter(\Closure $filterer): self
265
    {
266 1
        $objects = new \SplObjectStorage;
267
268 1
        foreach ($this->objects as $object) {
269 1
            $data = $this->objects[$object];
270
271 1
            if ($filterer($object, $data) === true) {
272 1
                $objects->attach($object, $data);
273
            }
274
        }
275
276 1
        $this->rewind();
277
278 1
        return new self($objects);
279
    }
280
281
    /**
282
     * Run the given closure on each element
283
     *
284
     * @param Closure $callback
285
     *
286
     * @return self
287
     */
288 1
    public function each(\Closure $callback): self
289
    {
290 1
        foreach ($this->objects as $object) {
291 1
            $callback($object, $this->objects[$object]);
292
        }
293
294 1
        $this->rewind();
295
296 1
        return $this;
297
    }
298
299
    /**
300
     * Generate a new storage based on the given mapper
301
     *
302
     * @param Closure $mapper
303
     *
304
     * @return self
305
     */
306 1
    public function map(\Closure $mapper): self
307
    {
308 1
        $objects = new \SplObjectStorage;
309
310 1
        foreach ($this->objects as $object) {
311 1
            $objects->attach(
312 1
                $mapper($object, $this->objects[$object])
313
            );
314
        }
315
316 1
        $this->rewind();
317
318 1
        return new self($objects);
319
    }
320
}
321