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 ( 7ab88f...fff013 )
by Baptiste
07:46
created

Set::defer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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
    /**
18
     * {@inheritdoc}
19
     */
20 78
    private function __construct(string $type, Set\Implementation $implementation)
21
    {
22 78
        $this->type = $type;
23 78
        $this->validate = Type::of($type);
24 78
        $this->implementation = $implementation;
25 78
    }
26
27
    /**
28
     * @param T $values
29
     *
30
     * @return self<T>
31
     */
32 66
    public static function of(string $type, ...$values): self
33
    {
34 66
        $self = new self($type, new Set\Primitive($type, ...$values));
35
36 66
        return $self;
37
    }
38
39
    /**
40
     * @param \Generator<T> $generator
41
     *
42
     * @return self<T>
43
     */
44 3
    public static function defer(string $type, \Generator $generator): self
45
    {
46 3
        $self = new self($type, new Set\Defer($type, $generator));
47
48 3
        return $self;
49
    }
50
51
    /**
52
     * @param mixed $values
53
     *
54
     * @return self<mixed>
55
     */
56 2
    public static function mixed(...$values): self
57
    {
58
        /** @var self<mixed> */
59 2
        $self = new self('mixed', new Set\Primitive('mixed', ...$values));
60
61 2
        return $self;
62
    }
63
64
    /**
65
     * @return self<int>
66
     */
67 6
    public static function ints(int ...$values): self
68
    {
69
        /** @var self<int> */
70 6
        $self = new self('int', new Set\Primitive('int', ...$values));
0 ignored issues
show
Bug introduced by
$values of type integer is incompatible with the type Innmind\Immutable\Set\T expected by parameter $values of Innmind\Immutable\Set\Primitive::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $self = new self('int', new Set\Primitive('int', /** @scrutinizer ignore-type */ ...$values));
Loading history...
71
72 6
        return $self;
73
    }
74
75
    /**
76
     * @return self<float>
77
     */
78 1
    public static function floats(float ...$values): self
79
    {
80
        /** @var self<float> */
81 1
        $self = new self('float', new Set\Primitive('float', ...$values));
0 ignored issues
show
Bug introduced by
$values of type double is incompatible with the type Innmind\Immutable\Set\T expected by parameter $values of Innmind\Immutable\Set\Primitive::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        $self = new self('float', new Set\Primitive('float', /** @scrutinizer ignore-type */ ...$values));
Loading history...
82
83 1
        return $self;
84
    }
85
86
    /**
87
     * @return self<string>
88
     */
89 1
    public static function strings(string ...$values): self
90
    {
91
        /** @var self<string> */
92 1
        $self = new self('string', new Set\Primitive('string', ...$values));
0 ignored issues
show
Bug introduced by
$values of type string is incompatible with the type Innmind\Immutable\Set\T expected by parameter $values of Innmind\Immutable\Set\Primitive::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
        $self = new self('string', new Set\Primitive('string', /** @scrutinizer ignore-type */ ...$values));
Loading history...
93
94 1
        return $self;
95
    }
96
97
    /**
98
     * @return self<object>
99
     */
100 1
    public static function objects(object ...$values): self
101
    {
102
        /** @var self<object> */
103 1
        $self = new self('object', new Set\Primitive('object', ...$values));
104
105 1
        return $self;
106
    }
107
108 22
    public function isOfType(string $type): bool
109
    {
110 22
        return $this->type === $type;
111
    }
112
113
    /**
114
     * Return the type of this set
115
     */
116 24
    public function type(): string
117
    {
118 24
        return $this->type;
119
    }
120
121 1
    public function size(): int
122
    {
123 1
        return $this->implementation->size();
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 1
    public function count(): int
130
    {
131 1
        return $this->implementation->size();
132
    }
133
134
    /**
135
     * Intersect this set with the given one
136
     *
137
     * @param self<T> $set
138
     *
139
     * @return self<T>
140
     */
141 2
    public function intersect(self $set): self
142
    {
143 2
        assertSet($this->type, $set, 1);
144
145 1
        $newSet = clone $this;
146 1
        $newSet->implementation = $this->implementation->intersect(
147 1
            $set->implementation
148
        );
149
150 1
        return $newSet;
151
    }
152
153
    /**
154
     * Add a element to the set
155
     *
156
     * @param T $element
157
     *
158
     * @return self<T>
159
     */
160 46
    public function add($element): self
161
    {
162 46
        ($this->validate)($element, 1);
163
164 45
        $self = clone $this;
165 45
        $self->implementation = $this->implementation->add($element);
166
167 45
        return $self;
168
    }
169
170
    /**
171
     * Alias for add method in order to have a syntax similar to a true tuple
172
     * when constructing the set
173
     *
174
     * Example:
175
     * <code>
176
     * Set::of('int')(1)(3)
177
     * </code>
178
     *
179
     * @param T $element
180
     *
181
     * @return self<T>
182
     */
183 31
    public function __invoke($element): self
184
    {
185 31
        return $this->add($element);
186
    }
187
188
    /**
189
     * Check if the set contains the given element
190
     *
191
     * @param T $element
192
     */
193 1
    public function contains($element): bool
194
    {
195 1
        ($this->validate)($element, 1);
196
197 1
        return $this->implementation->contains($element);
198
    }
199
200
    /**
201
     * Remove the element from the set
202
     *
203
     * @param T $element
204
     *
205
     * @return self<T>
206
     */
207 1
    public function remove($element): self
208
    {
209 1
        ($this->validate)($element, 1);
210
211 1
        $self = clone $this;
212 1
        $self->implementation = $this->implementation->remove($element);
213
214 1
        return $self;
215
    }
216
217
    /**
218
     * Return the diff between this set and the given one
219
     *
220
     * @param self<T> $set
221
     *
222
     * @return self<T>
223
     */
224 2
    public function diff(self $set): self
225
    {
226 2
        assertSet($this->type, $set, 1);
227
228 1
        $self = clone $this;
229 1
        $self->implementation = $this->implementation->diff(
230 1
            $set->implementation
231
        );
232
233 1
        return $self;
234
    }
235
236
    /**
237
     * Check if the given set is identical to this one
238
     *
239
     * @param self<T> $set
240
     */
241 10
    public function equals(self $set): bool
242
    {
243 10
        assertSet($this->type, $set, 1);
244
245 9
        return $this->implementation->equals($set->implementation);
246
    }
247
248
    /**
249
     * Return all elements that satisfy the given predicate
250
     *
251
     * @param callable(T): bool $predicate
252
     *
253
     * @return self<T>
254
     */
255 1
    public function filter(callable $predicate): self
256
    {
257 1
        $set = clone $this;
258 1
        $set->implementation = $this->implementation->filter($predicate);
259
260 1
        return $set;
261
    }
262
263
    /**
264
     * Apply the given function to all elements of the set
265
     *
266
     * @param callable(T): void $function
267
     */
268 1
    public function foreach(callable $function): void
269
    {
270 1
        $this->implementation->foreach($function);
271 1
    }
272
273
    /**
274
     * Return a new map of pairs grouped by keys determined with the given
275
     * discriminator function
276
     *
277
     * @template D
278
     * @param callable(T): D $discriminator
279
     *
280
     * @throws CannotGroupEmptyStructure
281
     *
282
     * @return Map<D, self<T>>
283
     */
284 1
    public function groupBy(callable $discriminator): Map
285
    {
286 1
        return $this->implementation->groupBy($discriminator);
287
    }
288
289
    /**
290
     * Return a new set by applying the given function to all elements
291
     *
292
     * @param callable(T): T $function
293
     *
294
     * @return self<T>
295
     */
296 2
    public function map(callable $function): self
297
    {
298 2
        $self = $this->clear();
299 2
        $self->implementation = $this->implementation->map($function);
300
301 1
        return $self;
302
    }
303
304
    /**
305
     * Return a sequence of 2 sets partitioned according to the given predicate
306
     *
307
     * @param callable(T): bool $predicate
308
     *
309
     * @return Map<bool, self<T>>
310
     */
311 1
    public function partition(callable $predicate): Map
312
    {
313 1
        return $this->implementation->partition($predicate);
314
    }
315
316
    /**
317
     * Return a sequence sorted with the given function
318
     *
319
     * @param callable(T, T): int $function
320
     *
321
     * @return Sequence<T>
322
     */
323 1
    public function sort(callable $function): Sequence
324
    {
325 1
        return $this->implementation->sort($function);
326
    }
327
328
    /**
329
     * Create a new set with elements of both sets
330
     *
331
     * @param self<T> $set
332
     *
333
     * @return self<T>
334
     */
335 2
    public function merge(self $set): self
336
    {
337 2
        assertSet($this->type, $set, 1);
338
339 1
        $self = clone $this;
340 1
        $self->implementation = $this->implementation->merge(
341 1
            $set->implementation
342
        );
343
344 1
        return $self;
345
    }
346
347
    /**
348
     * Reduce the set to a single value
349
     *
350
     * @template R
351
     * @param R $carry
352
     * @param callable(R, T): R $reducer
353
     *
354
     * @return R
355
     */
356 60
    public function reduce($carry, callable $reducer)
357
    {
358 60
        return $this->implementation->reduce($carry, $reducer);
359
    }
360
361
    /**
362
     * Return a set of the same type but without any value
363
     *
364
     * @return self<T>
365
     */
366 2
    public function clear(): self
367
    {
368 2
        $self = clone $this;
369 2
        $self->implementation = $this->implementation->clear();
370
371 2
        return $self;
372
    }
373
374 3
    public function empty(): bool
375
    {
376 3
        return $this->implementation->empty();
377
    }
378
379
    /**
380
     * @template ST
381
     *
382
     * @param null|callable(T): \Generator<ST> $mapper
383
     *
384
     * @return Sequence<ST>
385
     */
386 1
    public function toSequenceOf(string $type, callable $mapper = null): Sequence
387
    {
388 1
        return $this->implementation->toSequenceOf($type, $mapper);
389
    }
390
391
    /**
392
     * @template ST
393
     *
394
     * @param null|callable(T): \Generator<ST> $mapper
395
     *
396
     * @return self<ST>
397
     */
398 1
    public function toSetOf(string $type, callable $mapper = null): self
399
    {
400 1
        return $this->implementation->toSetOf($type, $mapper);
401
    }
402
403
404
405
    /**
406
     * @template MT
407
     * @template MS
408
     *
409
     * @param callable(T): \Generator<MT, MS> $mapper
410
     *
411
     * @return Map<MT, MS>
412
     */
413 1
    public function toMapOf(string $key, string $value, callable $mapper): Map
414
    {
415 1
        return $this->implementation->toMapOf($key, $value, $mapper);
416
    }
417
}
418