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 — develop ( 7af216...ebf010 )
by Baptiste
13:17
created

Primitive::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 2
rs 9.9666
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable\Set;
5
6
use Innmind\Immutable\{
7
    Map,
8
    Sequence,
9
    Set,
10
    Type,
11
    ValidateArgument,
12
    Str,
13
    Exception\CannotGroupEmptyStructure,
14
};
15
16
/**
17
 * @template T
18
 */
19
final class Primitive implements Implementation
20
{
21
    private string $type;
22
    private ValidateArgument $validate;
23
    private Sequence\Implementation $values;
24
25
    /**
26
     * @param T $values
27
     */
28 97
    public function __construct(string $type, ...$values)
29
    {
30 97
        $this->type = $type;
31 97
        $this->validate = Type::of($type);
32 97
        $this->values = (new Sequence\Primitive($type, ...$values))->distinct();
33 97
    }
34
35 1
    public function type(): string
36
    {
37 1
        return $this->type;
38
    }
39
40 14
    public function size(): int
41
    {
42 14
        return $this->values->size();
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function count(): int
49
    {
50 1
        return $this->values->size();
51
    }
52
53
    /**
54
     * @return \Iterator<T>
55
     */
56 25
    public function iterator(): \Iterator
57
    {
58 25
        return $this->values->iterator();
59
    }
60
61
    /**
62
     * @param Implementation<T> $set
63
     *
64
     * @return self<T>
65
     */
66 12
    public function intersect(Implementation $set): self
67
    {
68 12
        $self = $this->clear();
69 12
        $self->values = $this->values->intersect(
70 12
            new Sequence\Primitive($this->type, ...$set->iterator()),
71
        );
72
73 12
        return $self;
74
    }
75
76
    /**
77
     * @param T $element
78
     *
79
     * @return self<T>
80
     */
81 48
    public function __invoke($element): self
82
    {
83 48
        if ($this->contains($element)) {
84 5
            return $this;
85
        }
86
87 48
        $set = clone $this;
88 48
        $set->values = ($this->values)($element);
89
90 48
        return $set;
91
    }
92
93
    /**
94
     * @param T $element
95
     */
96 50
    public function contains($element): bool
97
    {
98 50
        return $this->values->contains($element);
99
    }
100
101
    /**
102
     * @param T $element
103
     *
104
     * @return self<T>
105
     */
106 2
    public function remove($element): self
107
    {
108 2
        if (!$this->contains($element)) {
109 1
            return $this;
110
        }
111
112 2
        $index = $this->values->indexOf($element);
113 2
        $set = clone $this;
114 2
        $set->values = $this
115 2
            ->values
116 2
            ->slice(0, $index)
117 2
            ->append($this->values->slice($index + 1, $this->size()));
118
119 2
        return $set;
120
    }
121
122
    /**
123
     * @param Implementation<T> $set
124
     *
125
     * @return self<T>
126
     */
127 2
    public function diff(Implementation $set): self
128
    {
129 2
        $self = clone $this;
130 2
        $self->values = $this->values->diff(
131 2
            new Sequence\Primitive($this->type, ...$set->iterator()),
132
        );
133
134 2
        return $self;
135
    }
136
137
    /**
138
     * @param Implementation<T> $set
139
     */
140 10
    public function equals(Implementation $set): bool
141
    {
142 10
        if ($this->size() !== $set->size()) {
143 3
            return false;
144
        }
145
146 10
        return $this->intersect($set)->size() === $this->size();
147
    }
148
149
    /**
150
     * @param callable(T): bool $predicate
151
     *
152
     * @return self<T>
153
     */
154 2
    public function filter(callable $predicate): self
155
    {
156 2
        $set = clone $this;
157 2
        $set->values = $this->values->filter($predicate);
158
159 2
        return $set;
160
    }
161
162
    /**
163
     * @param callable(T): void $function
164
     */
165 2
    public function foreach(callable $function): void
166
    {
167 2
        $this->values->foreach($function);
168 2
    }
169
170
    /**
171
     * @template D
172
     * @param callable(T): D $discriminator
173
     *
174
     * @throws CannotGroupEmptyStructure
175
     *
176
     * @return Map<D, Set<T>>
177
     */
178 2
    public function groupBy(callable $discriminator): Map
179
    {
180
        /** @var Map<D, Sequence<T>> */
181 2
        $map = $this->values->groupBy($discriminator);
182
183
        /**
184
         * @psalm-suppress MixedReturnTypeCoercion
185
         * @var Map<D, Set<T>>
186
         */
187 2
        return $map->reduce(
188 2
            Map::of($map->keyType(), Set::class),
189
            fn(Map $carry, $key, Sequence $values): Map => ($carry)(
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ':', expecting T_DOUBLE_ARROW on line 189 at column 50
Loading history...
190 2
                $key,
191 2
                $values->toSetOf($this->type),
192 2
            ),
193
        );
194 2
    }
195
196
    /**
197
     * @param callable(T): T $function
198
     *
199
     * @return self<T>
200
     */
201
    public function map(callable $function): self
202
    {
203 4
        $validate = $this->validate;
204
205
        /**
206
         * @psalm-suppress MissingClosureParamType
207
         * @psalm-suppress MissingClosureReturnType
208
         */
209
        $function = static function($value) use ($validate, $function) {
210
            /** @var T $value */
211 4
            $returned = $function($value);
212 4
            ($validate)($returned, 1);
213
214 2
            return $returned;
215 4
        };
216
217 4
        return $this->reduce(
218 4
            $this->clear(),
219
            static fn(self $carry, $value): self => ($carry)($function($value)),
220 4
        );
221 4
    }
222
223
    /**
224
     * @param callable(T): bool $predicate
225
     *
226
     * @return Map<bool, Set<T>>
227
     */
228
    public function partition(callable $predicate): Map
229
    {
230 2
        $partitions = $this->values->partition($predicate);
231
        /** @var Set<T> */
232 2
        $truthy = $partitions
233
            ->get(true)
234
            ->toSetOf($this->type);
235 2
        /** @var Set<T> */
236 2
        $falsy = $partitions
237
            ->get(false)
238
            ->toSetOf($this->type);
239 2
240 2
        /**
241
         * @psalm-suppress InvalidScalarArgument
242
         * @psalm-suppress InvalidArgument
243
         * @var Map<bool, Set<T>>
244
         */
245
        return Map::of('bool', Set::class)
246 2
            (true, $truthy)
247 2
            (false, $falsy);
248 2
    }
249
250
    /**
251
     * @param callable(T, T): int $function
252
     *
253
     * @return Sequence<T>
254
     */
255
    public function sort(callable $function): Sequence
256 2
    {
257
        return $this
258
            ->values
259 2
            ->sort($function)
260 2
            ->toSequenceOf($this->type);
261 2
    }
262
263
    /**
264
     * @param Implementation<T> $set
265
     *
266
     * @return self<T>
267
     */
268
    public function merge(Implementation $set): self
269 2
    {
270
        return $set->reduce(
271 2
            $this,
272 2
            static fn(self $carry, $value): self => ($carry)($value),
273
        );
274 2
    }
275 2
276
    /**
277
     * @template R
278
     * @param R $carry
279
     * @param callable(R, T): R $reducer
280
     *
281
     * @return R
282
     */
283
    public function reduce($carry, callable $reducer)
284
    {
285
        return $this->values->reduce($carry, $reducer);
286 62
    }
287
288 62
    /**
289
     * @return self<T>
290
     */
291
    public function clear(): self
292
    {
293
        return new self($this->type);
294 17
    }
295
296 17
    public function empty(): bool
297
    {
298
        return $this->values->empty();
299 4
    }
300
301 4
    /**
302
     * @template ST
303
     *
304
     * @param null|callable(T): \Generator<ST> $mapper
305
     *
306
     * @return Sequence<ST>
307
     */
308
    public function toSequenceOf(string $type, callable $mapper = null): Sequence
309
    {
310
        return $this->values->toSequenceOf($type, $mapper);
311 2
    }
312
313 2
    /**
314
     * @template ST
315
     *
316
     * @param null|callable(T): \Generator<ST> $mapper
317
     *
318
     * @return Set<ST>
319
     */
320
    public function toSetOf(string $type, callable $mapper = null): Set
321
    {
322
        return $this->values->toSetOf($type, $mapper);
323 2
    }
324
325 2
    /**
326
     * @template MT
327
     * @template MS
328
     *
329
     * @param callable(T): \Generator<MT, MS> $mapper
330
     *
331
     * @return Map<MT, MS>
332
     */
333
    public function toMapOf(string $key, string $value, callable $mapper): Map
334
    {
335
        return $this->values->toMapOf($key, $value, $mapper);
336 2
    }
337
}
338