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 ( 5205fe...5906b8 )
by Baptiste
03:02
created

Set   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 319
Duplicated Lines 0 %

Test Coverage

Coverage 99.15%

Importance

Changes 0
Metric Value
eloc 90
dl 0
loc 319
ccs 117
cts 118
cp 0.9915
rs 9.84
c 0
b 0
f 0
wmc 32

28 Methods

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