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 ( 5fe34b...11f42d )
by Baptiste
03:53
created

Set::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 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 102
    public function __construct(string $type)
20
    {
21 102
        $this->type = new Str($type);
22 102
        $this->spec = $this->getSpecificationFor($type);
23 102
        $this->values = new Stream($type);
24 102
    }
25
26 3
    public static function of(string $type, ...$values): self
27
    {
28 3
        return array_reduce(
29 3
            $values,
30 3
            static function(self $self, $value): self {
31 3
                return $self->add($value);
32 3
            },
33 3
            new self($type)
34
        );
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 57
    public function type(): Str
41
    {
42 57
        return $this->type;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 6
    public function size(): int
49
    {
50 6
        return $this->values->size();
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 3
    public function count(): int
57
    {
58 3
        return $this->values->size();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 60
    public function toPrimitive()
65
    {
66 60
        return $this->values->toPrimitive();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 3
    public function current()
73
    {
74 3
        return $this->values->current();
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 3
    public function key()
81
    {
82 3
        return $this->values->key();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 3
    public function next()
89
    {
90 3
        $this->values->next();
91 3
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 3
    public function rewind()
97
    {
98 3
        $this->values->rewind();
99 3
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 3
    public function valid()
105
    {
106 3
        return $this->values->valid();
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 6 View Code Duplication
    public function intersect(SetInterface $set): SetInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114 6
        $this->validate($set);
115
116 3
        $newSet = clone $this;
117 3
        $newSet->values = $this->values->intersect(
118 3
            $set->reduce(
119 3
                $this->values->clear(),
120 3
                function(Stream $carry, $value): Stream {
121 3
                    return $carry->add($value);
122 3
                }
123
            )
124
        );
125
126 3
        return $newSet;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 87
    public function add($element): SetInterface
133
    {
134 87
        $this->spec->validate($element);
135
136 84
        if ($this->contains($element)) {
137 9
            return $this;
138
        }
139
140 84
        $set = clone $this;
141 84
        $set->values = $this->values->add($element);
142
143 84
        return $set;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 84
    public function contains($element): bool
150
    {
151 84
        return $this->values->contains($element);
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 3
    public function remove($element): SetInterface
158
    {
159 3
        if (!$this->contains($element)) {
160
            return $this;
161
        }
162
163 3
        $index = $this->values->indexOf($element);
164 3
        $set = clone $this;
165 3
        $set->values = $this
166 3
            ->values
167 3
            ->clear()
168 3
            ->append($this->values->slice(0, $index))
169 3
            ->append($this->values->slice($index + 1, $this->size()));
170
171 3
        return $set;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 6 View Code Duplication
    public function diff(SetInterface $set): SetInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179 6
        $this->validate($set);
180
181 3
        $newSet = clone $this;
182 3
        $newSet->values = $this->values->diff(
183 3
            $set->reduce(
184 3
                $this->values->clear(),
185 3
                function(Stream $carry, $value): Stream {
186 3
                    return $carry->add($value);
187 3
                }
188
            )
189
        );
190
191 3
        return $newSet;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197 24
    public function equals(SetInterface $set): bool
198
    {
199 24
        $this->validate($set);
200
201 21
        return $this->values->equals(
202 21
            $set->reduce(
203 21
                $this->values->clear(),
204 21
                function(Stream $carry, $value): Stream {
205 21
                    return $carry->add($value);
206 21
                }
207
            )
208
        );
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214 3
    public function filter(callable $predicate): SetInterface
215
    {
216 3
        $set = clone $this;
217 3
        $set->values = $this->values->filter($predicate);
218
219 3
        return $set;
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225 3
    public function foreach(callable $function): SetInterface
226
    {
227 3
        $this->values->foreach($function);
228
229 3
        return $this;
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 3
    public function groupBy(callable $discriminator): MapInterface
236
    {
237 3
        $map = $this->values->groupBy($discriminator);
238
239 3
        return $map->reduce(
240 3
            new Map((string) $map->keyType(), SetInterface::class),
241 3
            function(Map $carry, $key, StreamInterface $values): Map {
242 3
                return $carry->put(
243 3
                    $key,
244 3
                    $values->reduce(
245 3
                        $this->clear(),
246 3
                        function(Set $carry, $value): Set {
247 3
                            return $carry->add($value);
248 3
                        }
249
                    )
250
                );
251 3
            }
252
        );
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258 6
    public function map(callable $function): SetInterface
259
    {
260 6
        return $this->reduce(
261 6
            $this->clear(),
262 6
            function(self $carry, $value) use ($function): self {
263 6
                return $carry->add($function($value));
264 6
            }
265
        );
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271 3
    public function partition(callable $predicate): MapInterface
272
    {
273 3
        $truthy = $this->clear();
274 3
        $falsy = $this->clear();
275 3
        $partitions = $this->values->partition($predicate);
276 3
        $truthy->values = $partitions->get(true);
277 3
        $falsy->values = $partitions->get(false);
278
279 3
        return (new Map('bool', SetInterface::class))
280 3
            ->put(true, $truthy)
281 3
            ->put(false, $falsy);
282
    }
283
284
    /**
285
     * {@inheritdoc}
286
     */
287 3
    public function join(string $separator): Str
288
    {
289 3
        return $this->values->join($separator);
290
    }
291
292
    /**
293
     * {@inheritdoc}
294
     */
295 3
    public function sort(callable $function): StreamInterface
296
    {
297 3
        return $this->values->sort($function);
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303 6
    public function merge(SetInterface $set): SetInterface
304
    {
305 6
        $this->validate($set);
306
307 3
        return $set->reduce(
308 3
            $this,
309 3
            function(self $carry, $value): self {
310 3
                return $carry->add($value);
311 3
            }
312
        );
313
    }
314
315
    /**
316
     * {@inheritdoc}
317
     */
318 36
    public function reduce($carry, callable $reducer)
319
    {
320 36
        return $this->values->reduce($carry, $reducer);
321
    }
322
323
    /**
324
     * {@inheritdoc}
325
     */
326 12
    public function clear(): SetInterface
327
    {
328 12
        $self = clone $this;
329 12
        $self->values = $this->values->clear();
330
331 12
        return $self;
332
    }
333
334
    /**
335
     * Make sure the set is compatible with the current one
336
     *
337
     * @param SetInterface $set
338
     *
339
     * @throws InvalidArgumentException
340
     *
341
     * @return void
342
     */
343 39
    private function validate(SetInterface $set)
344
    {
345 39
        if (!$set->type()->equals($this->type)) {
0 ignored issues
show
Documentation introduced by
$this->type is of type object<Innmind\Immutable\Str>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
346 12
            throw new InvalidArgumentException(
347 12
                'The 2 sets does not reference the same type'
348
            );
349
        }
350 27
    }
351
}
352