1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | |||
4 | namespace Innmind\Immutable; |
||
5 | |||
6 | use Innmind\Immutable\Exception\CannotGroupEmptyStructure; |
||
7 | |||
8 | /** |
||
9 | * @template T |
||
10 | */ |
||
11 | final class Set implements \Countable |
||
12 | { |
||
13 | private string $type; |
||
14 | private ValidateArgument $validate; |
||
15 | private Set\Implementation $implementation; |
||
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | 78 | private function __construct(string $type, Set\Implementation $implementation) |
|
21 | { |
||
22 | 78 | $this->type = $type; |
|
23 | 78 | $this->validate = Type::of($type); |
|
24 | 78 | $this->implementation = $implementation; |
|
25 | 78 | } |
|
26 | |||
27 | /** |
||
28 | * @param T $values |
||
29 | * |
||
30 | * @return self<T> |
||
31 | */ |
||
32 | 66 | public static function of(string $type, ...$values): self |
|
33 | { |
||
34 | 66 | return new self($type, new Set\Primitive($type, ...$values)); |
|
0 ignored issues
–
show
|
|||
35 | } |
||
36 | 66 | ||
37 | /** |
||
38 | * It will load the values inside the generator only upon the first use |
||
39 | * of the set |
||
40 | * |
||
41 | * Use this mode when the amount of data may not fit in memory |
||
42 | * |
||
43 | * @param \Generator<T> $generator |
||
44 | 3 | * |
|
45 | * @return self<T> |
||
46 | 3 | */ |
|
47 | public static function defer(string $type, \Generator $generator): self |
||
48 | 3 | { |
|
49 | return new self($type, new Set\Defer($type, $generator)); |
||
0 ignored issues
–
show
The type
Innmind\Immutable\Set\Defer was not found. Maybe you did not declare it correctly or list all dependencies?
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
|
|||
50 | } |
||
51 | |||
52 | /** |
||
53 | * It will call the given function every time a new operation is done on the |
||
54 | * set. This means the returned structure may not be truly immutable as |
||
55 | * between the calls the underlying source may change. |
||
56 | 2 | * |
|
57 | * Use this mode when calling to an external source (meaning IO bound) such |
||
58 | * as parsing a file or calling an API |
||
59 | 2 | * |
|
60 | * @param callable(): \Generator<T> $generator |
||
61 | 2 | * |
|
62 | * @return self<T> |
||
63 | */ |
||
64 | public static function lazy(string $type, callable $generator): self |
||
65 | { |
||
66 | return new self($type, new Set\Lazy($type, $generator)); |
||
0 ignored issues
–
show
The type
Innmind\Immutable\Set\Lazy was not found. Maybe you did not declare it correctly or list all dependencies?
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
|
|||
67 | 6 | } |
|
68 | |||
69 | /** |
||
70 | 6 | * @param mixed $values |
|
71 | * |
||
72 | 6 | * @return self<mixed> |
|
73 | */ |
||
74 | public static function mixed(...$values): self |
||
75 | { |
||
76 | return new self('mixed', new Set\Primitive('mixed', ...$values)); |
||
77 | } |
||
78 | 1 | ||
79 | /** |
||
80 | * @return self<int> |
||
81 | 1 | */ |
|
82 | public static function ints(int ...$values): self |
||
83 | 1 | { |
|
84 | /** @var self<int> */ |
||
85 | $self = new self('int', new Set\Primitive('int', ...$values)); |
||
86 | |||
87 | return $self; |
||
88 | } |
||
89 | 1 | ||
90 | /** |
||
91 | * @return self<float> |
||
92 | 1 | */ |
|
93 | public static function floats(float ...$values): self |
||
94 | 1 | { |
|
95 | /** @var self<float> */ |
||
96 | $self = new self('float', new Set\Primitive('float', ...$values)); |
||
97 | |||
98 | return $self; |
||
99 | } |
||
100 | 1 | ||
101 | /** |
||
102 | * @return self<string> |
||
103 | 1 | */ |
|
104 | public static function strings(string ...$values): self |
||
105 | 1 | { |
|
106 | /** @var self<string> */ |
||
107 | $self = new self('string', new Set\Primitive('string', ...$values)); |
||
108 | 22 | ||
109 | return $self; |
||
110 | 22 | } |
|
111 | |||
112 | /** |
||
113 | * @return self<object> |
||
114 | */ |
||
115 | public static function objects(object ...$values): self |
||
116 | 24 | { |
|
117 | /** @var self<object> */ |
||
118 | 24 | $self = new self('object', new Set\Primitive('object', ...$values)); |
|
119 | |||
120 | return $self; |
||
121 | 1 | } |
|
122 | |||
123 | 1 | public function isOfType(string $type): bool |
|
124 | { |
||
125 | return $this->type === $type; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | 1 | * Return the type of this set |
|
130 | */ |
||
131 | 1 | public function type(): string |
|
132 | { |
||
133 | return $this->type; |
||
134 | } |
||
135 | |||
136 | public function size(): int |
||
137 | { |
||
138 | return $this->implementation->size(); |
||
139 | } |
||
140 | |||
141 | 2 | /** |
|
142 | * {@inheritdoc} |
||
143 | 2 | */ |
|
144 | public function count(): int |
||
145 | 1 | { |
|
146 | 1 | return $this->implementation->size(); |
|
147 | 1 | } |
|
148 | |||
149 | /** |
||
150 | 1 | * Intersect this set with the given one |
|
151 | * |
||
152 | * @param self<T> $set |
||
153 | * |
||
154 | * @return self<T> |
||
155 | */ |
||
156 | public function intersect(self $set): self |
||
157 | { |
||
158 | assertSet($this->type, $set, 1); |
||
159 | |||
160 | 46 | $newSet = clone $this; |
|
161 | $newSet->implementation = $this->implementation->intersect( |
||
162 | 46 | $set->implementation, |
|
163 | ); |
||
164 | 45 | ||
165 | 45 | return $newSet; |
|
166 | } |
||
167 | 45 | ||
168 | /** |
||
169 | * Add an element to the set |
||
170 | * |
||
171 | * @param T $element |
||
172 | * |
||
173 | * @return self<T> |
||
174 | */ |
||
175 | public function add($element): self |
||
176 | { |
||
177 | return ($this)($element); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Add an element to the set |
||
182 | * |
||
183 | 31 | * Example: |
|
184 | * <code> |
||
185 | 31 | * Set::of('int')(1)(3) |
|
186 | * </code> |
||
187 | * |
||
188 | * @param T $element |
||
189 | * |
||
190 | * @return self<T> |
||
191 | */ |
||
192 | public function __invoke($element): self |
||
193 | 1 | { |
|
194 | ($this->validate)($element, 1); |
||
195 | 1 | ||
196 | $self = clone $this; |
||
197 | 1 | $self->implementation = ($this->implementation)($element); |
|
198 | |||
199 | return $self; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Check if the set contains the given element |
||
204 | * |
||
205 | * @param T $element |
||
206 | */ |
||
207 | 1 | public function contains($element): bool |
|
208 | { |
||
209 | 1 | ($this->validate)($element, 1); |
|
210 | |||
211 | 1 | return $this->implementation->contains($element); |
|
212 | 1 | } |
|
213 | |||
214 | 1 | /** |
|
215 | * Remove the element from the set |
||
216 | * |
||
217 | * @param T $element |
||
218 | * |
||
219 | * @return self<T> |
||
220 | */ |
||
221 | public function remove($element): self |
||
222 | { |
||
223 | ($this->validate)($element, 1); |
||
224 | 2 | ||
225 | $self = clone $this; |
||
226 | 2 | $self->implementation = $this->implementation->remove($element); |
|
227 | |||
228 | 1 | return $self; |
|
229 | 1 | } |
|
230 | 1 | ||
231 | /** |
||
232 | * Return the diff between this set and the given one |
||
233 | 1 | * |
|
234 | * @param self<T> $set |
||
235 | * |
||
236 | * @return self<T> |
||
237 | */ |
||
238 | public function diff(self $set): self |
||
239 | { |
||
240 | assertSet($this->type, $set, 1); |
||
241 | 10 | ||
242 | $self = clone $this; |
||
243 | 10 | $self->implementation = $this->implementation->diff( |
|
244 | $set->implementation, |
||
245 | 9 | ); |
|
246 | |||
247 | return $self; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Check if the given set is identical to this one |
||
252 | * |
||
253 | * @param self<T> $set |
||
254 | */ |
||
255 | 1 | public function equals(self $set): bool |
|
256 | { |
||
257 | 1 | assertSet($this->type, $set, 1); |
|
258 | 1 | ||
259 | return $this->implementation->equals($set->implementation); |
||
260 | 1 | } |
|
261 | |||
262 | /** |
||
263 | * Return all elements that satisfy the given predicate |
||
264 | * |
||
265 | * @param callable(T): bool $predicate |
||
266 | * |
||
267 | * @return self<T> |
||
268 | 1 | */ |
|
269 | public function filter(callable $predicate): self |
||
270 | 1 | { |
|
271 | 1 | $set = clone $this; |
|
272 | $set->implementation = $this->implementation->filter($predicate); |
||
273 | |||
274 | return $set; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Apply the given function to all elements of the set |
||
279 | * |
||
280 | * @param callable(T): void $function |
||
281 | */ |
||
282 | public function foreach(callable $function): void |
||
283 | { |
||
284 | 1 | $this->implementation->foreach($function); |
|
285 | } |
||
286 | 1 | ||
287 | /** |
||
288 | * Return a new map of pairs grouped by keys determined with the given |
||
289 | * discriminator function |
||
290 | * |
||
291 | * @template D |
||
292 | * @param callable(T): D $discriminator |
||
293 | * |
||
294 | * @throws CannotGroupEmptyStructure |
||
295 | * |
||
296 | 2 | * @return Map<D, self<T>> |
|
297 | */ |
||
298 | 2 | public function groupBy(callable $discriminator): Map |
|
299 | 2 | { |
|
300 | return $this->implementation->groupBy($discriminator); |
||
301 | 1 | } |
|
302 | |||
303 | /** |
||
304 | * Return a new set by applying the given function to all elements |
||
305 | * |
||
306 | * @param callable(T): T $function |
||
307 | * |
||
308 | * @return self<T> |
||
309 | */ |
||
310 | public function map(callable $function): self |
||
311 | 1 | { |
|
312 | $self = $this->clear(); |
||
313 | 1 | $self->implementation = $this->implementation->map($function); |
|
314 | |||
315 | return $self; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Return a sequence of 2 sets partitioned according to the given predicate |
||
320 | * |
||
321 | * @param callable(T): bool $predicate |
||
322 | * |
||
323 | 1 | * @return Map<bool, self<T>> |
|
324 | */ |
||
325 | 1 | public function partition(callable $predicate): Map |
|
326 | { |
||
327 | return $this->implementation->partition($predicate); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Return a sequence sorted with the given function |
||
332 | * |
||
333 | * @param callable(T, T): int $function |
||
334 | * |
||
335 | 2 | * @return Sequence<T> |
|
336 | */ |
||
337 | 2 | public function sort(callable $function): Sequence |
|
338 | { |
||
339 | 1 | return $this->implementation->sort($function); |
|
340 | 1 | } |
|
341 | 1 | ||
342 | /** |
||
343 | * Create a new set with elements of both sets |
||
344 | 1 | * |
|
345 | * @param self<T> $set |
||
346 | * |
||
347 | * @return self<T> |
||
348 | */ |
||
349 | public function merge(self $set): self |
||
350 | { |
||
351 | assertSet($this->type, $set, 1); |
||
352 | |||
353 | $self = clone $this; |
||
354 | $self->implementation = $this->implementation->merge( |
||
355 | $set->implementation, |
||
356 | 60 | ); |
|
357 | |||
358 | 60 | return $self; |
|
359 | } |
||
360 | |||
361 | /** |
||
362 | * Reduce the set to a single value |
||
363 | * |
||
364 | * @template R |
||
365 | * @param R $carry |
||
366 | 2 | * @param callable(R, T): R $reducer |
|
367 | * |
||
368 | 2 | * @return R |
|
369 | 2 | */ |
|
370 | public function reduce($carry, callable $reducer) |
||
371 | 2 | { |
|
372 | return $this->implementation->reduce($carry, $reducer); |
||
373 | } |
||
374 | 3 | ||
375 | /** |
||
376 | 3 | * Return a set of the same type but without any value |
|
377 | * |
||
378 | * @return self<T> |
||
379 | */ |
||
380 | public function clear(): self |
||
381 | { |
||
382 | $self = clone $this; |
||
383 | $self->implementation = $this->implementation->clear(); |
||
384 | |||
385 | return $self; |
||
386 | 1 | } |
|
387 | |||
388 | 1 | public function empty(): bool |
|
389 | { |
||
390 | return $this->implementation->empty(); |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @template ST |
||
395 | * |
||
396 | * @param null|callable(T): \Generator<ST> $mapper |
||
397 | * |
||
398 | 1 | * @return Sequence<ST> |
|
399 | */ |
||
400 | 1 | public function toSequenceOf(string $type, callable $mapper = null): Sequence |
|
401 | { |
||
402 | return $this->implementation->toSequenceOf($type, $mapper); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @template ST |
||
407 | * |
||
408 | * @param null|callable(T): \Generator<ST> $mapper |
||
409 | * |
||
410 | * @return self<ST> |
||
411 | */ |
||
412 | public function toSetOf(string $type, callable $mapper = null): self |
||
413 | 1 | { |
|
414 | return $this->implementation->toSetOf($type, $mapper); |
||
415 | 1 | } |
|
416 | |||
417 | /** |
||
418 | * @template MT |
||
419 | * @template MS |
||
420 | * |
||
421 | * @param callable(T): \Generator<MT, MS> $mapper |
||
422 | * |
||
423 | * @return Map<MT, MS> |
||
424 | */ |
||
425 | public function toMapOf(string $key, string $value, callable $mapper): Map |
||
426 | { |
||
427 | return $this->implementation->toMapOf($key, $value, $mapper); |
||
428 | } |
||
429 | } |
||
430 |
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