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 ( 9a685f...99d534 )
by Baptiste
02:17
created

Set.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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