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 ( 9c20fb...b12bea )
by Baptiste
05:26
created

Sequence::empty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\{
7
    Exception\OutOfBoundException,
8
    Exception\LogicException,
9
    Exception\ElementNotFoundException,
10
    Exception\GroupEmptySequenceException
11
};
12
13
/**
14
 * A defined set of ordered elements
15
 */
16
class Sequence implements SequenceInterface
17
{
18
    private $values;
19
    private $size;
20
21 388
    public function __construct(...$values)
22
    {
23 388
        $this->values = $values;
24 388
    }
25
26 6
    public static function of(...$values): self
27
    {
28 6
        $self = new self;
29 6
        $self->values = $values;
30
31 6
        return $self;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 268
    public function size(): int
38
    {
39 268
        return $this->size ?? $this->size = \count($this->values);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function count(): int
46
    {
47 2
        return $this->size();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 88
    public function current()
54
    {
55 88
        return \current($this->values);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 92
    public function key()
62
    {
63 92
        return \key($this->values);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 88
    public function next()
70
    {
71 88
        \next($this->values);
72 88
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 88
    public function rewind()
78
    {
79 88
        \reset($this->values);
80 88
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 88
    public function valid(): bool
86
    {
87 88
        return $this->key() !== null;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 2
    public function offsetExists($offset): bool
94
    {
95 2
        return $this->has($offset);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 2
    public function offsetGet($offset)
102
    {
103 2
        return $this->get($offset);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 2
    public function offsetSet($offset, $value)
110
    {
111 2
        throw new LogicException('You can\'t modify a sequence');
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 2
    public function offsetUnset($offset)
118
    {
119 2
        throw new LogicException('You can\'t modify a sequence');
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 192
    public function toPrimitive()
126
    {
127 192
        return $this->values;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 62
    public function get(int $index)
134
    {
135 62
        if (!$this->has($index)) {
136 6
            throw new OutOfBoundException;
137
        }
138
139 56
        return $this->values[$index];
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 74
    public function has(int $index): bool
146
    {
147 74
        return \array_key_exists($index, $this->values);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function diff(SequenceInterface $seq): SequenceInterface
154
    {
155 8
        return $this->filter(static function($value) use ($seq): bool {
156 8
            return !$seq->contains($value);
157 8
        });
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 66
    public function distinct(): SequenceInterface
164
    {
165 66
        return $this->reduce(
166 66
            new self,
167 66
            static function(self $values, $value): self {
168 52
                if ($values->contains($value)) {
169 8
                    return $values;
170
                }
171
172 52
                return $values->add($value);
173 66
            }
174
        );
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 10
    public function drop(int $size): SequenceInterface
181
    {
182 10
        $self = new self;
183 10
        $self->values = \array_slice($this->values, $size);
184
185 10
        return $self;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 4
    public function dropEnd(int $size): SequenceInterface
192
    {
193 4
        $self = new self;
194 4
        $self->values = \array_slice($this->values, 0, $this->size() - $size);
195
196 4
        return $self;
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202 16
    public function equals(SequenceInterface $seq): bool
203
    {
204 16
        return $this->values === $seq->toPrimitive();
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210 40
    public function filter(callable $predicate): SequenceInterface
211
    {
212 40
        return new self(...\array_filter(
213 40
            $this->values,
214 40
            $predicate
215
        ));
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221 102
    public function foreach(callable $function): SequenceInterface
222
    {
223 102
        foreach ($this->values as $value) {
224 84
            $function($value);
225
        }
226
227 100
        return $this;
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233 4
    public function groupBy(callable $discriminator): MapInterface
234
    {
235 4
        if ($this->size() === 0) {
236 2
            throw new GroupEmptySequenceException;
237
        }
238
239 2
        $map = null;
240
241 2
        foreach ($this->values as $value) {
242 2
            $key = $discriminator($value);
243
244 2
            if ($map === null) {
245 2
                $map = new Map(
246 2
                    Type::determine($key),
247 2
                    SequenceInterface::class
248
                );
249
            }
250
251 2
            if ($map->contains($key)) {
252 2
                $map = $map->put(
253 2
                    $key,
254 2
                    $map->get($key)->add($value)
255
                );
256
            } else {
257 2
                $map = $map->put($key, new self($value));
258
            }
259
        }
260
261 2
        return $map;
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267 8
    public function first()
268
    {
269 8
        if ($this->size() === 0) {
270 2
            throw new OutOfBoundException;
271
        }
272
273 6
        return $this->values[0];
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279 8
    public function last()
280
    {
281 8
        if ($this->size() === 0) {
282 2
            throw new OutOfBoundException;
283
        }
284
285 6
        return $this->values[$this->size() - 1];
286
    }
287
288
    /**
289
     * {@inheritdoc}
290
     */
291 158
    public function contains($element): bool
292
    {
293 158
        return \in_array($element, $this->values, true);
294
    }
295
296
    /**
297
     * {@inheritdoc}
298
     */
299 42
    public function indexOf($element): int
300
    {
301 42
        $index = \array_search($element, $this->values, true);
302
303 42
        if ($index === false) {
304 2
            throw new ElementNotFoundException;
305
        }
306
307 40
        return $index;
308
    }
309
310
    /**
311
     * {@inheritdoc}
312
     */
313 8
    public function indices(): StreamInterface
314
    {
315 8
        if ($this->size() === 0) {
316 4
            return Stream::of('int');
317
        }
318
319 4
        return Stream::of('int', ...\range(0, $this->size() - 1));
320
    }
321
322
    /**
323
     * {@inheritdoc}
324
     */
325 8
    public function map(callable $function): SequenceInterface
326
    {
327 8
        $self = clone $this;
328 8
        $self->values = \array_map($function, $this->values);
329
330 8
        return $self;
331
    }
332
333
    /**
334
     * {@inheritdoc}
335
     */
336 4
    public function pad(int $size, $element): SequenceInterface
337
    {
338 4
        $self = new self;
339 4
        $self->values = \array_pad($this->values, $size, $element);
340
341 4
        return $self;
342
    }
343
344
    /**
345
     * {@inheritdoc}
346
     */
347 2
    public function partition(callable $predicate): MapInterface
348
    {
349 2
        $truthy = [];
350 2
        $falsy = [];
351
352 2
        foreach ($this->values as $value) {
353 2
            if ($predicate($value) === true) {
354 2
                $truthy[] = $value;
355
            } else {
356 2
                $falsy[] = $value;
357
            }
358
        }
359
360 2
        $true = new self;
361 2
        $true->values = $truthy;
362 2
        $false = new self;
363 2
        $false->values = $falsy;
364
365 2
        return Map::of('bool', SequenceInterface::class)
366 2
            (true, $true)
367 2
            (false, $false);
368
    }
369
370
    /**
371
     * {@inheritdoc}
372
     */
373 26
    public function slice(int $from, int $until): SequenceInterface
374
    {
375 26
        $self = new self;
376 26
        $self->values = \array_slice(
377 26
            $this->values,
378 26
            $from,
379 26
            $until - $from
380
        );
381
382 26
        return $self;
383
    }
384
385
    /**
386
     * {@inheritdoc}
387
     */
388 4
    public function splitAt(int $index): StreamInterface
389
    {
390 4
        return (new Stream(SequenceInterface::class))
391 4
            ->add($this->slice(0, $index))
392 4
            ->add($this->slice($index, $this->size()));
393
    }
394
395
    /**
396
     * {@inheritdoc}
397
     */
398 10
    public function take(int $size): SequenceInterface
399
    {
400 10
        return $this->slice(0, $size);
401
    }
402
403
    /**
404
     * {@inheritdoc}
405
     */
406 4
    public function takeEnd(int $size): SequenceInterface
407
    {
408 4
        return $this->slice($this->size() - $size, $this->size());
409
    }
410
411
    /**
412
     * {@inheritdoc}
413
     */
414 14
    public function append(SequenceInterface $seq): SequenceInterface
415
    {
416 14
        $self = new self;
417 14
        $self->values = \array_merge($this->values, $seq->toPrimitive());
418
419 14
        return $self;
420
    }
421
422
    /**
423
     * {@inheritdoc}
424
     */
425
    public function intersect(SequenceInterface $seq): SequenceInterface
426
    {
427 26
        return $this->filter(static function($value) use ($seq): bool {
428 26
            return $seq->contains($value);
429 26
        });
430
    }
431
432
    /**
433
     * {@inheritdoc}
434
     */
435 22
    public function join(string $separator): Str
436
    {
437 22
        return new Str(\implode($separator, $this->values));
438
    }
439
440
    /**
441
     * {@inheritdoc}
442
     */
443 242
    public function add($element): SequenceInterface
444
    {
445 242
        $self = clone $this;
446 242
        $self->values[] = $element;
447 242
        $self->size = $this->size() + 1;
448
449 242
        return $self;
450
    }
451
452
    /**
453
     * {@inheritdoc}
454
     */
455 6
    public function sort(callable $function): SequenceInterface
456
    {
457 6
        $self = clone $this;
458 6
        \usort($self->values, $function);
459
460 6
        return $self;
461
    }
462
463
    /**
464
     * {@inheritdoc}
465
     */
466 78
    public function reduce($carry, callable $reducer)
467
    {
468 78
        return \array_reduce($this->values, $reducer, $carry);
469
    }
470
471
    /**
472
     * {@inheritdoc}
473
     */
474 6
    public function reverse(): SequenceInterface
475
    {
476 6
        $self = clone $this;
477 6
        $self->values = \array_reverse($this->values);
478
479 6
        return $self;
480
    }
481
482 8
    public function empty(): bool
483
    {
484 8
        return !$this->has(0);
485
    }
486
}
487