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 ( ea46e4...3fd0c3 )
by Baptiste
02:52
created

TypedCollection::unset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\InvalidArgumentException;
7
use Innmind\Immutable\Exception\BadMethodCallException;
8
9
class TypedCollection extends Collection implements TypedCollectionInterface
10
{
11
    private $type;
12
13
    /**
14
     * Constructor
15
     *
16
     * @param string $type The class every element must respect
17
     * @param array $values
18
     */
19 71
    public function __construct($type, array $values)
20
    {
21 71
        $type = (string) $type;
22 71
        $this->validate($type, $values);
23
24 70
        $this->type = $type;
25 70
        parent::__construct($values);
26 70
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 61
    public function getType(): string
32
    {
33 61
        return $this->type;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function type(): string
40
    {
41 1
        return $this->type;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function filter(callable $filter = null): CollectionInterface
48
    {
49 1
        return new self(
50 1
            $this->type,
51 1
            parent::filter($filter)->toPrimitive()
52
        );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 3
    public function intersect(CollectionInterface $collection): CollectionInterface
59
    {
60 3
        $this->validateCollection($collection);
61
62 1
        return new self(
63 1
            $this->type,
64 1
            parent::intersect($collection)->toPrimitive()
65
        );
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1 View Code Duplication
    public function chunk(int $size): CollectionInterface
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...
72
    {
73 1
        $chunks = parent::chunk($size);
74 1
        $subs = [];
75
76 1
        foreach ($chunks as $chunk) {
77 1
            $subs[] = new self(
78 1
                $this->type,
79 1
                $chunk->toPrimitive()
80
            );
81
        }
82
83 1
        return new parent($subs);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    public function shift(): CollectionInterface
90
    {
91 1
        return new self(
92 1
            $this->type,
93 1
            parent::shift()->toPrimitive()
94
        );
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 1
    public function uintersect(CollectionInterface $collection, callable $intersecter): CollectionInterface
101
    {
102 1
        $this->validateCollection($collection);
103
104 1
        return new self(
105 1
            $this->type,
106 1
            parent::uintersect($collection, $intersecter)->toPrimitive()
107
        );
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 2
    public function keyIntersect(CollectionInterface $collection): CollectionInterface
114
    {
115 2
        $this->validateCollection($collection);
116
117 1
        return new self(
118 1
            $this->type,
119 1
            parent::keyIntersect($collection)->toPrimitive()
120
        );
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 2
    public function map(callable $mapper): CollectionInterface
127
    {
128 2
        return new self(
129 2
            $this->type,
130 2
            parent::map($mapper)->toPrimitive()
131
        );
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 2 View Code Duplication
    public function pad(int $size, $value): CollectionInterface
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...
138
    {
139 2
        $this->validate($this->type, [$value]);
140
141 1
        return new self(
142 1
            $this->type,
143 1
            parent::pad($size, $value)->toPrimitive()
144
        );
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 1
    public function pop(): CollectionInterface
151
    {
152 1
        return new self(
153 1
            $this->type,
154 1
            parent::pop()->toPrimitive()
155
        );
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 2
    public function diff(CollectionInterface $collection): CollectionInterface
162
    {
163 2
        $this->validateCollection($collection);
164
165 1
        return new self(
166 1
            $this->type,
167 1
            parent::diff($collection)->toPrimitive()
168
        );
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174 2 View Code Duplication
    public function push($value): CollectionInterface
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...
175
    {
176 2
        $this->validate($this->type, [$value]);
177
178 1
        return new self(
179 1
            $this->type,
180 1
            parent::push($value)->toPrimitive()
181
        );
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 2
    public function rand(int $num = 1): CollectionInterface
188
    {
189 2
        return new self(
190 2
            $this->type,
191 2
            parent::rand($num)->toPrimitive()
192
        );
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198 2
    public function merge(CollectionInterface $collection): CollectionInterface
199
    {
200 2
        $this->validateCollection($collection);
201
202 1
        return new self(
203 1
            $this->type,
204 1
            parent::merge($collection)->toPrimitive()
205
        );
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211 1
    public function slice(int $offset, int $length = null, bool $preserveKeys = false): CollectionInterface
212
    {
213 1
        return new self(
214 1
            $this->type,
215 1
            parent::slice($offset, $length, $preserveKeys)->toPrimitive()
216
        );
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222 2
    public function udiff(CollectionInterface $collection, callable $differ): CollectionInterface
223
    {
224 2
        $this->validateCollection($collection);
225
226 1
        return new self(
227 1
            $this->type,
228 1
            parent::udiff($collection, $differ)->toPrimitive()
229
        );
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 1
    public function splice(int $offset, int $length = 0, $replacement = []): CollectionInterface
236
    {
237 1
        return new self(
238 1
            $this->type,
239 1
            parent::splice($offset, $length, $replacement)->toPrimitive()
240
        );
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246 1
    public function unique(int $flags = self::SORT_REGULAR): CollectionInterface
247
    {
248 1
        return new self(
249 1
            $this->type,
250 1
            parent::unique($flags)->toPrimitive()
251
        );
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257 1
    public function values(): CollectionInterface
258
    {
259 1
        return new self(
260 1
            $this->type,
261 1
            parent::values()->toPrimitive()
262
        );
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268 2
    public function replace(CollectionInterface $collection): CollectionInterface
269
    {
270 2
        $this->validateCollection($collection);
271
272 1
        return new self(
273 1
            $this->type,
274 1
            parent::replace($collection)->toPrimitive()
275
        );
276
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281 1
    public function reverse(bool $preserveKeys = false): CollectionInterface
282
    {
283 1
        return new self(
284 1
            $this->type,
285 1
            parent::reverse($preserveKeys)->toPrimitive()
286
        );
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292 2 View Code Duplication
    public function unshift($value): CollectionInterface
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...
293
    {
294 2
        $this->validate($this->type, [$value]);
295
296 1
        return new self(
297 1
            $this->type,
298 1
            parent::unshift($value)->toPrimitive()
299
        );
300
    }
301
302
    /**
303
     * {@inheritdoc}
304
     */
305 2
    public function keyDiff(CollectionInterface $collection): CollectionInterface
306
    {
307 2
        $this->validateCollection($collection);
308
309 1
        return new self(
310 1
            $this->type,
311 1
            parent::keyDiff($collection)->toPrimitive()
312
        );
313
    }
314
315
    /**
316
     * {@inheritdoc}
317
     */
318 2
    public function ukeyDiff(CollectionInterface $collection, callable $differ): CollectionInterface
319
    {
320 2
        $this->validateCollection($collection);
321
322 1
        return new self(
323 1
            $this->type,
324 1
            parent::ukeyDiff($collection, $differ)->toPrimitive()
325
        );
326
    }
327
328
    /**
329
     * {@inheritdoc}
330
     */
331 2
    public function associativeDiff(CollectionInterface $collection): CollectionInterface
332
    {
333 2
        $this->validateCollection($collection);
334
335 1
        return new self(
336 1
            $this->type,
337 1
            parent::associativeDiff($collection)->toPrimitive()
338
        );
339
    }
340
341
    /**
342
     * {@inheritdoc}
343
     */
344 2
    public function ukeyIntersect(CollectionInterface $collection, callable $intersecter): CollectionInterface
345
    {
346 2
        $this->validateCollection($collection);
347
348 1
        return new self(
349 1
            $this->type,
350 1
            parent::ukeyIntersect($collection, $intersecter)->toPrimitive()
351
        );
352
    }
353
354
    /**
355
     * {@inheritdoc}
356
     */
357 2
    public function associativeIntersect(CollectionInterface $collection): CollectionInterface
358
    {
359 2
        $this->validateCollection($collection);
360
361 1
        return new self(
362 1
            $this->type,
363 1
            parent::associativeIntersect($collection)->toPrimitive()
364
        );
365
    }
366
367
    /**
368
     * {@inheritdoc}
369
     */
370 1
    public function sort(int $flags = self::SORT_REGULAR): CollectionInterface
371
    {
372 1
        return new self(
373 1
            $this->type,
374 1
            parent::sort($flags)->toPrimitive()
375
        );
376
    }
377
378
    /**
379
     * {@inheritdoc}
380
     */
381 1
    public function associativeSort(int $flags = self::SORT_REGULAR): CollectionInterface
382
    {
383 1
        return new self(
384 1
            $this->type,
385 1
            parent::associativeSort($flags)->toPrimitive()
386
        );
387
    }
388
389
    /**
390
     * {@inheritdoc}
391
     */
392 1
    public function keySort(int $flags = self::SORT_REGULAR): CollectionInterface
393
    {
394 1
        return new self(
395 1
            $this->type,
396 1
            parent::keySort($flags)->toPrimitive()
397
        );
398
    }
399
400
    /**
401
     * {@inheritdoc}
402
     */
403 1
    public function ukeySort(callable $sorter): CollectionInterface
404
    {
405 1
        return new self(
406 1
            $this->type,
407 1
            parent::ukeySort($sorter)->toPrimitive()
408
        );
409
    }
410
411
    /**
412
     * {@inheritdoc}
413
     */
414 1
    public function reverseSort(int $flags = self::SORT_REGULAR): CollectionInterface
415
    {
416 1
        return new self(
417 1
            $this->type,
418 1
            parent::reverseSort($flags)->toPrimitive()
419
        );
420
    }
421
422
    /**
423
     * {@inheritdoc}
424
     */
425 1
    public function usort(callable $sorter): CollectionInterface
426
    {
427 1
        return new self(
428 1
            $this->type,
429 1
            parent::usort($sorter)->toPrimitive()
430
        );
431
    }
432
433
    /**
434
     * {@inheritdoc}
435
     */
436 1
    public function associativeReverseSort(int $flags = self::SORT_REGULAR): CollectionInterface
437
    {
438 1
        return new self(
439 1
            $this->type,
440 1
            parent::associativeReverseSort($flags)->toPrimitive()
441
        );
442
    }
443
444
    /**
445
     * {@inheritdoc}
446
     */
447 1
    public function keyReverseSort(int $flags = self::SORT_REGULAR): CollectionInterface
448
    {
449 1
        return new self(
450 1
            $this->type,
451 1
            parent::keyReverseSort($flags)->toPrimitive()
452
        );
453
    }
454
455
    /**
456
     * {@inheritdoc}
457
     */
458 1
    public function uassociativeSort(callable $sorter): CollectionInterface
459
    {
460 1
        return new self(
461 1
            $this->type,
462 1
            parent::uassociativeSort($sorter)->toPrimitive()
463
        );
464
    }
465
466
    /**
467
     * {@inheritdoc}
468
     */
469 1
    public function naturalSort(): CollectionInterface
470
    {
471 1
        return new self(
472 1
            $this->type,
473 1
            parent::naturalSort()->toPrimitive()
474
        );
475
    }
476
477
    /**
478
     * {@inheritdoc}
479
     */
480 1
    public function shuffle(): CollectionInterface
481
    {
482 1
        return new self(
483 1
            $this->type,
484 1
            parent::shuffle()->toPrimitive()
485
        );
486
    }
487
488
    /**
489
     * {@inheritdoc}
490
     */
491 1
    public function take(int $size, bool $preserveKeys = false): CollectionInterface
492
    {
493 1
        return new self(
494 1
            $this->type,
495 1
            parent::take($size, $preserveKeys)->toPrimitive()
496
        );
497
    }
498
499
    /**
500
     * {@inheritdoc}
501
     */
502 1
    public function grep(string $pattern, bool $revert = false): CollectionInterface
503
    {
504 1
        return new self(
505 1
            $this->type,
506 1
            parent::grep($pattern, $revert)->toPrimitive()
507
        );
508
    }
509
510
    /**
511
     * {@inheritdoc}
512
     */
513 2 View Code Duplication
    public function set($key, $value): CollectionInterface
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...
514
    {
515 2
        $this->validate($this->type, [$value]);
516
517 1
        return new self(
518 1
            $this->type,
519 1
            parent::set($key, $value)->toPrimitive()
520
        );
521
    }
522
523
    /**
524
     * {@inheritdoc}
525
     */
526 1
    public function contains($value): bool
527
    {
528
        //avoid searching the collection if we know it's not of the same type
529
        try {
530 1
            $this->validate($this->type, [$value]);
531 1
        } catch (InvalidArgumentException $e) {
532 1
            return false;
533
        }
534
535 1
        return parent::contains($value);
536
    }
537
538
    /**
539
     * {@inheritdoc}
540
     */
541 1
    public function walk(callable $walker): CollectionInterface
542
    {
543 1
        return new self(
544 1
            $this->type,
545 1
            parent::walk($walker)->toPrimitive()
546
        );
547
    }
548
549
    /**
550
     * {@inheritdoc}
551
     */
552 2
    public function unset($index): CollectionInterface
553
    {
554 2
        return new self(
555 2
            $this->type,
556 2
            parent::unset($index)->toPrimitive()
557
        );
558
    }
559
560
    /**
561
     * Check if every element respect the given type
562
     *
563
     * @throws InvalidArgumentException If a value doesn't respect the type
564
     *
565
     * @param string $type
566
     * @param array $values
567
     *
568
     * @return void
569
     */
570 71
    protected function validate(string $type, array $values)
571
    {
572 71
        foreach ($values as $value) {
573 57
            if (!$value instanceof $type) {
574 6
                throw new InvalidArgumentException(sprintf(
575 57
                    'Each value must be an instance of "%s"',
576
                    $type
577
                ));
578
            }
579
        }
580 70
    }
581
582
    /**
583
     * Check if the given collection is compatible with the current one
584
     *
585
     * @throws BadMethodCallException If the collection is not compatible
586
     *
587
     * @param CollectionInterface $collection
588
     *
589
     * @return void
590
     */
591 24
    private function validateCollection(CollectionInterface $collection)
592
    {
593
        if (
594 24
            !$collection instanceof self ||
595 24
            $collection->getType() !== $this->type
596
        ) {
597 12
            throw new BadMethodCallException(
598 12
                'The given collection is not compatible'
599
            );
600
        }
601 12
    }
602
}
603