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.
Passed
Push — master ( 487322...eaa181 )
by Baptiste
02:18 queued 28s
created

Set::strings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\CannotGroupEmptyStructure;
7
8
/**
9
 * @template T
10
 */
11
final class Set implements \Countable
12
{
13
    private string $type;
14
    private ValidateArgument $validate;
15
    private Set\Implementation $implementation;
16
17 108
    /**
18
     * {@inheritdoc}
19 108
     */
20 108
    private function __construct(string $type, Set\Implementation $implementation)
21 108
    {
22 108
        $this->type = $type;
23
        $this->validate = Type::of($type);
24 64
        $this->implementation = $implementation;
25
    }
26 64
27 64
    /**
28
     * @param T $values
29 64
     *
30
     * @return self<T>
31
     */
32
    public static function of(string $type, ...$values): self
33
    {
34
        return new self($type, new Set\Primitive($type, ...$values));
35 44
    }
36
37 44
    /**
38
     * It will load the values inside the generator only upon the first use
39
     * of the set
40
     *
41
     * Use this mode when the amount of data may not fit in memory
42
     *
43 22
     * @param \Generator<T> $generator
44
     *
45 22
     * @return self<T>
46
     */
47
    public static function defer(string $type, \Generator $generator): self
48
    {
49
        return new self($type, new Set\Defer($type, $generator));
50
    }
51 2
52
    /**
53 2
     * It will call the given function every time a new operation is done on the
54
     * set. This means the returned structure may not be truly immutable as
55
     * between the calls the underlying source may change.
56
     *
57
     * Use this mode when calling to an external source (meaning IO bound) such
58
     * as parsing a file or calling an API
59 78
     *
60
     * @param callable(): \Generator<T> $generator
61 78
     *
62
     * @return self<T>
63
     */
64
    public static function lazy(string $type, callable $generator): self
65
    {
66
        return new self($type, new Set\Lazy($type, $generator));
67 24
    }
68
69 24
    /**
70
     * @param mixed $values
71
     *
72
     * @return self<mixed>
73
     */
74
    public static function mixed(...$values): self
75 24
    {
76
        return new self('mixed', new Set\Primitive('mixed', ...$values));
77 24
    }
78
79
    /**
80
     * @return self<int>
81
     */
82
    public static function ints(int ...$values): self
83 24
    {
84
        /** @var self<int> */
85 24
        $self = new self('int', new Set\Primitive('int', ...$values));
86 24
87
        return $self;
88
    }
89
90
    /**
91 24
     * @return self<float>
92
     */
93 24
    public static function floats(float ...$values): self
94 24
    {
95
        /** @var self<float> */
96
        $self = new self('float', new Set\Primitive('float', ...$values));
97
98
        return $self;
99 24
    }
100
101 24
    /**
102
     * @return self<string>
103
     */
104
    public static function strings(string ...$values): self
105
    {
106
        /** @var self<string> */
107 22
        $self = new self('string', new Set\Primitive('string', ...$values));
108
109 22
        return $self;
110
    }
111 20
112 20
    /**
113 20
     * @return self<object>
114
     */
115
    public static function objects(object ...$values): self
116 20
    {
117
        /** @var self<object> */
118
        $self = new self('object', new Set\Primitive('object', ...$values));
119
120
        return $self;
121
    }
122 54
123
    public function isOfType(string $type): bool
124 54
    {
125
        return $this->type === $type;
126 52
    }
127 4
128
    /**
129
     * Return the type of this set
130 52
     */
131 52
    public function type(): string
132
    {
133 52
        return $this->type;
134
    }
135
136
    public function size(): int
137
    {
138
        return $this->implementation->size();
139 52
    }
140
141 52
    /**
142
     * {@inheritdoc}
143
     */
144
    public function count(): int
145
    {
146
        return $this->implementation->size();
147 2
    }
148
149 2
    /**
150
     * Intersect this set with the given one
151
     *
152
     * @param self<T> $set
153 2
     *
154 2
     * @return self<T>
155 2
     */
156 2
    public function intersect(self $set): self
157 2
    {
158 2
        assertSet($this->type, $set, 1);
159 2
160
        $newSet = clone $this;
161 2
        $newSet->implementation = $this->implementation->intersect(
162
            $set->implementation,
163
        );
164
165
        return $newSet;
166
    }
167 4
168
    /**
169 4
     * Add an element to the set
170
     *
171 2
     * @param T $element
172 2
     *
173 2
     * @return self<T>
174
     */
175
    public function add($element): self
176 2
    {
177
        return ($this)($element);
178
    }
179
180
    /**
181
     * Add an element to the set
182 20
     *
183
     * Example:
184 20
     * <code>
185
     * Set::of('int')(1)(3)
186 18
     * </code>
187 4
     *
188
     * @param T $element
189
     *
190 18
     * @return self<T>
191
     */
192
    public function __invoke($element): self
193
    {
194
        ($this->validate)($element, 1);
195
196 2
        $self = clone $this;
197
        $self->implementation = ($this->implementation)($element);
198 2
199 2
        return $self;
200
    }
201 2
202
    /**
203
     * Check if the set contains the given element
204
     *
205
     * @param T $element
206
     */
207 2
    public function contains($element): bool
208
    {
209 2
        ($this->validate)($element, 1);
210
211 2
        return $this->implementation->contains($element);
212
    }
213
214
    /**
215
     * Remove the element from the set
216
     *
217 2
     * @param T $element
218
     *
219 2
     * @return self<T>
220
     */
221 2
    public function remove($element): self
222 2
    {
223
        ($this->validate)($element, 1);
224 2
225 2
        $self = clone $this;
226
        $self->implementation = $this->implementation->remove($element);
227 2
228 2
        return $self;
229
    }
230
231
    /**
232
     * Return the diff between this set and the given one
233
     *
234
     * @param self<T> $set
235 4
     *
236
     * @return self<T>
237 4
     */
238 4
    public function diff(self $set): self
239
    {
240 4
        assertSet($this->type, $set, 1);
241 4
242
        $self = clone $this;
243
        $self->implementation = $this->implementation->diff(
244
            $set->implementation,
245
        );
246
247
        return $self;
248 2
    }
249
250 2
    /**
251 2
     * Check if the given set is identical to this one
252 2
     *
253 2
     * @param self<T> $set
254 2
     */
255
    public function equals(self $set): bool
256 2
    {
257 2
        assertSet($this->type, $set, 1);
258 2
259
        return $this->implementation->equals($set->implementation);
260
    }
261
262
    /**
263
     * Return all elements that satisfy the given predicate
264 2
     *
265
     * @param callable(T): bool $predicate
266 2
     *
267
     * @return self<T>
268
     */
269
    public function filter(callable $predicate): self
270
    {
271
        $set = clone $this;
272 2
        $set->implementation = $this->implementation->filter($predicate);
273
274 2
        return $set;
275
    }
276
277
    /**
278
     * Apply the given function to all elements of the set
279
     *
280 4
     * @param callable(T): void $function
281
     */
282 4
    public function foreach(callable $function): void
283
    {
284 2
        $this->implementation->foreach($function);
285 2
    }
286
287 2
    /**
288 2
     * Return a new map of pairs grouped by keys determined with the given
289
     * discriminator function
290
     *
291
     * @template D
292
     * @param callable(T): D $discriminator
293
     *
294
     * @throws CannotGroupEmptyStructure
295 8
     *
296
     * @return Map<D, self<T>>
297 8
     */
298
    public function groupBy(callable $discriminator): Map
299
    {
300
        return $this->implementation->groupBy($discriminator);
301
    }
302
303 8
    /**
304
     * Return a new set by applying the given function to all elements
305 8
     *
306 8
     * @param callable(T): T $function
307
     *
308 8
     * @return self<T>
309
     */
310
    public function map(callable $function): self
311 2
    {
312
        $self = $this->clear();
313 2
        $self->implementation = $this->implementation->map($function);
314
315
        return $self;
316
    }
317
318
    /**
319
     * Create a new Set with the exact same number of elements but with a
320
     * new type transformed via the given function
321
     *
322
     * @template S
323
     *
324
     * @param callable(T): S $map
325 30
     *
326
     * @return self<S>
327 30
     */
328 8
    public function mapTo(string $type, callable $map): self
329 8
    {
330
        /** @psalm-suppress MixedArgument */
331
        return $this->toSetOf(
332 22
            $type,
333
            static fn($value): \Generator => yield $map($value),
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_FN, expecting T_PAAMAYIM_NEKUDOTAYIM on line 333 at column 19
Loading history...
334
        );
335
    }
336
337
    /**
338
     * Return a sequence of 2 sets partitioned according to the given predicate
339
     *
340
     * @param callable(T): bool $predicate
341
     *
342
     * @return Map<bool, self<T>>
343
     */
344
    public function partition(callable $predicate): Map
345
    {
346
        return $this->implementation->partition($predicate);
347
    }
348
349
    /**
350
     * Return a sequence sorted with the given function
351
     *
352
     * @param callable(T, T): int $function
353
     *
354
     * @return Sequence<T>
355
     */
356
    public function sort(callable $function): Sequence
357
    {
358
        return $this->implementation->sort($function);
359
    }
360
361
    /**
362
     * Create a new set with elements of both sets
363
     *
364
     * @param self<T> $set
365
     *
366
     * @return self<T>
367
     */
368
    public function merge(self $set): self
369
    {
370
        assertSet($this->type, $set, 1);
371
372
        $self = clone $this;
373
        $self->implementation = $this->implementation->merge(
374
            $set->implementation,
375
        );
376
377
        return $self;
378
    }
379
380
    /**
381
     * Reduce the set to a single value
382
     *
383
     * @template R
384
     * @param R $carry
385
     * @param callable(R, T): R $reducer
386
     *
387
     * @return R
388
     */
389
    public function reduce($carry, callable $reducer)
390
    {
391
        return $this->implementation->reduce($carry, $reducer);
392
    }
393
394
    /**
395
     * Return a set of the same type but without any value
396
     *
397
     * @return self<T>
398
     */
399
    public function clear(): self
400
    {
401
        $self = clone $this;
402
        $self->implementation = $this->implementation->clear();
403
404
        return $self;
405
    }
406
407
    public function empty(): bool
408
    {
409
        return $this->implementation->empty();
410
    }
411
412
    /**
413
     * @template ST
414
     *
415
     * @param null|callable(T): \Generator<ST> $mapper
416
     *
417
     * @return Sequence<ST>
418
     */
419
    public function toSequenceOf(string $type, callable $mapper = null): Sequence
420
    {
421
        return $this->implementation->toSequenceOf($type, $mapper);
422
    }
423
424
    /**
425
     * @template ST
426
     *
427
     * @param null|callable(T): \Generator<ST> $mapper
428
     *
429
     * @return self<ST>
430
     */
431
    public function toSetOf(string $type, callable $mapper = null): self
432
    {
433
        return $this->implementation->toSetOf($type, $mapper);
434
    }
435
436
    /**
437
     * @template MT
438
     * @template MS
439
     *
440
     * @param callable(T): \Generator<MT, MS> $mapper
441
     *
442
     * @return Map<MT, MS>
443
     */
444
    public function toMapOf(string $key, string $value, callable $mapper): Map
445
    {
446
        return $this->implementation->toMapOf($key, $value, $mapper);
447
    }
448
}
449