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 ( bf65c0...bb2528 )
by Baptiste
04:09 queued 01:44
created

Map.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\{
7
    Exception\InvalidArgumentException,
8
    Exception\LogicException,
9
    Exception\ElementNotFoundException,
10
    Exception\GroupEmptyMapException
11
};
12
13
class Map implements MapInterface
14
{
15
    use Type;
16
17
    private $keyType;
18
    private $valueType;
19
    private $keySpec;
20
    private $valueSpec;
21
    private $keys;
22
    private $values;
23
    private $pairs;
24
25 36
    public function __construct(string $keyType, string $valueType)
26
    {
27 36
        $this->keySpec = $this->getSpecFor($keyType);
28 36
        $this->valueSpec = $this->getSpecFor($valueType);
29 36
        $this->keyType = new StringPrimitive($keyType);
30 36
        $this->valueType = new StringPrimitive($valueType);
31 36
        $this->keys = new Sequence;
32 36
        $this->values = new Sequence;
33 36
        $this->pairs = new Sequence;
34 36
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 14
    public function keyType(): StringPrimitive
40
    {
41 14
        return $this->keyType;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 13
    public function valueType(): StringPrimitive
48
    {
49 13
        return $this->valueType;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 11
    public function size(): int
56
    {
57 11
        return $this->keys->size();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function count(): int
64
    {
65
        return $this->keys->count();
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function current()
72
    {
73 1
        return $this->values->current();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function key()
80
    {
81 1
        return $this->keys->current();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function next()
88
    {
89 1
        $this->keys->next();
90 1
        $this->values->next();
91 1
        $this->pairs->next();
92 1
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 1
    public function rewind()
98
    {
99 1
        $this->keys->rewind();
100 1
        $this->values->rewind();
101 1
        $this->pairs->rewind();
102 1
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 1
    public function valid()
108
    {
109 1
        return $this->keys->valid();
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    public function offsetExists($offset): bool
116
    {
117 1
        return $this->keys->contains($offset);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 2
    public function offsetGet($offset)
124
    {
125 2
        return $this->get($offset);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 1
    public function offsetSet($offset, $value)
132
    {
133 1
        throw new LogicException('You can\'t modify a map');
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 1
    public function offsetUnset($offset)
140
    {
141 1
        throw new LogicException('You can\'t modify a map');
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 30
    public function put($key, $value): MapInterface
148
    {
149 30
        $this->keySpec->validate($key);
150 29
        $this->valueSpec->validate($value);
151
152 27
        $map = clone $this;
153
154 27
        if ($this->keys->contains($key)) {
155 6
            $index = $this->keys->indexOf($key);
156 6
            $map->values = (new Sequence)
157 6
                ->append($this->values->take($index))
158 6
                ->add($value)
159 6
                ->append($this->values->drop($index + 1));
160 6
            $map->pairs = (new Sequence)
161 6
                ->append($this->pairs->take($index))
162 6
                ->add(new Pair($key, $value))
163 6
                ->append($this->pairs->drop($index + 1));
164
        } else {
165 27
            $map->keys = $this->keys->add($key);
166 27
            $map->values = $this->values->add($value);
167 27
            $map->pairs = $this->pairs->add(new Pair($key, $value));
168
        }
169
170 27
        return $map;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176 12
    public function get($key)
177
    {
178 12
        if (!$this->keys->contains($key)) {
179 2
            throw new ElementNotFoundException;
180
        }
181
182 10
        return $this->values->get(
183 10
            $this->keys->indexOf($key)
184
        );
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190 10
    public function contains($key): bool
191
    {
192 10
        return $this->keys->contains($key);
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198 1 View Code Duplication
    public function drop(int $size): MapInterface
199
    {
200 1
        $map = clone $this;
201 1
        $map->keys = $this->keys->drop($size);
202 1
        $map->values = $this->values->drop($size);
203 1
        $map->pairs = $this->pairs->drop($size);
204
205 1
        return $map;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211 1 View Code Duplication
    public function dropEnd(int $size): MapInterface
212
    {
213 1
        $map = clone $this;
214 1
        $map->keys = $this->keys->dropEnd($size);
215 1
        $map->values = $this->values->dropEnd($size);
216 1
        $map->pairs = $this->pairs->dropEnd($size);
217
218 1
        return $map;
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224 4
    public function clear(): MapInterface
225
    {
226 4
        $map = clone $this;
227 4
        $map->keys = new Sequence;
228 4
        $map->values = new Sequence;
229 4
        $map->pairs = new Sequence;
230
231 4
        return $map;
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237 2
    public function equals(MapInterface $map): bool
238
    {
239 2
        if (!$map->keys()->equals($this->keys())) {
240 2
            return false;
241
        }
242
243 1
        return $map->values()->equals($this->values());
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249 1
    public function filter(\Closure $predicate): MapInterface
250
    {
251 1
        $map = $this->clear();
252
253 1
        foreach ($this->keys as $index => $key) {
254 1
            $value = $this->values->get($index);
255
256 1
            if ($predicate($key, $value) === true) {
257 1
                $map->keys = $map->keys->add($key);
258 1
                $map->values = $map->values->add($value);
259 1
                $map->pairs = $map->pairs->add($this->pairs->get($index));
260
            }
261
        }
262
263 1
        return $map;
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function foreach(\Closure $function): MapInterface
270
    {
271 2
        foreach ($this->keys as $index => $key) {
272 2
            $function($key, $this->values->get($index));
273
        }
274
275 2
        return $this;
276
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281 2
    public function groupBy(\Closure $discriminator): MapInterface
282
    {
283 2
        if ($this->size() === 0) {
284 1
            throw new GroupEmptyMapException;
285
        }
286
287 1
        $map = null;
288
289 1
        foreach ($this->keys as $index => $key) {
290 1
            $newKey = $discriminator($key, $this->values->get($index));
291
292 1 View Code Duplication
            if ($map === null) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
293 1
                $type = gettype($newKey);
294 1
                $map = new self(
295 1
                    $type === 'object' ? get_class($newKey) : $type,
296 1
                    SequenceInterface::class
297
                );
298
            }
299
300 1
            $pair = $this->pairs->get($index);
301
302 1
            if ($map->contains($newKey)) {
303 1
                $map = $map->put(
304
                    $newKey,
305 1
                    $map->get($newKey)->add($pair)
306
                );
307
            } else {
308 1
                $map = $map->put($newKey, new Sequence($pair));
309
            }
310
        }
311
312 1
        return $map;
313
    }
314
315
    /**
316
     * {@inheritdoc}
317
     */
318 1
    public function first(): Pair
319
    {
320 1
        return $this->pairs->first();
321
    }
322
323
    /**
324
     * {@inheritdoc}
325
     */
326 1
    public function last(): Pair
327
    {
328 1
        return $this->pairs->last();
329
    }
330
331
    /**
332
     * {@inheritdoc}
333
     */
334 8
    public function keys(): SequenceInterface
335
    {
336 8
        return $this->keys;
337
    }
338
339
    /**
340
     * {@inheritdoc}
341
     */
342 6
    public function values(): SequenceInterface
343
    {
344 6
        return $this->values;
345
    }
346
347
    /**
348
     * {@inheritdoc}
349
     */
350 1
    public function map(\Closure $function): MapInterface
351
    {
352 1
        $map = $this->clear();
353
354 1 View Code Duplication
        foreach ($this->keys as $index => $key) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
355 1
            $return = $function(
356
                $key,
357 1
                $this->values->get($index)
358
            );
359
360 1
            if ($return instanceof Pair) {
361 1
                $key = $return->key();
362 1
                $value = $return->value();
363
            } else {
364 1
                $value = $return;
365
            }
366
367 1
            $map = $map->put($key, $value);
368
        }
369
370 1
        return $map;
371
    }
372
373
    /**
374
     * {@inheritdoc}
375
     */
376 1 View Code Duplication
    public function take(int $size): MapInterface
377
    {
378 1
        $map = clone $this;
379 1
        $map->keys = $this->keys->take($size);
380 1
        $map->values = $this->values->take($size);
381 1
        $map->pairs = $this->pairs->take($size);
382
383 1
        return $map;
384
    }
385
386
    /**
387
     * {@inheritdoc}
388
     */
389 1 View Code Duplication
    public function takeEnd(int $size): MapInterface
390
    {
391 1
        $map = clone $this;
392 1
        $map->keys = $this->keys->takeEnd($size);
393 1
        $map->values = $this->values->takeEnd($size);
394 1
        $map->pairs = $this->pairs->takeEnd($size);
395
396 1
        return $map;
397
    }
398
399
    /**
400
     * {@inheritdoc}
401
     */
402 1
    public function join(string $separator): StringPrimitive
403
    {
404 1
        return $this->values->join($separator);
405
    }
406
407
    /**
408
     * {@inheritdoc}
409
     */
410 1
    public function remove($key): MapInterface
411
    {
412 1
        if (!$this->contains($key)) {
413 1
            return $this;
414
        }
415
416 1
        $index = $this->keys->indexOf($key);
417 1
        $map = clone $this;
418 1
        $map->keys = $this
419 1
            ->keys
420 1
            ->slice(0, $index)
421 1
            ->append($this->keys->slice($index + 1, $this->keys->size()));
422 1
        $map->values = $this
423 1
            ->values
424 1
            ->slice(0, $index)
425 1
            ->append($this->values->slice($index + 1, $this->values->size()));
426 1
        $map->pairs = $this
427 1
            ->pairs
428 1
            ->slice(0, $index)
429 1
            ->append($this->pairs->slice($index + 1, $this->pairs->size()));
430
431 1
        return $map;
432
    }
433
434
    /**
435
     * {@inheritdoc}
436
     */
437 2
    public function merge(MapInterface $map): MapInterface
438
    {
439
        if (
440 2
            !$this->keyType()->equals($map->keyType()) ||
441 2
            !$this->valueType()->equals($map->valueType())
442
        ) {
443 1
            throw new InvalidArgumentException(
444 1
                'The 2 maps does not reference the same types'
445
            );
446
        }
447
448 1
        $newMap = clone $this;
449
450 1
        $map->foreach(function($key, $value) use (&$newMap) {
451 1
            $newMap = $newMap->put($key, $value);
452 1
        });
453
454 1
        return $newMap;
455
    }
456
457
    /**
458
     * {@inheritdoc}
459
     */
460 1
    public function partition(\Closure $predicate): MapInterface
461
    {
462 1
        $truthy = $this->clear();
463 1
        $falsy = $this->clear();
464
465 1 View Code Duplication
        foreach ($this->keys as $index => $key) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
466 1
            $return = $predicate(
467
                $key,
468 1
                $value = $this->values->get($index)
469
            );
470
471 1
            if ($return === true) {
472 1
                $truthy = $truthy->put($key, $value);
473
            } else {
474 1
                $falsy = $falsy->put($key, $value);
475
            }
476
        }
477
478 1
        return (new self('bool', MapInterface::class))
479 1
            ->put(true, $truthy)
480 1
            ->put(false, $falsy);
481
    }
482
}
483