1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | |||
4 | namespace Innmind\Immutable; |
||
5 | |||
6 | use Innmind\Immutable\{ |
||
7 | ValidateArgument\ClassType, |
||
8 | Exception\LogicException, |
||
9 | Exception\ElementNotFound, |
||
10 | Exception\CannotGroupEmptyStructure, |
||
11 | }; |
||
12 | |||
13 | /** |
||
14 | * @template T |
||
15 | * @template S |
||
16 | */ |
||
17 | final class Map implements \Countable |
||
18 | { |
||
19 | private Map\Implementation $implementation; |
||
20 | private string $keyType; |
||
21 | private string $valueType; |
||
22 | private ValidateArgument $validateKey; |
||
23 | private ValidateArgument $validateValue; |
||
24 | |||
25 | 61 | private function __construct(string $keyType, string $valueType) |
|
26 | { |
||
27 | 61 | $type = Type::of($keyType); |
|
28 | 61 | $this->implementation = new Map\DoubleIndex($keyType, $valueType); |
|
0 ignored issues
–
show
|
|||
29 | 61 | $this->keyType = $keyType; |
|
30 | 61 | $this->valueType = $valueType; |
|
31 | 61 | $this->validateKey = $type; |
|
32 | 61 | $this->validateValue = Type::of($valueType); |
|
33 | |||
34 | 61 | if ($type instanceof ClassType || $keyType === 'object') { |
|
35 | 4 | $this->implementation = new Map\ObjectKeys($keyType, $valueType); |
|
0 ignored issues
–
show
The type
Innmind\Immutable\Map\ObjectKeys 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...
|
|||
36 | 60 | } else if (\in_array($keyType, ['int', 'integer', 'string'], true)) { |
|
37 | 50 | $this->implementation = new Map\Primitive($keyType, $valueType); |
|
0 ignored issues
–
show
The type
Innmind\Immutable\Map\Primitive 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...
|
|||
38 | } |
||
39 | 61 | } |
|
40 | |||
41 | /** |
||
42 | * @param list<T> $keys |
||
43 | * @param list<S> $values |
||
44 | * |
||
45 | * @return self<T, S> |
||
46 | */ |
||
47 | 62 | public static function of( |
|
48 | string $key, |
||
49 | string $value, |
||
50 | array $keys = [], |
||
51 | array $values = [] |
||
52 | ): self { |
||
53 | 62 | $keys = \array_values($keys); |
|
54 | 62 | $values = \array_values($values); |
|
55 | |||
56 | 62 | if (\count($keys) !== \count($values)) { |
|
57 | 1 | throw new LogicException('Different sizes of keys and values'); |
|
58 | } |
||
59 | |||
60 | 61 | $self = new self($key, $value); |
|
61 | |||
62 | 61 | foreach ($keys as $i => $key) { |
|
63 | 1 | $self = $self->put($key, $values[$i]); |
|
64 | } |
||
65 | |||
66 | 61 | return $self; |
|
67 | } |
||
68 | |||
69 | 18 | public function isOfType(string $key, string $value): bool |
|
70 | { |
||
71 | 18 | return $this->keyType === $key && $this->valueType === $value; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Return the key type for this map |
||
76 | */ |
||
77 | 26 | public function keyType(): string |
|
78 | { |
||
79 | 26 | return $this->implementation->keyType(); |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Return the value type for this map |
||
84 | */ |
||
85 | 24 | public function valueType(): string |
|
86 | { |
||
87 | 24 | return $this->implementation->valueType(); |
|
88 | } |
||
89 | |||
90 | 8 | public function size(): int |
|
91 | { |
||
92 | 8 | return $this->implementation->size(); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 21 | public function count(): int |
|
99 | { |
||
100 | 21 | return $this->implementation->count(); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Set a new key/value pair |
||
105 | * |
||
106 | * @param T $key |
||
107 | * @param S $value |
||
108 | * |
||
109 | * @return self<T, S> |
||
110 | */ |
||
111 | 54 | public function put($key, $value): self |
|
112 | { |
||
113 | 54 | ($this->validateKey)($key, 1); |
|
114 | 53 | ($this->validateValue)($value, 2); |
|
115 | |||
116 | 52 | $map = clone $this; |
|
117 | 52 | $map->implementation = $this->implementation->put($key, $value); |
|
118 | |||
119 | 52 | return $map; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Alias for put method in order to have a syntax similar to a true tuple |
||
124 | * when constructing the map |
||
125 | * |
||
126 | * Example: |
||
127 | * <code> |
||
128 | * Map::of('int', 'int') |
||
129 | * (1, 2) |
||
130 | * (3, 4) |
||
131 | * </code> |
||
132 | * |
||
133 | * @param T $key |
||
134 | * @param S $value |
||
135 | * |
||
136 | * @return self<T, S> |
||
137 | */ |
||
138 | 36 | public function __invoke($key, $value): self |
|
139 | { |
||
140 | 36 | return $this->put($key, $value); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Return the element with the given key |
||
145 | * |
||
146 | * @param T $key |
||
147 | * |
||
148 | * @throws ElementNotFound |
||
149 | * |
||
150 | * @return S |
||
151 | */ |
||
152 | 37 | public function get($key) |
|
153 | { |
||
154 | 37 | ($this->validateKey)($key, 1); |
|
155 | |||
156 | /** @var S */ |
||
157 | 37 | return $this->implementation->get($key); |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Check if there is an element for the given key |
||
162 | * |
||
163 | * @param T $key |
||
164 | */ |
||
165 | 12 | public function contains($key): bool |
|
166 | { |
||
167 | 12 | ($this->validateKey)($key, 1); |
|
168 | |||
169 | 12 | return $this->implementation->contains($key); |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Return an empty map given the same given type |
||
174 | * |
||
175 | * @return self<T, S> |
||
176 | */ |
||
177 | 5 | public function clear(): self |
|
178 | { |
||
179 | 5 | $map = clone $this; |
|
180 | 5 | $map->implementation = $this->implementation->clear(); |
|
181 | |||
182 | 5 | return $map; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * Check if the two maps are equal |
||
187 | * |
||
188 | * @param self<T, S> $map |
||
189 | */ |
||
190 | 7 | public function equals(self $map): bool |
|
191 | { |
||
192 | 7 | assertMap($this->keyType, $this->valueType, $map, 1); |
|
193 | |||
194 | 6 | return $this->implementation->equals($map->implementation); |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Filter the map based on the given predicate |
||
199 | * |
||
200 | * @param callable(T, S): bool $predicate |
||
201 | * |
||
202 | * @return self<T, S> |
||
203 | */ |
||
204 | 1 | public function filter(callable $predicate): self |
|
205 | { |
||
206 | 1 | $map = $this->clear(); |
|
207 | 1 | $map->implementation = $this->implementation->filter($predicate); |
|
208 | |||
209 | 1 | return $map; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Run the given function for each element of the map |
||
214 | * |
||
215 | * @param callable(T, S): void $function |
||
216 | */ |
||
217 | 1 | public function foreach(callable $function): void |
|
218 | { |
||
219 | 1 | $this->implementation->foreach($function); |
|
220 | 1 | } |
|
221 | |||
222 | /** |
||
223 | * Return a new map of pairs' sequences grouped by keys determined with the given |
||
224 | * discriminator function |
||
225 | * |
||
226 | * @template D |
||
227 | * @param callable(T, S): D $discriminator |
||
228 | * |
||
229 | * @throws CannotGroupEmptyStructure |
||
230 | * |
||
231 | * @return self<D, self<T, S>> |
||
232 | */ |
||
233 | 2 | public function groupBy(callable $discriminator): self |
|
234 | { |
||
235 | 2 | return $this->implementation->groupBy($discriminator); |
|
236 | } |
||
237 | |||
238 | /** |
||
239 | * Return all keys |
||
240 | * |
||
241 | * @return Set<T> |
||
242 | */ |
||
243 | 10 | public function keys(): Set |
|
244 | { |
||
245 | 10 | return $this->implementation->keys(); |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * Return all values |
||
250 | * |
||
251 | * @return Sequence<S> |
||
252 | */ |
||
253 | 9 | public function values(): Sequence |
|
254 | { |
||
255 | 9 | return $this->implementation->values(); |
|
256 | } |
||
257 | |||
258 | /** |
||
259 | * Apply the given function on all elements and return a new map |
||
260 | * |
||
261 | * Keys can't be modified |
||
262 | * |
||
263 | * @param callable(T, S): (S|Pair<T, S>) $function |
||
264 | * |
||
265 | * @return self<T, S> |
||
266 | */ |
||
267 | 3 | public function map(callable $function): self |
|
268 | { |
||
269 | 3 | $map = $this->clear(); |
|
270 | 3 | $map->implementation = $this->implementation->map($function); |
|
271 | |||
272 | 1 | return $map; |
|
273 | } |
||
274 | |||
275 | /** |
||
276 | * Remove the element with the given key |
||
277 | * |
||
278 | * @param T $key |
||
279 | * |
||
280 | * @return self<T, S> |
||
281 | */ |
||
282 | 1 | public function remove($key): self |
|
283 | { |
||
284 | 1 | ($this->validateKey)($key, 1); |
|
285 | |||
286 | 1 | $map = clone $this; |
|
287 | 1 | $map->implementation = $this->implementation->remove($key); |
|
288 | |||
289 | 1 | return $map; |
|
290 | } |
||
291 | |||
292 | /** |
||
293 | * Create a new map by combining both maps |
||
294 | * |
||
295 | * @param self<T, S> $map |
||
296 | * |
||
297 | * @return self<T, S> |
||
298 | */ |
||
299 | 2 | public function merge(self $map): self |
|
300 | { |
||
301 | 2 | assertMap($this->keyType, $this->valueType, $map, 1); |
|
302 | |||
303 | 1 | $self = clone $this; |
|
304 | 1 | $self->implementation = $this->implementation->merge($map->implementation); |
|
305 | |||
306 | 1 | return $self; |
|
307 | } |
||
308 | |||
309 | /** |
||
310 | * Return a map of 2 maps partitioned according to the given predicate |
||
311 | * |
||
312 | * @param callable(T, S): bool $predicate |
||
313 | * |
||
314 | * @return self<bool, self<T, S>> |
||
315 | */ |
||
316 | 1 | public function partition(callable $predicate): self |
|
317 | { |
||
318 | 1 | return $this->implementation->partition($predicate); |
|
319 | } |
||
320 | |||
321 | /** |
||
322 | * Reduce the map to a single value |
||
323 | * |
||
324 | * @template R |
||
325 | * @param R $carry |
||
326 | * @param callable(R, T, S): R $reducer |
||
327 | * |
||
328 | * @return R |
||
329 | */ |
||
330 | 4 | public function reduce($carry, callable $reducer) |
|
331 | { |
||
332 | 4 | return $this->implementation->reduce($carry, $reducer); |
|
333 | } |
||
334 | |||
335 | public function empty(): bool |
||
336 | { |
||
337 | return $this->implementation->empty(); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @template ST |
||
342 | * |
||
343 | * @param callable(T, S): \Generator<ST> $mapper |
||
344 | * |
||
345 | * @return Sequence<ST> |
||
346 | */ |
||
347 | public function toSequenceOf(string $type, callable $mapper): Sequence |
||
348 | { |
||
349 | return $this->implementation->toSequenceOf($type, $mapper); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @template ST |
||
354 | * |
||
355 | * @param callable(T, S): \Generator<ST> $mapper |
||
356 | * |
||
357 | * @return Set<ST> |
||
358 | */ |
||
359 | 1 | public function toSetOf(string $type, callable $mapper): Set |
|
360 | { |
||
361 | 1 | return $this->implementation->toSetOf($type, $mapper); |
|
362 | } |
||
363 | |||
364 | /** |
||
365 | * @template MT |
||
366 | * @template MS |
||
367 | * |
||
368 | * @param null|callable(T, S): \Generator<MT, MS> $mapper |
||
369 | * |
||
370 | * @return self<MT, MS> |
||
371 | */ |
||
372 | 1 | public function toMapOf(string $key, string $value, callable $mapper = null): self |
|
373 | { |
||
374 | 1 | return $this->implementation->toMapOf($key, $value, $mapper); |
|
375 | } |
||
376 | } |
||
377 |
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