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 ( a1e45f...4cf84e )
by Baptiste
05:27
created

Map::toSetOf()   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
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\{
7
    ValidateArgument\ClassType,
8
    Exception\LogicException,
9
    Exception\ElementNotFound,
10
    Exception\CannotGroupEmptyStructure,
11
};
12
13
/**
14
 * @template T
15
 * @template S
16
 */
17
final class Map implements \Countable
18
{
19
    private Map\Implementation $implementation;
20
    private string $keyType;
21
    private string $valueType;
22
    private ValidateArgument $validateKey;
23
    private ValidateArgument $validateValue;
24
25 48
    private function __construct(string $keyType, string $valueType)
26
    {
27 48
        $type = Type::of($keyType);
28 48
        $this->implementation = new Map\DoubleIndex($keyType, $valueType);
29 48
        $this->keyType = $keyType;
30 48
        $this->valueType = $valueType;
31 48
        $this->validateKey = $type;
32 48
        $this->validateValue = Type::of($valueType);
33
34 48
        if ($type instanceof ClassType || $keyType === 'object') {
35 3
            $this->implementation = new Map\ObjectKeys($keyType, $valueType);
36 47
        } else if (\in_array($keyType, ['int', 'integer', 'string'], true)) {
37 39
            $this->implementation = new Map\Primitive($keyType, $valueType);
38
        }
39 48
    }
40
41
    /**
42
     * @param list<T> $keys
43
     * @param list<S> $values
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\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...
44
     *
45
     * @return self<T, S>
46
     */
47 49
    public static function of(
48
        string $key,
49
        string $value,
50
        array $keys = [],
51
        array $values = []
52
    ): self {
53 49
        $keys = \array_values($keys);
54 49
        $values = \array_values($values);
55
56 49
        if (\count($keys) !== \count($values)) {
57 1
            throw new LogicException('Different sizes of keys and values');
58
        }
59
60 48
        $self = new self($key, $value);
61
62 48
        foreach ($keys as $i => $key) {
63 1
            $self = $self->put($key, $values[$i]);
64
        }
65
66 48
        return $self;
67
    }
68
69 13
    public function isOfType(string $key, string $value): bool
70
    {
71 13
        return $this->keyType === $key && $this->valueType === $value;
72
    }
73
74
    /**
75
     * Return the key type for this map
76
     */
77 25
    public function keyType(): string
78
    {
79 25
        return $this->implementation->keyType();
80
    }
81
82
    /**
83
     * Return the value type for this map
84
     */
85 24
    public function valueType(): string
86
    {
87 24
        return $this->implementation->valueType();
88
    }
89
90 8
    public function size(): int
91
    {
92 8
        return $this->implementation->size();
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 7
    public function count(): int
99
    {
100 7
        return $this->implementation->count();
101
    }
102
103
    /**
104
     * Set a new key/value pair
105
     *
106
     * @param T $key
107
     * @param S $value
108
     *
109
     * @return self<T, S>
110
     */
111 41
    public function put($key, $value): self
112
    {
113 41
        ($this->validateKey)($key, 1);
114 40
        ($this->validateValue)($value, 2);
115
116 39
        $map = clone $this;
117 39
        $map->implementation = $this->implementation->put($key, $value);
118
119 39
        return $map;
120
    }
121
122
    /**
123
     * Alias for put method in order to have a syntax similar to a true tuple
124
     * when constructing the map
125
     *
126
     * Example:
127
     * <code>
128
     * Map::of('int', 'int')
129
     *     (1, 2)
130
     *     (3, 4)
131
     * </code>
132
     *
133
     * @param T $key
134
     * @param S $value
135
     *
136
     * @return self<T, S>
137
     */
138 22
    public function __invoke($key, $value): self
139
    {
140 22
        return $this->put($key, $value);
141
    }
142
143
    /**
144
     * Return the element with the given key
145
     *
146
     * @param T $key
147
     *
148
     * @throws ElementNotFound
149
     *
150
     * @return S
151
     */
152 23
    public function get($key)
153
    {
154 23
        ($this->validateKey)($key, 1);
155
156
        /** @var S */
157 23
        return $this->implementation->get($key);
158
    }
159
160
    /**
161
     * Check if there is an element for the given key
162
     *
163
     * @param T $key
164
     */
165 10
    public function contains($key): bool
166
    {
167 10
        ($this->validateKey)($key, 1);
168
169 10
        return $this->implementation->contains($key);
170
    }
171
172
    /**
173
     * Return an empty map given the same given type
174
     *
175
     * @return self<T, S>
176
     */
177 5
    public function clear(): self
178
    {
179 5
        $map = clone $this;
180 5
        $map->implementation = $this->implementation->clear();
181
182 5
        return $map;
183
    }
184
185
    /**
186
     * Check if the two maps are equal
187
     *
188
     * @param self<T, S> $map
189
     */
190 7
    public function equals(self $map): bool
191
    {
192 7
        assertMap($this->keyType, $this->valueType, $map, 1);
193
194 6
        return $this->implementation->equals($map->implementation);
195
    }
196
197
    /**
198
     * Filter the map based on the given predicate
199
     *
200
     * @param callable(T, S): bool $predicate
201
     *
202
     * @return self<T, S>
203
     */
204 1
    public function filter(callable $predicate): self
205
    {
206 1
        $map = $this->clear();
207 1
        $map->implementation = $this->implementation->filter($predicate);
208
209 1
        return $map;
210
    }
211
212
    /**
213
     * Run the given function for each element of the map
214
     *
215
     * @param callable(T, S): void $function
216
     */
217 1
    public function foreach(callable $function): void
218
    {
219 1
        $this->implementation->foreach($function);
220 1
    }
221
222
    /**
223
     * Return a new map of pairs' sequences grouped by keys determined with the given
224
     * discriminator function
225
     *
226
     * @template D
227
     * @param callable(T, S): D $discriminator
228
     *
229
     * @throws CannotGroupEmptyStructure
230
     *
231
     * @return self<D, self<T, S>>
232
     */
233 2
    public function groupBy(callable $discriminator): self
234
    {
235 2
        return $this->implementation->groupBy($discriminator);
236
    }
237
238
    /**
239
     * Return all keys
240
     *
241
     * @return Set<T>
242
     */
243 10
    public function keys(): Set
244
    {
245 10
        return $this->implementation->keys();
246
    }
247
248
    /**
249
     * Return all values
250
     *
251
     * @return Sequence<S>
252
     */
253 9
    public function values(): Sequence
254
    {
255 9
        return $this->implementation->values();
256
    }
257
258
    /**
259
     * Apply the given function on all elements and return a new map
260
     *
261
     * Keys can't be modified
262
     *
263
     * @param callable(T, S): (S|Pair<T, S>) $function
264
     *
265
     * @return self<T, S>
266
     */
267 3
    public function map(callable $function): self
268
    {
269 3
        $map = $this->clear();
270 3
        $map->implementation = $this->implementation->map($function);
271
272 1
        return $map;
273
    }
274
275
    /**
276
     * Concatenate all elements with the given separator
277
     */
278 1
    public function join(string $separator): Str
279
    {
280 1
        return $this->implementation->join($separator);
281
    }
282
283
    /**
284
     * Remove the element with the given key
285
     *
286
     * @param T $key
287
     *
288
     * @return self<T, S>
289
     */
290 1
    public function remove($key): self
291
    {
292 1
        ($this->validateKey)($key, 1);
293
294 1
        $map = clone $this;
295 1
        $map->implementation = $this->implementation->remove($key);
296
297 1
        return $map;
298
    }
299
300
    /**
301
     * Create a new map by combining both maps
302
     *
303
     * @param self<T, S> $map
304
     *
305
     * @return self<T, S>
306
     */
307 2
    public function merge(self $map): self
308
    {
309 2
        assertMap($this->keyType, $this->valueType, $map, 1);
310
311 1
        $self = clone $this;
312 1
        $self->implementation = $this->implementation->merge($map->implementation);
313
314 1
        return $self;
315
    }
316
317
    /**
318
     * Return a map of 2 maps partitioned according to the given predicate
319
     *
320
     * @param callable(T, S): bool $predicate
321
     *
322
     * @return self<bool, self<T, S>>
323
     */
324 1
    public function partition(callable $predicate): self
325
    {
326 1
        return $this->implementation->partition($predicate);
327
    }
328
329
    /**
330
     * Reduce the map to a single value
331
     *
332
     * @template R
333
     * @param R $carry
334
     * @param callable(R, T, S): R $reducer
335
     *
336
     * @return R
337
     */
338 3
    public function reduce($carry, callable $reducer)
339
    {
340 3
        return $this->implementation->reduce($carry, $reducer);
341
    }
342
343
    public function empty(): bool
344
    {
345
        return $this->implementation->empty();
346
    }
347
348
    /**
349
     * @template ST
350
     *
351
     * @param callable(T, S): \Generator<ST> $mapper
352
     *
353
     * @return Set<ST>
354
     */
355 1
    public function toSetOf(string $type, callable $mapper): Set
356
    {
357 1
        return $this->implementation->toSetOf($type, $mapper);
358
    }
359
}
360