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