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

DoubleIndex::clearMap()   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 0
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\Map;
5
6
use Innmind\Immutable\{
7
    Map,
8
    Type,
9
    Str,
10
    Stream,
11
    Set,
12
    Pair,
13
    SpecificationInterface,
14
    Exception\InvalidArgumentException,
15
    Exception\LogicException,
16
    Exception\ElementNotFoundException,
17
    Exception\GroupEmptyMapException
18
};
19
20
/**
21
 * {@inheritdoc}
22
 */
23
final class DoubleIndex implements Implementation
24
{
25
    private string $keyType;
26
    private string $valueType;
27
    private SpecificationInterface $keySpecification;
28
    private SpecificationInterface $valueSpecification;
29
    private Stream $keys;
30
    private Stream $values;
31
    private Stream $pairs;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 36
    public function __construct(string $keyType, string $valueType)
37
    {
38 36
        $this->keySpecification = Type::of($keyType);
39 36
        $this->valueSpecification = Type::of($valueType);
40 36
        $this->keyType = $keyType;
41 36
        $this->valueType = $valueType;
42 36
        $this->keys = new Stream($keyType);
43 36
        $this->values = new Stream($valueType);
44 36
        $this->pairs = new Stream(Pair::class);
45 36
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 17
    public function keyType(): string
51
    {
52 17
        return $this->keyType;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 15
    public function valueType(): string
59
    {
60 15
        return $this->valueType;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 7
    public function size(): int
67
    {
68 7
        return $this->keys->size();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 2
    public function count(): int
75
    {
76 2
        return $this->keys->count();
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 31
    public function put($key, $value): Implementation
83
    {
84 31
        $this->keySpecification->validate($key);
85 30
        $this->valueSpecification->validate($value);
86
87 28
        $map = clone $this;
88
89 28
        if ($this->keys->contains($key)) {
90 3
            $index = $this->keys->indexOf($key);
91 3
            $map->values = $this->values->take($index)
92 3
                ->add($value)
93 3
                ->append($this->values->drop($index + 1));
94 3
            $map->pairs = $this->pairs->take($index)
95 3
                ->add(new Pair($key, $value))
96 3
                ->append($this->pairs->drop($index + 1));
97
        } else {
98 28
            $map->keys = $this->keys->add($key);
99 28
            $map->values = $this->values->add($value);
100 28
            $map->pairs = $this->pairs->add(new Pair($key, $value));
101
        }
102
103 28
        return $map;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 15
    public function get($key)
110
    {
111 15
        if (!$this->keys->contains($key)) {
112 1
            throw new ElementNotFoundException;
113
        }
114
115 14
        return $this->values->get(
116 14
            $this->keys->indexOf($key)
117
        );
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 3
    public function contains($key): bool
124
    {
125 3
        return $this->keys->contains($key);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 5
    public function clear(): Implementation
132
    {
133 5
        $map = clone $this;
134 5
        $map->keys = $this->keys->clear();
135 5
        $map->values = $this->values->clear();
136 5
        $map->pairs = $this->pairs->clear();
137
138 5
        return $map;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 2
    public function equals(Implementation $map): bool
145
    {
146 2
        if (!$map->keys()->equals($this->keys())) {
147 1
            return false;
148
        }
149
150 2
        foreach ($this->pairs->toArray() as $pair) {
151 2
            if ($map->get($pair->key()) !== $pair->value()) {
152 2
                return false;
153
            }
154
        }
155
156 1
        return true;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 1
    public function filter(callable $predicate): Implementation
163
    {
164 1
        $map = $this->clear();
165
166 1
        foreach ($this->pairs->toArray() as $pair) {
167 1
            if ($predicate($pair->key(), $pair->value()) === true) {
168 1
                $map->keys = $map->keys->add($pair->key());
169 1
                $map->values = $map->values->add($pair->value());
170 1
                $map->pairs = $map->pairs->add($pair);
171
            }
172
        }
173
174 1
        return $map;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 1
    public function foreach(callable $function): void
181
    {
182 1
        foreach ($this->pairs->toArray() as $pair) {
183 1
            $function($pair->key(), $pair->value());
184
        }
185 1
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190 2
    public function groupBy(callable $discriminator): Map
191
    {
192 2
        if ($this->size() === 0) {
193 1
            throw new GroupEmptyMapException;
194
        }
195
196 1
        $map = null;
197
198 1
        foreach ($this->pairs->toArray() as $pair) {
199 1
            $key = $discriminator($pair->key(), $pair->value());
200
201 1
            if ($map === null) {
202 1
                $map = new Map(
203 1
                    Type::determine($key),
204 1
                    Map::class
205
                );
206
            }
207
208 1
            if ($map->contains($key)) {
209 1
                $map = $map->put(
210 1
                    $key,
211 1
                    $map->get($key)->put(
212 1
                        $pair->key(),
213 1
                        $pair->value()
214
                    )
215
                );
216
            } else {
217 1
                $map = $map->put(
218 1
                    $key,
219 1
                    $this->clearMap()->put(
220 1
                        $pair->key(),
221 1
                        $pair->value()
222
                    )
223
                );
224
            }
225
        }
226
227 1
        return $map;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $map could return the type null which is incompatible with the type-hinted return Innmind\Immutable\Map. Consider adding an additional type-check to rule them out.
Loading history...
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233 10
    public function keys(): Set
234
    {
235 10
        return Set::of($this->keyType, ...$this->keys->toArray());
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241 5
    public function values(): Stream
242
    {
243 5
        return $this->values;
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249 3
    public function map(callable $function): Implementation
250
    {
251 3
        $map = $this->clear();
252
253 3
        foreach ($this->pairs->toArray() as $pair) {
254 3
            $return = $function(
255 3
                $pair->key(),
256 3
                $pair->value()
257
            );
258
259 3
            if ($return instanceof Pair) {
260 2
                $key = $return->key();
261 2
                $value = $return->value();
262
            } else {
263 2
                $key = $pair->key();
264 2
                $value = $return;
265
            }
266
267 3
            $map = $map->put($key, $value);
268
        }
269
270 1
        return $map;
271
    }
272
273
    /**
274
     * {@inheritdoc}
275
     */
276 1
    public function join(string $separator): Str
277
    {
278 1
        return $this->values->join($separator);
279
    }
280
281
    /**
282
     * {@inheritdoc}
283
     */
284 1
    public function remove($key): Implementation
285
    {
286 1
        if (!$this->contains($key)) {
287 1
            return $this;
288
        }
289
290 1
        $index = $this->keys->indexOf($key);
291 1
        $map = clone $this;
292 1
        $map->keys = $this
293 1
            ->keys
294 1
            ->slice(0, $index)
295 1
            ->append($this->keys->slice($index + 1, $this->keys->size()));
296 1
        $map->values = $this
297 1
            ->values
298 1
            ->slice(0, $index)
299 1
            ->append($this->values->slice($index + 1, $this->values->size()));
300 1
        $map->pairs = $this
301 1
            ->pairs
302 1
            ->slice(0, $index)
303 1
            ->append($this->pairs->slice($index + 1, $this->pairs->size()));
304
305 1
        return $map;
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311 2
    public function merge(Implementation $map): Implementation
312
    {
313
        if (
314 2
            $this->keyType !== $map->keyType() ||
315 2
            $this->valueType !== $map->valueType()
316
        ) {
317 1
            throw new InvalidArgumentException(
318 1
                'The 2 maps does not reference the same types'
319
            );
320
        }
321
322 1
        return $map->reduce(
323 1
            $this,
324
            function(self $carry, $key, $value): self {
325 1
                return $carry->put($key, $value);
326 1
            }
327
        );
328
    }
329
330
    /**
331
     * {@inheritdoc}
332
     */
333 1
    public function partition(callable $predicate): Map
334
    {
335 1
        $truthy = $this->clearMap();
336 1
        $falsy = $this->clearMap();
337
338 1
        foreach ($this->pairs->toArray() as $pair) {
339 1
            $return = $predicate(
340 1
                $pair->key(),
341 1
                $pair->value()
342
            );
343
344 1
            if ($return === true) {
345 1
                $truthy = $truthy->put($pair->key(), $pair->value());
346
            } else {
347 1
                $falsy = $falsy->put($pair->key(), $pair->value());
348
            }
349
        }
350
351 1
        return Map::of('bool', Map::class)
352 1
            (true, $truthy)
353 1
            (false, $falsy);
354
    }
355
356
    /**
357
     * {@inheritdoc}
358
     */
359 2
    public function reduce($carry, callable $reducer)
360
    {
361 2
        foreach ($this->pairs->toArray() as $pair) {
362 2
            $carry = $reducer($carry, $pair->key(), $pair->value());
363
        }
364
365 2
        return $carry;
366
    }
367
368 1
    public function empty(): bool
369
    {
370 1
        return $this->pairs->empty();
371
    }
372
373
    /**
374
     * @return Map<T, S>
375
     */
376 2
    private function clearMap(): Map
377
    {
378 2
        return Map::of($this->keyType, $this->valueType);
379
    }
380
}
381