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 ( 03a9d8...d2240e )
by Baptiste
04:51
created

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