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

Set   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 322
Duplicated Lines 0 %

Test Coverage

Coverage 99.15%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 91
c 3
b 1
f 0
dl 0
loc 322
ccs 116
cts 117
cp 0.9915
rs 9.76
wmc 33

29 Methods

Rating   Name   Duplication   Size   Complexity  
A foreach() 0 5 1
A reduce() 0 3 1
A contains() 0 3 1
A valid() 0 3 1
A current() 0 3 1
A type() 0 3 1
A intersect() 0 10 1
A count() 0 3 1
A key() 0 3 1
A filter() 0 6 1
A clear() 0 6 1
A toPrimitive() 0 3 1
A add() 0 12 2
A remove() 0 15 2
A sort() 0 3 1
A __construct() 0 5 1
A partition() 0 11 1
A join() 0 3 1
A rewind() 0 3 1
A next() 0 3 1
A size() 0 3 1
A empty() 0 3 1
A validate() 0 5 2
A of() 0 6 1
A diff() 0 10 1
A equals() 0 9 2
A groupBy() 0 11 1
A merge() 0 8 1
A map() 0 6 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\InvalidArgumentException;
7
8
class Set implements SetInterface
9
{
10
    private $type;
11
    private $spec;
12
    private $values;
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 108
    public function __construct(string $type)
18
    {
19 108
        $this->type = new Str($type);
20 108
        $this->spec = Type::of($type);
21 108
        $this->values = new Stream($type);
22 108
    }
23
24 64
    public static function of(string $type, ...$values): self
25
    {
26 64
        $self = new self($type);
27 64
        $self->values = Stream::of($type, ...$values)->distinct();
28
29 64
        return $self;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 44
    public function type(): Str
36
    {
37 44
        return $this->type;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 22
    public function size(): int
44
    {
45 22
        return $this->values->size();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function count(): int
52
    {
53 2
        return $this->values->size();
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 78
    public function toPrimitive(): array
60
    {
61 78
        return $this->values->toPrimitive();
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 24
    public function current()
68
    {
69 24
        return $this->values->current();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 24
    public function key(): int
76
    {
77 24
        return $this->values->key();
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 24
    public function next(): void
84
    {
85 24
        $this->values->next();
86 24
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 24
    public function rewind(): void
92
    {
93 24
        $this->values->rewind();
94 24
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 24
    public function valid(): bool
100
    {
101 24
        return $this->values->valid();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 22
    public function intersect(SetInterface $set): SetInterface
108
    {
109 22
        $this->validate($set);
110
111 20
        $newSet = clone $this;
112 20
        $newSet->values = $this->values->intersect(
113 20
            Stream::of((string) $this->type, ...$set)
114
        );
115
116 20
        return $newSet;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 54
    public function add($element): SetInterface
123
    {
124 54
        $this->spec->validate($element);
125
126 52
        if ($this->contains($element)) {
127 4
            return $this;
128
        }
129
130 52
        $set = clone $this;
131 52
        $set->values = $this->values->add($element);
132
133 52
        return $set;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 52
    public function contains($element): bool
140
    {
141 52
        return $this->values->contains($element);
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 2
    public function remove($element): SetInterface
148
    {
149 2
        if (!$this->contains($element)) {
150
            return $this;
151
        }
152
153 2
        $index = $this->values->indexOf($element);
154 2
        $set = clone $this;
155 2
        $set->values = $this
156 2
            ->values
157 2
            ->clear()
158 2
            ->append($this->values->slice(0, $index))
159 2
            ->append($this->values->slice($index + 1, $this->size()));
160
161 2
        return $set;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 4
    public function diff(SetInterface $set): SetInterface
168
    {
169 4
        $this->validate($set);
170
171 2
        $newSet = clone $this;
172 2
        $newSet->values = $this->values->diff(
173 2
            Stream::of((string) $this->type, ...$set)
174
        );
175
176 2
        return $newSet;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 20
    public function equals(SetInterface $set): bool
183
    {
184 20
        $this->validate($set);
185
186 18
        if ($this->size() !== $set->size()) {
187 4
            return false;
188
        }
189
190 18
        return $this->intersect($set)->size() === $this->size();
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 2
    public function filter(callable $predicate): SetInterface
197
    {
198 2
        $set = clone $this;
199 2
        $set->values = $this->values->filter($predicate);
200
201 2
        return $set;
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207 2
    public function foreach(callable $function): SetInterface
208
    {
209 2
        $this->values->foreach($function);
210
211 2
        return $this;
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217 2
    public function groupBy(callable $discriminator): MapInterface
218
    {
219 2
        $map = $this->values->groupBy($discriminator);
220
221 2
        return $map->reduce(
222 2
            new Map((string) $map->keyType(), SetInterface::class),
223
            function(MapInterface $carry, $key, StreamInterface $values): MapInterface {
224 2
                $set = $this->clear();
225 2
                $set->values = $values;
226
227 2
                return $carry->put($key, $set);
228 2
            }
229
        );
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 4
    public function map(callable $function): SetInterface
236
    {
237 4
        return $this->reduce(
238 4
            $this->clear(),
239
            function(SetInterface $carry, $value) use ($function): SetInterface {
240 4
                return $carry->add($function($value));
241 4
            }
242
        );
243
    }
244
245
    /**
246
     * {@inheritdoc}
247
     */
248 2
    public function partition(callable $predicate): MapInterface
249
    {
250 2
        $truthy = $this->clear();
251 2
        $falsy = $this->clear();
252 2
        $partitions = $this->values->partition($predicate);
253 2
        $truthy->values = $partitions->get(true);
254 2
        $falsy->values = $partitions->get(false);
255
256 2
        return Map::of('bool', SetInterface::class)
257 2
            (true, $truthy)
258 2
            (false, $falsy);
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264 2
    public function join(string $separator): Str
265
    {
266 2
        return $this->values->join($separator);
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272 2
    public function sort(callable $function): StreamInterface
273
    {
274 2
        return $this->values->sort($function);
275
    }
276
277
    /**
278
     * {@inheritdoc}
279
     */
280 4
    public function merge(SetInterface $set): SetInterface
281
    {
282 4
        $this->validate($set);
283
284 2
        return $set->reduce(
285 2
            $this,
286
            function(SetInterface $carry, $value): SetInterface {
287 2
                return $carry->add($value);
288 2
            }
289
        );
290
    }
291
292
    /**
293
     * {@inheritdoc}
294
     */
295 8
    public function reduce($carry, callable $reducer)
296
    {
297 8
        return $this->values->reduce($carry, $reducer);
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303 8
    public function clear(): SetInterface
304
    {
305 8
        $self = clone $this;
306 8
        $self->values = $this->values->clear();
307
308 8
        return $self;
309
    }
310
311 2
    public function empty(): bool
312
    {
313 2
        return $this->values->empty();
314
    }
315
316
    /**
317
     * Make sure the set is compatible with the current one
318
     *
319
     * @param SetInterface $set
320
     *
321
     * @throws InvalidArgumentException
322
     *
323
     * @return void
324
     */
325 30
    private function validate(SetInterface $set)
326
    {
327 30
        if (!$set->type()->equals($this->type)) {
328 8
            throw new InvalidArgumentException(
329 8
                'The 2 sets does not reference the same type'
330
            );
331
        }
332 22
    }
333
}
334