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.
Passed
Push — master ( f46a23...825633 )
by Baptiste
04:48
created

Primitive::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable\Map;
5
6
use Innmind\Immutable\{
7
    MapInterface,
8
    Map,
9
    Type,
10
    Str,
11
    Stream,
12
    StreamInterface,
13
    SetInterface,
14
    Set,
15
    Pair,
16
    Exception\InvalidArgumentException,
17
    Exception\LogicException,
18
    Exception\ElementNotFoundException,
19
    Exception\GroupEmptyMapException
20
};
21
22
final class Primitive implements MapInterface
23
{
24
    private $keyType;
25
    private $valueType;
26
    private $keySpecification;
27
    private $valueSpecification;
28
    private $values;
29
    private $size;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 138
    public function __construct(string $keyType, string $valueType)
35
    {
36 138
        $this->keySpecification = Type::of($keyType);
37
38 138
        if (!in_array($keyType, ['int', 'integer', 'string'], true)) {
39 2
            throw new LogicException;
40
        }
41
42 136
        $this->valueSpecification = Type::of($valueType);
43 136
        $this->keyType = new Str($keyType);
44 136
        $this->valueType = new Str($valueType);
45 136
        $this->values = [];
46 136
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 44
    public function keyType(): Str
52
    {
53 44
        return $this->keyType;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 38
    public function valueType(): Str
60
    {
61 38
        return $this->valueType;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 38
    public function size(): int
68
    {
69 38
        return $this->size ?? $this->size = \count($this->values);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public function count(): int
76
    {
77 2
        return $this->size();
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 4
    public function current()
84
    {
85 4
        return \current($this->values);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 4
    public function key()
92
    {
93 4
        return \key($this->values);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 4
    public function next()
100
    {
101 4
        \next($this->values);
102 4
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 96
    public function rewind()
108
    {
109 96
        \reset($this->values);
110 96
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 4
    public function valid()
116
    {
117 4
        return $this->key() !== null;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 56
    public function offsetExists($offset): bool
124
    {
125 56
        return \array_key_exists($offset, $this->values);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 8
    public function offsetGet($offset)
132
    {
133 8
        return $this->get($offset);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 2
    public function offsetSet($offset, $value)
140
    {
141 2
        throw new LogicException('You can\'t modify a map');
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 2
    public function offsetUnset($offset)
148
    {
149 2
        throw new LogicException('You can\'t modify a map');
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 108
    public function put($key, $value): MapInterface
156
    {
157 108
        $this->keySpecification->validate($key);
158 104
        $this->valueSpecification->validate($value);
159
160 96
        $map = clone $this;
161 96
        $map->size = null;
162 96
        $map->values[$key] = $value;
163 96
        $map->rewind();
164
165 96
        return $map;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 46
    public function get($key)
172
    {
173 46
        if (!$this->offsetExists($key)) {
174 8
            throw new ElementNotFoundException;
175
        }
176
177 38
        return $this->values[$key];
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 36
    public function contains($key): bool
184
    {
185 36
        return $this->offsetExists($key);
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 28
    public function clear(): MapInterface
192
    {
193 28
        $map = clone $this;
194 28
        $map->size = null;
195 28
        $map->values = [];
196
197 28
        return $map;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203 14
    public function equals(MapInterface $map): bool
204
    {
205 14
        if ($map->size() !== $this->size()) {
206 4
            return false;
207
        }
208
209 14
        foreach ($this->values as $k => $v) {
210 12
            if (!$map->contains($k)) {
211 4
                return false;
212
            }
213
214 12
            if ($map->get($k) !== $v) {
215 12
                return false;
216
            }
217
        }
218
219 12
        return true;
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225 4
    public function filter(callable $predicate): MapInterface
226
    {
227 4
        $map = $this->clear();
228
229 4
        foreach ($this->values as $k => $v) {
230 4
            if ($predicate($k, $v) === true) {
231 4
                $map->values[$k] = $v;
232
            }
233
        }
234
235 4
        $map->rewind();
236
237 4
        return $map;
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243 4
    public function foreach(callable $function): MapInterface
244
    {
245 4
        foreach ($this->values as $k => $v) {
246 4
            $function($k, $v);
247
        }
248
249 4
        return $this;
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255 8
    public function groupBy(callable $discriminator): MapInterface
256
    {
257 8
        if ($this->size() === 0) {
258 4
            throw new GroupEmptyMapException;
259
        }
260
261 4
        $map = null;
262
263 4
        foreach ($this->values as $k => $v) {
264 4
            $key = $discriminator($k, $v);
265
266 4
            if ($map === null) {
267 4
                $map = new Map(
268 4
                    Type::determine($key),
269 4
                    MapInterface::class
270
                );
271
            }
272
273 4
            if ($map->contains($key)) {
274 4
                $map = $map->put(
275 4
                    $key,
276 4
                    $map->get($key)->put($k, $v)
277
                );
278
            } else {
279 4
                $map = $map->put(
280 4
                    $key,
281 4
                    $this->clear()->put($k, $v)
282
                );
283
            }
284
        }
285
286 4
        return $map;
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292 26
    public function keys(): SetInterface
293
    {
294 26
        return Set::of((string) $this->keyType, ...\array_keys($this->values));
295
    }
296
297
    /**
298
     * {@inheritdoc}
299
     */
300 26
    public function values(): StreamInterface
301
    {
302 26
        return Stream::of((string) $this->valueType, ...\array_values($this->values));
303
    }
304
305
    /**
306
     * {@inheritdoc}
307
     */
308 12
    public function map(callable $function): MapInterface
309
    {
310 12
        $map = $this->clear();
311
312 12
        foreach ($this->values as $k => $v) {
313 12
            $return = $function($k, $v);
314
315 12
            if ($return instanceof Pair) {
316 8
                $this->keySpecification->validate($return->key());
317
318 4
                $key = $return->key();
319 4
                $value = $return->value();
320
            } else {
321 8
                $key = $k;
322 8
                $value = $return;
323
            }
324
325 8
            $this->valueSpecification->validate($value);
326
327 4
            $map->values[$key] = $value;
328
        }
329
330 4
        $map->rewind();
331
332 4
        return $map;
333
    }
334
335
    /**
336
     * {@inheritdoc}
337
     */
338 4
    public function join(string $separator): Str
339
    {
340 4
        return $this->values()->join($separator);
341
    }
342
343
    /**
344
     * {@inheritdoc}
345
     */
346 4
    public function remove($key): MapInterface
347
    {
348 4
        if (!$this->contains($key)) {
349 4
            return $this;
350
        }
351
352 4
        $map = clone $this;
353 4
        $map->size = null;
354 4
        unset($map->values[$key]);
355 4
        $map->rewind();
356
357 4
        return $map;
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363 6
    public function merge(MapInterface $map): MapInterface
364
    {
365
        if (
366 6
            !$this->keyType()->equals($map->keyType()) ||
367 6
            !$this->valueType()->equals($map->valueType())
368
        ) {
369 4
            throw new InvalidArgumentException(
370 4
                'The 2 maps does not reference the same types'
371
            );
372
        }
373
374 2
        return $map->reduce(
375 2
            $this,
376 2
            function(self $carry, $key, $value): self {
377 2
                return $carry->put($key, $value);
378 2
            }
379
        );
380
    }
381
382
    /**
383
     * {@inheritdoc}
384
     */
385 4
    public function partition(callable $predicate): MapInterface
386
    {
387 4
        $truthy = $this->clear();
388 4
        $falsy = $this->clear();
389
390 4
        foreach ($this->values as $k => $v) {
391 4
            $return = $predicate($k, $v);
392
393 4
            if ($return === true) {
394 4
                $truthy->values[$k] = $v;
395
            } else {
396 4
                $falsy->values[$k] = $v;
397
            }
398
        }
399
400 4
        $truthy->rewind();
401 4
        $falsy->rewind();
402
403 4
        return Map::of('bool', MapInterface::class)
404 4
            (true, $truthy)
405 4
            (false, $falsy);
406
    }
407
408
    /**
409
     * {@inheritdoc}
410
     */
411 8
    public function reduce($carry, callable $reducer)
412
    {
413 8
        foreach ($this->values as $k => $v) {
414 8
            $carry = $reducer($carry, $k, $v);
415
        }
416
417 8
        return $carry;
418
    }
419
}
420