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 ( c3b70c...588978 )
by Baptiste
05:12
created

Map::isOfType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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