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 | 19 | public function __construct(string $type) |
|
17 | { |
||
18 | 19 | $this->type = $type; |
|
19 | 19 | $this->spec = $this->getSpecFor($type); |
|
20 | 19 | $this->values = new Sequence; |
|
21 | 19 | } |
|
22 | |||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | 11 | public function type(): string |
|
27 | { |
||
28 | 11 | 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 | 10 | public function toPrimitive() |
|
51 | { |
||
52 | 10 | 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
|
|||
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 | 15 | public function add($element): SetInterface |
|
114 | { |
||
115 | 15 | $this->spec->validate($element); |
|
116 | |||
117 | 14 | if ($this->contains($element)) { |
|
118 | 1 | return $this; |
|
119 | } |
||
120 | |||
121 | 14 | $set = clone $this; |
|
122 | 14 | $set->values = $this->values->add($element); |
|
123 | |||
124 | 14 | return $set; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 14 | public function contains($element): bool |
|
131 | { |
||
132 | 14 | 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. ![]() |
|||
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 | 2 | public function equals(SetInterface $set): bool |
|
172 | { |
||
173 | 2 | $this->validate($set); |
|
174 | |||
175 | 1 | return $this->values->equals( |
|
176 | 1 | 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 | 1 | public function foreach(\Closure $function): SetInterface |
|
195 | { |
||
196 | 1 | $this->values->foreach($function); |
|
197 | |||
198 | 1 | 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): SequenceInterface |
|
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 Sequence($truthy, $falsy); |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | 1 | public function join(string $separator): StringPrimitive |
|
238 | { |
||
239 | 1 | return $this->values->join($separator); |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | 1 | public function sort(\Closure $function): SequenceInterface |
|
246 | { |
||
247 | 1 | return $this->values->sort($function); |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * Make sure the set is compatible with the current one |
||
252 | * |
||
253 | * @param SetInterface $set |
||
254 | * |
||
255 | * @throws InvalidArgumentException |
||
256 | * |
||
257 | * @return void |
||
258 | */ |
||
259 | 6 | private function validate(SetInterface $set) |
|
260 | { |
||
261 | 6 | if ($set->type() !== $this->type) { |
|
262 | 3 | throw new InvalidArgumentException( |
|
263 | 3 | 'The 2 sets does not reference the same type' |
|
264 | ); |
||
265 | } |
||
266 | 3 | } |
|
267 | } |
||
268 |
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.