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 — develop ( 870d79...a1e45f )
by Baptiste
05:29
created

Sequence::ints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\{
7
    LogicException,
8
    CannotGroupEmptyStructure,
9
    ElementNotFound,
10
};
11
12
/**
13
 * @template T
14
 */
15
final class Sequence implements \Countable
16
{
17
    private string $type;
18
    private ValidateArgument $validate;
19
    private Sequence\Implementation $implementation;
20
21 176
    private function __construct(string $type)
22
    {
23 176
        $this->type = $type;
24 176
        $this->validate = Type::of($type);
25 176
        $this->implementation = new Sequence\Primitive($type);
26 176
    }
27
28
    /**
29
     * @param T $values
30
     */
31 171
    public static function of(string $type, ...$values): self
32
    {
33 171
        $self = new self($type);
34 171
        $self->implementation = new Sequence\Primitive($type, ...$values);
35 171
        $self->implementation->reduce(
36 171
            1,
37
            static function(int $position, $element) use ($self): int {
38 68
                ($self->validate)($element, $position);
39
40 68
                return ++$position;
41 171
            }
42
        );
43
44 171
        return $self;
45
    }
46
47
    /**
48
     * @param mixed $values
49
     *
50
     * @return self<mixed>
51
     */
52 1
    public static function mixed(...$values): self
53
    {
54 1
        $self = new self('mixed');
55 1
        $self->implementation = new Sequence\Primitive('mixed', ...$values);
56
57 1
        return $self;
58
    }
59
60
    /**
61
     * @return self<int>
62
     */
63 2
    public static function ints(int ...$values): self
64
    {
65 2
        $self = new self('int');
66 2
        $self->implementation = new Sequence\Primitive('int', ...$values);
67
68 2
        return $self;
69
    }
70
71
    /**
72
     * @return self<float>
73
     */
74 1
    public static function floats(float ...$values): self
75
    {
76 1
        $self = new self('float');
77 1
        $self->implementation = new Sequence\Primitive('float', ...$values);
78
79 1
        return $self;
80
    }
81
82
    /**
83
     * @return self<string>
84
     */
85 1
    public static function strings(string ...$values): self
86
    {
87 1
        $self = new self('string');
88 1
        $self->implementation = new Sequence\Primitive('string', ...$values);
89
90 1
        return $self;
91
    }
92
93
    /**
94
     * @return self<object>
95
     */
96 1
    public static function objects(object ...$values): self
97
    {
98 1
        $self = new self('object');
99 1
        $self->implementation = new Sequence\Primitive('object', ...$values);
100
101 1
        return $self;
102
    }
103
104 35
    public function isOfType(string $type): bool
105
    {
106 35
        return $this->type === $type;
107
    }
108
109
    /**
110
     * Type of the elements
111
     */
112 38
    public function type(): string
113
    {
114 38
        return $this->type;
115
    }
116
117 23
    public function size(): int
118
    {
119 23
        return $this->implementation->size();
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 8
    public function count(): int
126
    {
127 8
        return $this->implementation->size();
128
    }
129
130
    /**
131
     * @return list<T>
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
132
     */
133 101
    public function toArray(): array
134
    {
135 101
        return $this->implementation->toArray();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->implementation->toArray() returns the type array which is incompatible with the documented return type Innmind\Immutable\list.
Loading history...
136
    }
137
138
    /**
139
     * Return the element at the given index
140
     *
141
     * @throws OutOfBoundException
142
     *
143
     * @return T
144
     */
145 26
    public function get(int $index)
146
    {
147 26
        return $this->implementation->get($index);
148
    }
149
150
    /**
151
     * Return the diff between this sequence and another
152
     *
153
     * @param self<T> $sequence
154
     *
155
     * @return self<T>
156
     */
157 3
    public function diff(self $sequence): self
158
    {
159 3
        assertSequence($this->type, $sequence, 1);
160
161 3
        $self = clone $this;
162 3
        $self->implementation = $this->implementation->diff(
163 3
            $sequence->implementation
164
        );
165
166 3
        return $self;
167
    }
168
169
    /**
170
     * Remove all duplicates from the sequence
171
     *
172
     * @return self<T>
173
     */
174 81
    public function distinct(): self
175
    {
176 81
        $self = clone $this;
177 81
        $self->implementation = $this->implementation->distinct();
178
179 81
        return $self;
180
    }
181
182
    /**
183
     * Remove the n first elements
184
     *
185
     * @return self<T>
186
     */
187 5
    public function drop(int $size): self
188
    {
189 5
        $self = clone $this;
190 5
        $self->implementation = $this->implementation->drop($size);
191
192 5
        return $self;
193
    }
194
195
    /**
196
     * Remove the n last elements
197
     *
198
     * @return self<T>
199
     */
200 1
    public function dropEnd(int $size): self
201
    {
202 1
        $self = clone $this;
203 1
        $self->implementation = $this->implementation->dropEnd($size);
204
205 1
        return $self;
206
    }
207
208
    /**
209
     * Check if the two sequences are equal
210
     *
211
     * @param self<T> $sequence
212
     */
213 7
    public function equals(self $sequence): bool
214
    {
215 7
        assertSequence($this->type, $sequence, 1);
216
217 6
        return $this->implementation->equals(
218 6
            $sequence->implementation
219
        );
220
    }
221
222
    /**
223
     * Return all elements that satisfy the given predicate
224
     *
225
     * @param callable(T): bool $predicate
226
     *
227
     * @return self<T>
228
     */
229 3
    public function filter(callable $predicate): self
230
    {
231 3
        $self = clone $this;
232 3
        $self->implementation = $this->implementation->filter($predicate);
233
234 3
        return $self;
235
    }
236
237
    /**
238
     * Apply the given function to all elements of the sequence
239
     *
240
     * @param callable(T): void $function
241
     */
242 3
    public function foreach(callable $function): void
243
    {
244 3
        $this->implementation->foreach($function);
245 3
    }
246
247
    /**
248
     * Return a new map of pairs grouped by keys determined with the given
249
     * discriminator function
250
     *
251
     * @param callable(T) $discriminator
252
     *
253
     * @throws CannotGroupEmptyStructure
254
     *
255
     * @return Map<mixed, self<T>>
256
     */
257 4
    public function groupBy(callable $discriminator): Map
258
    {
259 4
        return $this->implementation->groupBy($discriminator);
260
    }
261
262
    /**
263
     * Return the first element
264
     *
265
     * @return T
266
     */
267 3
    public function first()
268
    {
269 3
        return $this->implementation->first();
270
    }
271
272
    /**
273
     * Return the last element
274
     *
275
     * @return T
276
     */
277 3
    public function last()
278
    {
279 3
        return $this->implementation->last();
280
    }
281
282
    /**
283
     * Check if the sequence contains the given element
284
     *
285
     * @param T $element
286
     */
287 60
    public function contains($element): bool
288
    {
289 60
        ($this->validate)($element, 1);
290
291 60
        return $this->implementation->contains($element);
292
    }
293
294
    /**
295
     * Return the index for the given element
296
     *
297
     * @param T $element
298
     *
299
     * @throws ElementNotFound
300
     */
301 21
    public function indexOf($element): int
302
    {
303 21
        ($this->validate)($element, 1);
304
305 21
        return $this->implementation->indexOf($element);
306
    }
307
308
    /**
309
     * Return the list of indices
310
     *
311
     * @return self<int>
312
     */
313 2
    public function indices(): self
314
    {
315 2
        $self = new self('int');
316 2
        $self->implementation = $this->implementation->indices();
317
318 2
        return $self;
319
    }
320
321
    /**
322
     * Return a new sequence by applying the given function to all elements
323
     *
324
     * @param callable(T): T $function
325
     *
326
     * @return self<T>
327
     */
328 3
    public function map(callable $function): self
329
    {
330 3
        $self = clone $this;
331 3
        $self->implementation = $this->implementation->map($function);
332
333 2
        return $self;
334
    }
335
336
    /**
337
     * Pad the sequence to a defined size with the given element
338
     *
339
     * @param T $element
340
     *
341
     * @return self<T>
342
     */
343 2
    public function pad(int $size, $element): self
344
    {
345 2
        ($this->validate)($element, 2);
346
347 1
        $self = clone $this;
348 1
        $self->implementation = $this->implementation->pad($size, $element);
349
350 1
        return $self;
351
    }
352
353
    /**
354
     * Return a sequence of 2 sequences partitioned according to the given predicate
355
     *
356
     * @param callable(T): bool $predicate
357
     *
358
     * @return Map<bool, self<T>>
359
     */
360 3
    public function partition(callable $predicate): Map
361
    {
362 3
        return $this->implementation->partition($predicate);
363
    }
364
365
    /**
366
     * Slice the sequence
367
     *
368
     * @return self<T>
369
     */
370 4
    public function slice(int $from, int $until): self
371
    {
372 4
        $self = clone $this;
373 4
        $self->implementation = $this->implementation->slice($from, $until);
374
375 4
        return $self;
376
    }
377
378
    /**
379
     * Split the sequence in a sequence of 2 sequences splitted at the given position
380
     *
381
     * @throws OutOfBoundException
382
     *
383
     * @return self<self<T>>
384
     */
385 1
    public function splitAt(int $position): self
386
    {
387 1
        return $this->implementation->splitAt($position);
388
    }
389
390
    /**
391
     * Return a sequence with the n first elements
392
     *
393
     * @return self<T>
394
     */
395 5
    public function take(int $size): self
396
    {
397 5
        $self = clone $this;
398 5
        $self->implementation = $this->implementation->take($size);
399
400 5
        return $self;
401
    }
402
403
    /**
404
     * Return a sequence with the n last elements
405
     *
406
     * @return self<T>
407
     */
408 1
    public function takeEnd(int $size): self
409
    {
410 1
        $self = clone $this;
411 1
        $self->implementation = $this->implementation->takeEnd($size);
412
413 1
        return $self;
414
    }
415
416
    /**
417
     * Append the given sequence to the current one
418
     *
419
     * @param self<T> $sequence
420
     *
421
     * @return self<T>
422
     */
423 9
    public function append(self $sequence): self
424
    {
425 9
        assertSequence($this->type, $sequence, 1);
426
427 8
        $self = clone $this;
428 8
        $self->implementation = $this->implementation->append(
429 8
            $sequence->implementation
430
        );
431
432 8
        return $self;
433
    }
434
435
    /**
436
     * Return a sequence with all elements from the current one that exist
437
     * in the given one
438
     *
439
     * @param self<T> $sequence
440
     *
441
     * @return self<T>
442
     */
443 15
    public function intersect(self $sequence): self
444
    {
445 15
        assertSequence($this->type, $sequence, 1);
446
447 14
        $self = clone $this;
448 14
        $self->implementation = $this->implementation->intersect(
449 14
            $sequence->implementation
450
        );
451
452 14
        return $self;
453
    }
454
455
    /**
456
     * Concatenate all elements with the given separator
457
     */
458 11
    public function join(string $separator): Str
459
    {
460 11
        return $this->implementation->join($separator);
461
    }
462
463
    /**
464
     * Add the given element at the end of the sequence
465
     *
466
     * @param T $element
467
     *
468
     * @return self<T>
469
     */
470 108
    public function add($element): self
471
    {
472 108
        ($this->validate)($element, 1);
473
474 107
        $self = clone $this;
475 107
        $self->implementation = $this->implementation->add($element);
476
477 107
        return $self;
478
    }
479
480
    /**
481
     * Alias for add method in order to have a syntax similar to a true tuple
482
     * when constructing the sequence
483
     *
484
     * Example:
485
     * <code>
486
     * Sequence::of('int')(1)(3)
487
     * </code>
488
     *
489
     * @param T $element
490
     *
491
     * @return self<T>
492
     */
493 1
    public function __invoke($element): self
494
    {
495 1
        return $this->add($element);
496
    }
497
498
    /**
499
     * Sort the sequence in a different order
500
     *
501
     * @param callable(T, T): int $function
502
     *
503
     * @return self<T>
504
     */
505 3
    public function sort(callable $function): self
506
    {
507 3
        $self = clone $this;
508 3
        $self->implementation = $this->implementation->sort($function);
509
510 3
        return $self;
511
    }
512
513
    /**
514
     * Reduce the sequence to a single value
515
     *
516
     * @param mixed $carry
517
     * @param callable(mixed, T) $reducer
518
     *
519
     * @return mixed
520
     */
521 10
    public function reduce($carry, callable $reducer)
522
    {
523 10
        return $this->implementation->reduce($carry, $reducer);
524
    }
525
526
    /**
527
     * Return a set of the same type but without any value
528
     *
529
     * @return self<T>
530
     */
531 8
    public function clear(): self
532
    {
533 8
        $self = clone $this;
534 8
        $self->implementation = new Sequence\Primitive($this->type);
535
536 8
        return $self;
537
    }
538
539
    /**
540
     * Return the same sequence but in reverse order
541
     *
542
     * @return self<T>
543
     */
544 2
    public function reverse(): self
545
    {
546 2
        $self = clone $this;
547 2
        $self->implementation = $this->implementation->reverse();
548
549 2
        return $self;
550
    }
551
552 4
    public function empty(): bool
553
    {
554 4
        return $this->implementation->empty();
555
    }
556
}
557