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 ( bb0fa7...ea46e4 )
by Baptiste
02:12
created

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