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 ( daf5da...11189b )
by Baptiste
02:20
created

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