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 ( 5a9a96...099867 )
by Baptiste
01:51
created

Set   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 341
Duplicated Lines 9.38 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 99.25%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 8
dl 32
loc 341
ccs 133
cts 134
cp 0.9925
rs 9.6
c 0
b 0
f 0

28 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A of() 0 10 1
A type() 0 4 1
A size() 0 4 1
A count() 0 4 1
A toPrimitive() 0 4 1
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A rewind() 0 4 1
A valid() 0 4 1
A intersect() 16 16 1
A add() 0 13 2
A contains() 0 4 1
A remove() 0 16 2
A diff() 16 16 1
A equals() 0 10 2
A filter() 0 7 1
A foreach() 0 6 1
A groupBy() 0 19 1
A map() 0 9 1
A partition() 0 12 1
A join() 0 4 1
A sort() 0 4 1
A merge() 0 11 1
A reduce() 0 4 1
A clear() 0 7 1
A validate() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 6
    public static function of(string $type, ...$values): self
27
    {
28 6
        return array_reduce(
29 6
            $values,
30 6
            static function(self $self, $value): self {
31 3
                return $self->add($value);
32 6
            },
33 6
            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 27
    public function size(): int
49
    {
50 27
        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 27 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 27
        $this->validate($set);
115
116 24
        $newSet = clone $this;
117 24
        $newSet->values = $this->values->intersect(
118 24
            $set->reduce(
119 24
                $this->values->clear(),
120 24
                function(Stream $carry, $value): Stream {
121 24
                    return $carry->add($value);
122 24
                }
123
            )
124
        );
125
126 24
        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
        if ($this->size() !== $set->size()) {
202 6
            return false;
203
        }
204
205 21
        return $this->intersect($set)->size() === $this->size();
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211 3
    public function filter(callable $predicate): SetInterface
212
    {
213 3
        $set = clone $this;
214 3
        $set->values = $this->values->filter($predicate);
215
216 3
        return $set;
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222 3
    public function foreach(callable $function): SetInterface
223
    {
224 3
        $this->values->foreach($function);
225
226 3
        return $this;
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232 3
    public function groupBy(callable $discriminator): MapInterface
233
    {
234 3
        $map = $this->values->groupBy($discriminator);
235
236 3
        return $map->reduce(
237 3
            new Map((string) $map->keyType(), SetInterface::class),
238 3
            function(Map $carry, $key, StreamInterface $values): Map {
239 3
                return $carry->put(
240 3
                    $key,
241 3
                    $values->reduce(
242 3
                        $this->clear(),
243 3
                        function(Set $carry, $value): Set {
244 3
                            return $carry->add($value);
245 3
                        }
246
                    )
247
                );
248 3
            }
249
        );
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255 6
    public function map(callable $function): SetInterface
256
    {
257 6
        return $this->reduce(
258 6
            $this->clear(),
259 6
            function(self $carry, $value) use ($function): self {
260 6
                return $carry->add($function($value));
261 6
            }
262
        );
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268 3
    public function partition(callable $predicate): MapInterface
269
    {
270 3
        $truthy = $this->clear();
271 3
        $falsy = $this->clear();
272 3
        $partitions = $this->values->partition($predicate);
273 3
        $truthy->values = $partitions->get(true);
274 3
        $falsy->values = $partitions->get(false);
275
276 3
        return (new Map('bool', SetInterface::class))
277 3
            ->put(true, $truthy)
278 3
            ->put(false, $falsy);
279
    }
280
281
    /**
282
     * {@inheritdoc}
283
     */
284 3
    public function join(string $separator): Str
285
    {
286 3
        return $this->values->join($separator);
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292 3
    public function sort(callable $function): StreamInterface
293
    {
294 3
        return $this->values->sort($function);
295
    }
296
297
    /**
298
     * {@inheritdoc}
299
     */
300 6
    public function merge(SetInterface $set): SetInterface
301
    {
302 6
        $this->validate($set);
303
304 3
        return $set->reduce(
305 3
            $this,
306 3
            function(self $carry, $value): self {
307 3
                return $carry->add($value);
308 3
            }
309
        );
310
    }
311
312
    /**
313
     * {@inheritdoc}
314
     */
315 36
    public function reduce($carry, callable $reducer)
316
    {
317 36
        return $this->values->reduce($carry, $reducer);
318
    }
319
320
    /**
321
     * {@inheritdoc}
322
     */
323 12
    public function clear(): SetInterface
324
    {
325 12
        $self = clone $this;
326 12
        $self->values = $this->values->clear();
327
328 12
        return $self;
329
    }
330
331
    /**
332
     * Make sure the set is compatible with the current one
333
     *
334
     * @param SetInterface $set
335
     *
336
     * @throws InvalidArgumentException
337
     *
338
     * @return void
339
     */
340 39
    private function validate(SetInterface $set)
341
    {
342 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...
343 12
            throw new InvalidArgumentException(
344 12
                'The 2 sets does not reference the same type'
345
            );
346
        }
347 27
    }
348
}
349