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   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 479
Duplicated Lines 13.99 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 98.97%
Metric Value
wmc 55
lcom 1
cbo 11
dl 67
loc 479
ccs 193
cts 195
cp 0.9897
rs 6.8

36 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A keyType() 0 4 1
A valueType() 0 4 1
A size() 0 4 1
A count() 0 4 1
A current() 0 4 1
A key() 0 4 1
A next() 0 6 1
A rewind() 0 6 1
A valid() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
B put() 0 25 2
A get() 0 10 2
A contains() 0 4 1
A drop() 9 9 1
A dropEnd() 9 9 1
A clear() 0 9 1
A equals() 0 8 2
A filter() 0 14 3
A foreach() 0 8 2
B groupBy() 31 31 6
A first() 0 4 1
A last() 0 4 1
A keys() 0 4 1
A values() 0 4 1
A map() 0 23 3
A take() 9 9 1
A takeEnd() 9 9 1
A join() 0 4 1
A remove() 0 23 2
A merge() 0 19 3
A partition() 0 22 3
A reduce() 0 8 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Map often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Map, and based on these observations, apply Extract Interface, too.

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 37
    public function __construct(string $keyType, string $valueType)
26
    {
27 37
        $this->keySpec = $this->getSpecFor($keyType);
28 37
        $this->valueSpec = $this->getSpecFor($valueType);
29 37
        $this->keyType = new StringPrimitive($keyType);
30 37
        $this->valueType = new StringPrimitive($valueType);
31 37
        $this->keys = new Sequence;
32 37
        $this->values = new Sequence;
33 37
        $this->pairs = new Sequence;
34 37
    }
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 31
    public function put($key, $value): MapInterface
148
    {
149 31
        $this->keySpec->validate($key);
150 30
        $this->valueSpec->validate($value);
151
152 28
        $map = clone $this;
153
154 28
        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 28
            $map->keys = $this->keys->add($key);
166 28
            $map->values = $this->values->add($value);
167 28
            $map->pairs = $this->pairs->add(new Pair($key, $value));
168
        }
169
170 28
        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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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->pairs as $pair) {
254 1
            if ($predicate($pair->key(), $pair->value()) === true) {
255 1
                $map->keys = $map->keys->add($pair->key());
256 1
                $map->values = $map->values->add($pair->value());
257 1
                $map->pairs = $map->pairs->add($pair);
258
            }
259
        }
260
261 1
        return $map;
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function foreach(\Closure $function): MapInterface
268
    {
269 2
        foreach ($this->pairs as $pair) {
270 2
            $function($pair->key(), $pair->value());
271
        }
272
273 2
        return $this;
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279 2 View Code Duplication
    public function groupBy(\Closure $discriminator): MapInterface
280
    {
281 2
        if ($this->size() === 0) {
282 1
            throw new GroupEmptyMapException;
283
        }
284
285 1
        $map = null;
286
287 1
        foreach ($this->pairs as $pair) {
288 1
            $key = $discriminator($pair->key(), $pair->value());
289
290 1
            if ($map === null) {
291 1
                $type = gettype($key);
292 1
                $map = new self(
293 1
                    $type === 'object' ? get_class($key) : $type,
294 1
                    SequenceInterface::class
295
                );
296
            }
297
298 1
            if ($map->contains($key)) {
299 1
                $map = $map->put(
300
                    $key,
301 1
                    $map->get($key)->add($pair)
302
                );
303
            } else {
304 1
                $map = $map->put($key, new Sequence($pair));
305
            }
306
        }
307
308 1
        return $map;
309
    }
310
311
    /**
312
     * {@inheritdoc}
313
     */
314 1
    public function first(): Pair
315
    {
316 1
        return $this->pairs->first();
317
    }
318
319
    /**
320
     * {@inheritdoc}
321
     */
322 1
    public function last(): Pair
323
    {
324 1
        return $this->pairs->last();
325
    }
326
327
    /**
328
     * {@inheritdoc}
329
     */
330 9
    public function keys(): SequenceInterface
331
    {
332 9
        return $this->keys;
333
    }
334
335
    /**
336
     * {@inheritdoc}
337
     */
338 7
    public function values(): SequenceInterface
339
    {
340 7
        return $this->values;
341
    }
342
343
    /**
344
     * {@inheritdoc}
345
     */
346 1
    public function map(\Closure $function): MapInterface
347
    {
348 1
        $map = $this->clear();
349
350 1
        foreach ($this->pairs as $pair) {
351 1
            $return = $function(
352 1
                $pair->key(),
353 1
                $pair->value()
354
            );
355
356 1
            if ($return instanceof Pair) {
357 1
                $key = $return->key();
358 1
                $value = $return->value();
359
            } else {
360 1
                $key = $pair->key();
361 1
                $value = $return;
362
            }
363
364 1
            $map = $map->put($key, $value);
365
        }
366
367 1
        return $map;
368
    }
369
370
    /**
371
     * {@inheritdoc}
372
     */
373 1 View Code Duplication
    public function take(int $size): MapInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
374
    {
375 1
        $map = clone $this;
376 1
        $map->keys = $this->keys->take($size);
377 1
        $map->values = $this->values->take($size);
378 1
        $map->pairs = $this->pairs->take($size);
379
380 1
        return $map;
381
    }
382
383
    /**
384
     * {@inheritdoc}
385
     */
386 1 View Code Duplication
    public function takeEnd(int $size): MapInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
387
    {
388 1
        $map = clone $this;
389 1
        $map->keys = $this->keys->takeEnd($size);
390 1
        $map->values = $this->values->takeEnd($size);
391 1
        $map->pairs = $this->pairs->takeEnd($size);
392
393 1
        return $map;
394
    }
395
396
    /**
397
     * {@inheritdoc}
398
     */
399 1
    public function join(string $separator): StringPrimitive
400
    {
401 1
        return $this->values->join($separator);
402
    }
403
404
    /**
405
     * {@inheritdoc}
406
     */
407 1
    public function remove($key): MapInterface
408
    {
409 1
        if (!$this->contains($key)) {
410 1
            return $this;
411
        }
412
413 1
        $index = $this->keys->indexOf($key);
414 1
        $map = clone $this;
415 1
        $map->keys = $this
416 1
            ->keys
417 1
            ->slice(0, $index)
418 1
            ->append($this->keys->slice($index + 1, $this->keys->size()));
419 1
        $map->values = $this
420 1
            ->values
421 1
            ->slice(0, $index)
422 1
            ->append($this->values->slice($index + 1, $this->values->size()));
423 1
        $map->pairs = $this
424 1
            ->pairs
425 1
            ->slice(0, $index)
426 1
            ->append($this->pairs->slice($index + 1, $this->pairs->size()));
427
428 1
        return $map;
429
    }
430
431
    /**
432
     * {@inheritdoc}
433
     */
434 2
    public function merge(MapInterface $map): MapInterface
435
    {
436
        if (
437 2
            !$this->keyType()->equals($map->keyType()) ||
438 2
            !$this->valueType()->equals($map->valueType())
439
        ) {
440 1
            throw new InvalidArgumentException(
441 1
                'The 2 maps does not reference the same types'
442
            );
443
        }
444
445 1
        $newMap = clone $this;
446
447 1
        $map->foreach(function($key, $value) use (&$newMap) {
448 1
            $newMap = $newMap->put($key, $value);
449 1
        });
450
451 1
        return $newMap;
452
    }
453
454
    /**
455
     * {@inheritdoc}
456
     */
457 1
    public function partition(\Closure $predicate): MapInterface
458
    {
459 1
        $truthy = $this->clear();
460 1
        $falsy = $this->clear();
461
462 1
        foreach ($this->pairs as $pair) {
463 1
            $return = $predicate(
464 1
                $pair->key(),
465 1
                $pair->value()
466
            );
467
468 1
            if ($return === true) {
469 1
                $truthy = $truthy->put($pair->key(), $pair->value());
470
            } else {
471 1
                $falsy = $falsy->put($pair->key(), $pair->value());
472
            }
473
        }
474
475 1
        return (new self('bool', MapInterface::class))
476 1
            ->put(true, $truthy)
477 1
            ->put(false, $falsy);
478
    }
479
480
    /**
481
     * {@inheritdoc}
482
     */
483 1
    public function reduce($carry, \Closure $reducer)
484
    {
485 1
        foreach ($this->pairs as $pair) {
486 1
            $carry = $reducer($carry, $pair->key(), $pair->value());
487
        }
488
489 1
        return $carry;
490
    }
491
}
492