1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Immutable\Set; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
7
|
|
|
Map, |
8
|
|
|
Sequence, |
9
|
|
|
Set, |
10
|
|
|
Type, |
11
|
|
|
ValidateArgument, |
12
|
|
|
Str, |
13
|
|
|
Exception\CannotGroupEmptyStructure, |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @template T |
18
|
|
|
*/ |
19
|
|
|
final class Primitive implements Implementation |
20
|
|
|
{ |
21
|
|
|
private string $type; |
22
|
|
|
private ValidateArgument $validate; |
23
|
|
|
private Sequence\Implementation $values; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param T $values |
|
|
|
|
27
|
|
|
*/ |
28
|
97 |
|
public function __construct(string $type, ...$values) |
29
|
|
|
{ |
30
|
97 |
|
$this->type = $type; |
31
|
97 |
|
$this->validate = Type::of($type); |
32
|
97 |
|
$this->values = (new Sequence\Primitive($type, ...$values))->distinct(); |
|
|
|
|
33
|
97 |
|
} |
34
|
|
|
|
35
|
1 |
|
public function type(): string |
36
|
|
|
{ |
37
|
1 |
|
return $this->type; |
38
|
|
|
} |
39
|
|
|
|
40
|
14 |
|
public function size(): int |
41
|
|
|
{ |
42
|
14 |
|
return $this->values->size(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
1 |
|
public function count(): int |
49
|
|
|
{ |
50
|
1 |
|
return $this->values->size(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return \Iterator<T> |
55
|
|
|
*/ |
56
|
25 |
|
public function iterator(): \Iterator |
57
|
|
|
{ |
58
|
25 |
|
return $this->values->iterator(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Implementation<T> $set |
63
|
|
|
* |
64
|
|
|
* @return self<T> |
65
|
|
|
*/ |
66
|
12 |
|
public function intersect(Implementation $set): self |
67
|
|
|
{ |
68
|
12 |
|
$self = $this->clear(); |
69
|
12 |
|
$self->values = $this->values->intersect( |
70
|
12 |
|
new Sequence\Primitive($this->type, ...$set->iterator()) |
71
|
|
|
); |
72
|
|
|
|
73
|
12 |
|
return $self; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param T $element |
78
|
|
|
* |
79
|
|
|
* @return self<T> |
80
|
|
|
*/ |
81
|
48 |
|
public function add($element): self |
82
|
|
|
{ |
83
|
48 |
|
if ($this->contains($element)) { |
84
|
5 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
48 |
|
$set = clone $this; |
88
|
48 |
|
$set->values = $this->values->add($element); |
89
|
|
|
|
90
|
48 |
|
return $set; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param T $element |
95
|
|
|
*/ |
96
|
50 |
|
public function contains($element): bool |
97
|
|
|
{ |
98
|
50 |
|
return $this->values->contains($element); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param T $element |
103
|
|
|
* |
104
|
|
|
* @return self<T> |
105
|
|
|
*/ |
106
|
2 |
|
public function remove($element): self |
107
|
|
|
{ |
108
|
2 |
|
if (!$this->contains($element)) { |
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
$index = $this->values->indexOf($element); |
113
|
2 |
|
$set = clone $this; |
114
|
2 |
|
$set->values = $this |
115
|
2 |
|
->values |
116
|
2 |
|
->slice(0, $index) |
117
|
2 |
|
->append($this->values->slice($index + 1, $this->size())); |
118
|
|
|
|
119
|
2 |
|
return $set; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Implementation<T> $set |
124
|
|
|
* |
125
|
|
|
* @return self<T> |
126
|
|
|
*/ |
127
|
2 |
|
public function diff(Implementation $set): self |
128
|
|
|
{ |
129
|
2 |
|
$self = clone $this; |
130
|
2 |
|
$self->values = $this->values->diff( |
131
|
2 |
|
new Sequence\Primitive($this->type, ...$set->iterator()) |
132
|
|
|
); |
133
|
|
|
|
134
|
2 |
|
return $self; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param Implementation<T> $set |
139
|
|
|
*/ |
140
|
10 |
|
public function equals(Implementation $set): bool |
141
|
|
|
{ |
142
|
10 |
|
if ($this->size() !== $set->size()) { |
143
|
3 |
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
10 |
|
return $this->intersect($set)->size() === $this->size(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param callable(T): bool $predicate |
151
|
|
|
* |
152
|
|
|
* @return self<T> |
153
|
|
|
*/ |
154
|
2 |
|
public function filter(callable $predicate): self |
155
|
|
|
{ |
156
|
2 |
|
$set = clone $this; |
157
|
2 |
|
$set->values = $this->values->filter($predicate); |
158
|
|
|
|
159
|
2 |
|
return $set; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param callable(T): void $function |
164
|
|
|
*/ |
165
|
2 |
|
public function foreach(callable $function): void |
166
|
|
|
{ |
167
|
2 |
|
$this->values->foreach($function); |
168
|
2 |
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @template D |
172
|
|
|
* @param callable(T): D $discriminator |
173
|
|
|
* |
174
|
|
|
* @throws CannotGroupEmptyStructure |
175
|
|
|
* |
176
|
|
|
* @return Map<D, Set<T>> |
177
|
|
|
*/ |
178
|
2 |
|
public function groupBy(callable $discriminator): Map |
179
|
|
|
{ |
180
|
|
|
/** @var Map<D, Sequence<T>> */ |
181
|
2 |
|
$map = $this->values->groupBy($discriminator); |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @psalm-suppress MixedReturnTypeCoercion |
185
|
|
|
* @var Map<D, Set<T>> |
186
|
|
|
*/ |
187
|
2 |
|
return $map->reduce( |
188
|
2 |
|
Map::of($map->keyType(), Set::class), |
189
|
|
|
function(Map $carry, $key, Sequence $values): Map { |
190
|
2 |
|
return ($carry)( |
191
|
2 |
|
$key, |
192
|
2 |
|
$values->toSetOf($this->type), |
193
|
|
|
); |
194
|
2 |
|
} |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param callable(T): T $function |
200
|
|
|
* |
201
|
|
|
* @return self<T> |
202
|
|
|
*/ |
203
|
4 |
|
public function map(callable $function): self |
204
|
|
|
{ |
205
|
|
|
/** |
206
|
|
|
* @psalm-suppress MissingClosureParamType |
207
|
|
|
* @psalm-suppress MissingClosureReturnType |
208
|
|
|
*/ |
209
|
|
|
$function = function($value) use ($function) { |
210
|
|
|
/** @var T $value */ |
211
|
4 |
|
$returned = $function($value); |
212
|
4 |
|
($this->validate)($returned, 1); |
213
|
|
|
|
214
|
2 |
|
return $returned; |
215
|
4 |
|
}; |
216
|
|
|
|
217
|
4 |
|
return $this->reduce( |
218
|
4 |
|
$this->clear(), |
219
|
|
|
function(self $carry, $value) use ($function): self { |
220
|
4 |
|
return $carry->add($function($value)); |
221
|
4 |
|
} |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param callable(T): bool $predicate |
227
|
|
|
* |
228
|
|
|
* @return Map<bool, Set<T>> |
229
|
|
|
*/ |
230
|
2 |
|
public function partition(callable $predicate): Map |
231
|
|
|
{ |
232
|
2 |
|
$partitions = $this->values->partition($predicate); |
233
|
|
|
/** @var Set<T> */ |
234
|
|
|
$truthy = $partitions |
235
|
2 |
|
->get(true) |
236
|
2 |
|
->toSetOf($this->type); |
237
|
|
|
/** @var Set<T> */ |
238
|
|
|
$falsy = $partitions |
239
|
2 |
|
->get(false) |
240
|
2 |
|
->toSetOf($this->type); |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @psalm-suppress InvalidScalarArgument |
244
|
|
|
* @psalm-suppress InvalidArgument |
245
|
|
|
*/ |
246
|
2 |
|
return Map::of('bool', Set::class) |
247
|
2 |
|
(true, $truthy) |
248
|
2 |
|
(false, $falsy); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param callable(T, T): int $function |
253
|
|
|
* |
254
|
|
|
* @return Sequence<T> |
255
|
|
|
*/ |
256
|
2 |
|
public function sort(callable $function): Sequence |
257
|
|
|
{ |
258
|
|
|
return $this |
259
|
2 |
|
->values |
260
|
2 |
|
->sort($function) |
261
|
2 |
|
->toSequenceOf($this->type); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param Implementation<T> $set |
266
|
|
|
* |
267
|
|
|
* @return self<T> |
268
|
|
|
*/ |
269
|
2 |
|
public function merge(Implementation $set): self |
270
|
|
|
{ |
271
|
2 |
|
return $set->reduce( |
272
|
2 |
|
$this, |
273
|
|
|
function(self $carry, $value): self { |
274
|
2 |
|
return $carry->add($value); |
275
|
2 |
|
} |
276
|
|
|
); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @template R |
281
|
|
|
* @param R $carry |
|
|
|
|
282
|
|
|
* @param callable(R, T): R $reducer |
283
|
|
|
* |
284
|
|
|
* @return R |
285
|
|
|
*/ |
286
|
62 |
|
public function reduce($carry, callable $reducer) |
287
|
|
|
{ |
288
|
62 |
|
return $this->values->reduce($carry, $reducer); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @return self<T> |
293
|
|
|
*/ |
294
|
17 |
|
public function clear(): self |
295
|
|
|
{ |
296
|
17 |
|
return new self($this->type); |
297
|
|
|
} |
298
|
|
|
|
299
|
4 |
|
public function empty(): bool |
300
|
|
|
{ |
301
|
4 |
|
return $this->values->empty(); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @template ST |
306
|
|
|
* |
307
|
|
|
* @param null|callable(T): \Generator<ST> $mapper |
308
|
|
|
* |
309
|
|
|
* @return Sequence<ST> |
310
|
|
|
*/ |
311
|
2 |
|
public function toSequenceOf(string $type, callable $mapper = null): Sequence |
312
|
|
|
{ |
313
|
2 |
|
return $this->values->toSequenceOf($type, $mapper); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @template ST |
318
|
|
|
* |
319
|
|
|
* @param null|callable(T): \Generator<ST> $mapper |
320
|
|
|
* |
321
|
|
|
* @return Set<ST> |
322
|
|
|
*/ |
323
|
2 |
|
public function toSetOf(string $type, callable $mapper = null): Set |
324
|
|
|
{ |
325
|
2 |
|
return $this->values->toSetOf($type, $mapper); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @template MT |
330
|
|
|
* @template MS |
331
|
|
|
* |
332
|
|
|
* @param callable(T): \Generator<MT, MS> $mapper |
333
|
|
|
* |
334
|
|
|
* @return Map<MT, MS> |
335
|
|
|
*/ |
336
|
2 |
|
public function toMapOf(string $key, string $value, callable $mapper): Map |
337
|
|
|
{ |
338
|
2 |
|
return $this->values->toMapOf($key, $value, $mapper); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths