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 ( 3d4c79...7ab88f )
by Baptiste
05:30
created

Primitive::toMapOf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
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\Set;
5
6
use Innmind\Immutable\{
7
    Map,
8
    Sequence,
9
    Set,
10
    Type,
11
    ValidateArgument,
12
    Str,
13
    Exception\CannotGroupEmptyStructure,
14
};
15
use function Innmind\Immutable\unwrap;
16
17
/**
18
 * @template T
19
 */
20
final class Primitive implements Implementation
21
{
22
    private string $type;
23
    private ValidateArgument $validate;
24
    private Sequence $values;
25
26
    /**
27
     * @param T $values
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\Set\T 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...
28
     */
29 95
    public function __construct(string $type, ...$values)
30
    {
31 95
        $this->type = $type;
32 95
        $this->validate = Type::of($type);
33 95
        $this->values = Sequence::of($type, ...$values)->distinct();
34 95
    }
35
36 1
    public function isOfType(string $type): bool
37
    {
38 1
        return $this->type === $type;
39
    }
40
41 1
    public function type(): string
42
    {
43 1
        return $this->type;
44
    }
45
46 14
    public function size(): int
47
    {
48 14
        return $this->values->size();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function count(): int
55
    {
56 1
        return $this->values->size();
57
    }
58
59
    /**
60
     * @return list<T>
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\Set\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...
61
     */
62 24
    public function toArray(): array
63
    {
64 24
        return unwrap($this->values);
0 ignored issues
show
Bug introduced by
$this->values of type Innmind\Immutable\Sequence is incompatible with the type Innmind\Immutable\Set expected by parameter $structure of Innmind\Immutable\unwrap(). ( Ignorable by Annotation )

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

64
        return unwrap(/** @scrutinizer ignore-type */ $this->values);
Loading history...
Bug Best Practice introduced by
The expression return unwrap($this->values) returns the type array which is incompatible with the documented return type Innmind\Immutable\Set\list.
Loading history...
65
    }
66
67
    /**
68
     * @param Implementation<T> $set
69
     *
70
     * @return self<T>
71
     */
72 12
    public function intersect(Implementation $set): self
73
    {
74 12
        $self = $this->clear();
75 12
        $self->values = $this->values->intersect(
76 12
            Sequence::of($this->type, ...$set->toArray())
77
        );
78
79 12
        return $self;
80
    }
81
82
    /**
83
     * @param T $element
84
     *
85
     * @return self<T>
86
     */
87 35
    public function add($element): self
88
    {
89 35
        if ($this->contains($element)) {
90 4
            return $this;
91
        }
92
93 35
        $set = clone $this;
94 35
        $set->values = ($this->values)($element);
95
96 35
        return $set;
97
    }
98
99
    /**
100
     * @param T $element
101
     */
102 37
    public function contains($element): bool
103
    {
104 37
        return $this->values->contains($element);
105
    }
106
107
    /**
108
     * @param T $element
109
     *
110
     * @return self<T>
111
     */
112 2
    public function remove($element): self
113
    {
114 2
        if (!$this->contains($element)) {
115 1
            return $this;
116
        }
117
118 2
        $index = $this->values->indexOf($element);
119 2
        $set = clone $this;
120 2
        $set->values = $this
121 2
            ->values
122 2
            ->clear()
123 2
            ->append($this->values->slice(0, $index))
124 2
            ->append($this->values->slice($index + 1, $this->size()));
125
126 2
        return $set;
127
    }
128
129
    /**
130
     * @param Implementation<T> $set
131
     *
132
     * @return self<T>
133
     */
134 2
    public function diff(Implementation $set): self
135
    {
136 2
        $self = clone $this;
137 2
        $self->values = $this->values->diff(
138 2
            Sequence::of($this->type, ...$set->toArray())
139
        );
140
141 2
        return $self;
142
    }
143
144
    /**
145
     * @param Implementation<T> $set
146
     */
147 10
    public function equals(Implementation $set): bool
148
    {
149 10
        if ($this->size() !== $set->size()) {
150 3
            return false;
151
        }
152
153 10
        return $this->intersect($set)->size() === $this->size();
154
    }
155
156
    /**
157
     * @param callable(T): bool $predicate
158
     *
159
     * @return self<T>
160
     */
161 2
    public function filter(callable $predicate): self
162
    {
163 2
        $set = clone $this;
164 2
        $set->values = $this->values->filter($predicate);
165
166 2
        return $set;
167
    }
168
169
    /**
170
     * @param callable(T): void $function
171
     */
172 2
    public function foreach(callable $function): void
173
    {
174 2
        $this->values->foreach($function);
175 2
    }
176
177
    /**
178
     * @template D
179
     * @param callable(T): D $discriminator
180
     *
181
     * @throws CannotGroupEmptyStructure
182
     *
183
     * @return Map<D, Set<T>>
184
     */
185 2
    public function groupBy(callable $discriminator): Map
186
    {
187
        /** @var Map<D, Sequence<T>> */
188 2
        $map = $this->values->groupBy($discriminator);
189
190
        /**
191
         * @psalm-suppress MixedReturnTypeCoercion
192
         * @var Map<D, Set<T>>
193
         */
194 2
        return $map->reduce(
195 2
            Map::of($map->keyType(), Set::class),
196
            function(Map $carry, $key, Sequence $values): Map {
197 2
                return ($carry)(
198 2
                    $key,
199 2
                    Set::of($this->type, ...unwrap($values)),
0 ignored issues
show
Bug introduced by
$values of type Innmind\Immutable\Sequence is incompatible with the type Innmind\Immutable\Set expected by parameter $structure of Innmind\Immutable\unwrap(). ( Ignorable by Annotation )

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

199
                    Set::of($this->type, ...unwrap(/** @scrutinizer ignore-type */ $values)),
Loading history...
200
                );
201 2
            }
202
        );
203
    }
204
205
    /**
206
     * @param callable(T): T $function
207
     *
208
     * @return self<T>
209
     */
210 4
    public function map(callable $function): self
211
    {
212
        /**
213
         * @psalm-suppress MissingClosureParamType
214
         * @psalm-suppress MissingClosureReturnType
215
         */
216
        $function = function($value) use ($function) {
217
            /** @var T $value */
218 4
            $returned = $function($value);
219 4
            ($this->validate)($returned, 1);
220
221 2
            return $returned;
222 4
        };
223
224 4
        return $this->reduce(
225 4
            $this->clear(),
226
            function(self $carry, $value) use ($function): self {
227 4
                return $carry->add($function($value));
228 4
            }
229
        );
230
    }
231
232
    /**
233
     * @param callable(T): bool $predicate
234
     *
235
     * @return Map<bool, Set<T>>
236
     */
237 2
    public function partition(callable $predicate): Map
238
    {
239 2
        $partitions = $this->values->partition($predicate);
240
        /** @var Set<T> */
241 2
        $truthy = Set::of($this->type, ...unwrap($partitions->get(true)));
242
        /** @var Set<T> */
243 2
        $falsy = Set::of($this->type, ...unwrap($partitions->get(false)));
244
245
        /**
246
         * @psalm-suppress InvalidScalarArgument
247
         * @psalm-suppress InvalidArgument
248
         */
249 2
        return Map::of('bool', Set::class)
250 2
            (true, $truthy)
251 2
            (false, $falsy);
252
    }
253
254
    /**
255
     * @param callable(T, T): int $function
256
     *
257
     * @return Sequence<T>
258
     */
259 2
    public function sort(callable $function): Sequence
260
    {
261 2
        return $this->values->sort($function);
262
    }
263
264
    /**
265
     * @param Implementation<T> $set
266
     *
267
     * @return self<T>
268
     */
269 2
    public function merge(Implementation $set): self
270
    {
271 2
        return $set->reduce(
272 2
            $this,
273
            function(self $carry, $value): self {
274 2
                return $carry->add($value);
275 2
            }
276
        );
277
    }
278
279
    /**
280
     * @template R
281
     * @param R $carry
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\Set\R 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...
282
     * @param callable(R, T): R $reducer
283
     *
284
     * @return R
285
     */
286 60
    public function reduce($carry, callable $reducer)
287
    {
288 60
        return $this->values->reduce($carry, $reducer);
289
    }
290
291
    /**
292
     * @return self<T>
293
     */
294 17
    public function clear(): self
295
    {
296 17
        return new self($this->type);
297
    }
298
299 4
    public function empty(): bool
300
    {
301 4
        return $this->values->empty();
302
    }
303
304
    /**
305
     * @template ST
306
     *
307
     * @param callable(T): \Generator<ST> $mapper
308
     *
309
     * @return Sequence<ST>
310
     */
311 2
    public function toSequenceOf(string $type, callable $mapper): Sequence
312
    {
313 2
        return $this->values->toSequenceOf($type, $mapper);
314
    }
315
316
    /**
317
     * @template ST
318
     *
319
     * @param callable(T): \Generator<ST> $mapper
320
     *
321
     * @return Set<ST>
322
     */
323 2
    public function toSetOf(string $type, callable $mapper): Set
324
    {
325 2
        return $this->values->toSetOf($type, $mapper);
326
    }
327
328
    /**
329
     * @template MT
330
     * @template MS
331
     *
332
     * @param callable(T): \Generator<MT, MS> $mapper
333
     *
334
     * @return Map<MT, MS>
335
     */
336 2
    public function toMapOf(string $key, string $value, callable $mapper): Map
337
    {
338 2
        return $this->values->toMapOf($key, $value, $mapper);
339
    }
340
}
341