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 — master ( f46a23...825633 )
by Baptiste
04:48
created

ObjectKeys::filter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
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
    MapInterface,
8
    Map,
9
    Type,
10
    Str,
11
    Stream,
12
    StreamInterface,
13
    SetInterface,
14
    Set,
15
    Pair,
16
    Specification\ClassType,
17
    Exception\InvalidArgumentException,
18
    Exception\LogicException,
19
    Exception\ElementNotFoundException,
20
    Exception\GroupEmptyMapException
21
};
22
23
final class ObjectKeys implements MapInterface
24
{
25
    private $keyType;
26
    private $valueType;
27
    private $keySpecification;
28
    private $valueSpecification;
29
    private $values;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 66
    public function __construct(string $keyType, string $valueType)
35
    {
36 66
        $this->keySpecification = Type::of($keyType);
37
38 66
        if (!$this->keySpecification instanceof ClassType) {
39 2
            throw new LogicException;
40
        }
41
42 64
        $this->valueSpecification = Type::of($valueType);
43 64
        $this->keyType = new Str($keyType);
44 64
        $this->valueType = new Str($valueType);
45 64
        $this->values = new \SplObjectStorage;
46 64
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 18
    public function keyType(): Str
52
    {
53 18
        return $this->keyType;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 16
    public function valueType(): Str
60
    {
61 16
        return $this->valueType;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 16
    public function size(): int
68
    {
69 16
        return $this->values->count();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function count(): int
76
    {
77
        return $this->size();
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 2
    public function current()
84
    {
85 2
        return $this->get($this->key());
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 2
    public function key()
92
    {
93 2
        return $this->values->current();
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 2
    public function next()
100
    {
101 2
        $this->values->next();
102 2
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 46
    public function rewind()
108
    {
109 46
        $this->values->rewind();
110 46
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 2
    public function valid()
116
    {
117 2
        return $this->values->valid();
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 28
    public function offsetExists($offset): bool
124
    {
125 28
        if (!is_object($offset)) {
126 2
            return false;
127
        }
128
129 28
        return $this->values->offsetExists($offset);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 6
    public function offsetGet($offset)
136
    {
137 6
        return $this->get($offset);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 2
    public function offsetSet($offset, $value)
144
    {
145 2
        throw new LogicException('You can\'t modify a map');
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 2
    public function offsetUnset($offset)
152
    {
153 2
        throw new LogicException('You can\'t modify a map');
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 52
    public function put($key, $value): MapInterface
160
    {
161 52
        $this->keySpecification->validate($key);
162 52
        $this->valueSpecification->validate($value);
163
164 46
        $map = clone $this;
165 46
        $map->values = clone $this->values;
166 46
        $map->values[$key] = $value;
167 46
        $map->rewind();
168
169 46
        return $map;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 22
    public function get($key)
176
    {
177 22
        if (!$this->offsetExists($key)) {
178 4
            throw new ElementNotFoundException;
179
        }
180
181 18
        return $this->values->offsetGet($key);
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 12
    public function contains($key): bool
188
    {
189 12
        return $this->offsetExists($key);
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195 14
    public function clear(): MapInterface
196
    {
197 14
        $map = clone $this;
198 14
        $map->values = new \SplObjectStorage;
199
200 14
        return $map;
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206 6
    public function equals(MapInterface $map): bool
207
    {
208 6
        if ($map->size() !== $this->size()) {
209 2
            return false;
210
        }
211
212 6
        foreach ($this->values as $k) {
213 6
            $v = $this->values[$k];
214
215 6
            if (!$map->contains($k)) {
216
                return false;
217
            }
218
219 6
            if ($map->get($k) !== $v) {
220 6
                return false;
221
            }
222
        }
223
224 2
        return true;
225
    }
226
227
    /**
228
     * {@inheritdoc}
229
     */
230 2
    public function filter(callable $predicate): MapInterface
231
    {
232 2
        $map = $this->clear();
233
234 2
        foreach ($this->values as $k) {
235 2
            $v = $this->values[$k];
236
237 2
            if ($predicate($k, $v) === true) {
238 2
                $map->values[$k] = $v;
239
            }
240
        }
241
242 2
        $map->rewind();
243
244 2
        return $map;
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250 2
    public function foreach(callable $function): MapInterface
251
    {
252 2
        foreach ($this->values as $k) {
253 2
            $v = $this->values[$k];
254
255 2
            $function($k, $v);
256
        }
257
258 2
        return $this;
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264 4
    public function groupBy(callable $discriminator): MapInterface
265
    {
266 4
        if ($this->size() === 0) {
267 2
            throw new GroupEmptyMapException;
268
        }
269
270 2
        $map = null;
271
272 2
        foreach ($this->values as $k) {
273 2
            $v = $this->values[$k];
274
275 2
            $key = $discriminator($k, $v);
276
277 2
            if ($map === null) {
278 2
                $map = new Map(
279 2
                    Type::determine($key),
280 2
                    MapInterface::class
281
                );
282
            }
283
284 2
            if ($map->contains($key)) {
285 2
                $map = $map->put(
286 2
                    $key,
287 2
                    $map->get($key)->put($k, $v)
288
                );
289
            } else {
290 2
                $map = $map->put(
291 2
                    $key,
292 2
                    $this->clear()->put($k, $v)
293
                );
294
            }
295
        }
296
297 2
        return $map;
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303 14
    public function keys(): SetInterface
304
    {
305 14
        return $this->reduce(
306 14
            Set::of((string) $this->keyType),
307 14
            static function(SetInterface $keys, $key): SetInterface {
308 14
                return $keys->add($key);
309 14
            }
310
        );
311
    }
312
313
    /**
314
     * {@inheritdoc}
315
     */
316 16
    public function values(): StreamInterface
317
    {
318 16
        return $this->reduce(
319 16
            Stream::of((string) $this->valueType),
320 16
            static function(StreamInterface $values, $key, $value): StreamInterface {
321 16
                return $values->add($value);
322 16
            }
323
        );
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329 6
    public function map(callable $function): MapInterface
330
    {
331 6
        $map = $this->clear();
332
333 6
        foreach ($this->values as $k) {
334 6
            $v = $this->values[$k];
335
336 6
            $return = $function($k, $v);
337
338 6
            if ($return instanceof Pair) {
339 4
                $this->keySpecification->validate($return->key());
340
341 2
                $key = $return->key();
342 2
                $value = $return->value();
343
            } else {
344 4
                $key = $k;
345 4
                $value = $return;
346
            }
347
348 4
            $this->valueSpecification->validate($value);
349
350 2
            $map->values[$key] = $value;
351
        }
352
353 2
        $map->rewind();
354
355 2
        return $map;
356
    }
357
358
    /**
359
     * {@inheritdoc}
360
     */
361 2
    public function join(string $separator): Str
362
    {
363 2
        return $this->values()->join($separator);
364
    }
365
366
    /**
367
     * {@inheritdoc}
368
     */
369 2
    public function remove($key): MapInterface
370
    {
371 2
        if (!$this->contains($key)) {
372 2
            return $this;
373
        }
374
375 2
        $map = clone $this;
376 2
        $map->values = clone $this->values;
377 2
        $map->values->detach($key);
378 2
        $map->rewind();
379
380 2
        return $map;
381
    }
382
383
    /**
384
     * {@inheritdoc}
385
     */
386 6
    public function merge(MapInterface $map): MapInterface
387
    {
388
        if (
389 6
            !$this->keyType()->equals($map->keyType()) ||
390 6
            !$this->valueType()->equals($map->valueType())
391
        ) {
392 2
            throw new InvalidArgumentException(
393 2
                'The 2 maps does not reference the same types'
394
            );
395
        }
396
397 4
        return $map->reduce(
398 4
            $this,
399 4
            function(self $carry, $key, $value): self {
400 4
                return $carry->put($key, $value);
401 4
            }
402
        );
403
    }
404
405
    /**
406
     * {@inheritdoc}
407
     */
408 2
    public function partition(callable $predicate): MapInterface
409
    {
410 2
        $truthy = $this->clear();
411 2
        $falsy = $this->clear();
412
413 2
        foreach ($this->values as $k) {
414 2
            $v = $this->values[$k];
415
416 2
            $return = $predicate($k, $v);
417
418 2
            if ($return === true) {
419 2
                $truthy->values[$k] = $v;
420
            } else {
421 2
                $falsy->values[$k] = $v;
422
            }
423
        }
424
425 2
        $truthy->rewind();
426 2
        $falsy->rewind();
427
428 2
        return Map::of('bool', MapInterface::class)
429 2
            (true, $truthy)
430 2
            (false, $falsy);
431
    }
432
433
    /**
434
     * {@inheritdoc}
435
     */
436 18
    public function reduce($carry, callable $reducer)
437
    {
438 18
        foreach ($this->values as $k) {
439 18
            $v = $this->values[$k];
440
441 18
            $carry = $reducer($carry, $k, $v);
442
        }
443
444 18
        return $carry;
445
    }
446
}
447