Total Complexity | 41 |
Total Lines | 381 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like HashMap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HashMap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
64 | class HashMap extends BaseCollection implements |
||
65 | MapInterface, |
||
66 | MergeableInterface, |
||
67 | SortableInterface |
||
68 | { |
||
69 | /** |
||
70 | * Create new instance |
||
71 | * @param mixed $keyType |
||
72 | * @param mixed $valueType |
||
73 | * @param array<mixed, T> $initials |
||
74 | */ |
||
75 | public function __construct( |
||
76 | protected mixed $keyType, |
||
77 | protected mixed $valueType, |
||
78 | array $initials = [] |
||
79 | ) { |
||
80 | foreach ($initials as $key => $value) { |
||
81 | $this->validateEntry($key, $value); |
||
82 | } |
||
83 | |||
84 | parent::__construct($initials); |
||
85 | $this->initializePairs($initials); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * |
||
90 | * @param mixed $key |
||
91 | * @param T $value |
||
|
|||
92 | * @return void |
||
93 | */ |
||
94 | public function add(mixed $key, mixed $value): void |
||
95 | { |
||
96 | $this->validateEntry($key, $value); |
||
97 | $this->data->offsetSet($key, new Pair($key, $value)); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * |
||
102 | * @param BaseCollection<T> $collection |
||
103 | * @return HashMap<T> |
||
104 | * @throws InvalidOperationException |
||
105 | */ |
||
106 | public function diff(BaseCollection $collection): BaseCollection |
||
107 | { |
||
108 | if (!$collection instanceof self) { |
||
109 | throw new InvalidOperationException( |
||
110 | 'You should only compare a Map against another Map' |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | if ($this->keyType !== $collection->getKeyType()) { |
||
115 | throw new InvalidOperationException(sprintf( |
||
116 | 'The key type for this map is [%s], you cannot pass a map with [%s] as key type', |
||
117 | $this->keyType, |
||
118 | $collection->getKeyType() |
||
119 | )); |
||
120 | } |
||
121 | |||
122 | if ($this->valueType !== $collection->getValueType()) { |
||
123 | throw new InvalidOperationException(sprintf( |
||
124 | 'The value type for this map is [%s], you cannot pass a map with [%s] as value type', |
||
125 | $this->keyType, |
||
126 | $collection->getKeyType() |
||
127 | )); |
||
128 | } |
||
129 | |||
130 | $diffValues = array_udiff_uassoc( |
||
131 | $this->all(), |
||
132 | $collection->all(), |
||
133 | function ($a, $b) { |
||
134 | return $a <=> $b; |
||
135 | }, |
||
136 | function ($c, $d) { |
||
137 | return $c <=> $d; |
||
138 | } |
||
139 | ); |
||
140 | |||
141 | return new self($this->keyType, $this->valueType, $diffValues); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Return the type of the key |
||
146 | * @return mixed |
||
147 | */ |
||
148 | public function getKeyType(): mixed |
||
149 | { |
||
150 | return $this->keyType; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Return the type of the value |
||
155 | * @return mixed |
||
156 | */ |
||
157 | public function getValueType(): mixed |
||
158 | { |
||
159 | return $this->valueType; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * |
||
164 | * @param array<mixed, T> $data |
||
165 | * @return void |
||
166 | */ |
||
167 | public function fill(array $data): void |
||
168 | { |
||
169 | foreach ($data as $key => $value) { |
||
170 | $this->add($key, $value); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * |
||
176 | * @param callable $callback |
||
177 | * @return $this|null |
||
178 | */ |
||
179 | public function filter(callable $callback): ?self |
||
180 | { |
||
181 | $matches = []; |
||
182 | |||
183 | foreach ($this->data as $value) { |
||
184 | $val = call_user_func($callback, $value->getKey(), $value->getValue()); |
||
185 | if ($val === true) { |
||
186 | $matches[$value->getKey()] = $value->getValue(); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | return count($matches) > 0 |
||
191 | ? new $this($this->keyType, $this->valueType, $matches) |
||
192 | : null; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * |
||
197 | * @param callable $callback |
||
198 | * @return $this|null |
||
199 | */ |
||
200 | public function map(callable $callback): ?self |
||
201 | { |
||
202 | $matches = array_map($callback, $this->all()); |
||
203 | |||
204 | return count($matches) > 0 |
||
205 | ? new $this($this->keyType, $this->valueType, $this->all()) |
||
206 | : null; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * |
||
211 | * @param BaseCollection<T> $collection |
||
212 | * @return bool |
||
213 | * @throws InvalidOperationException |
||
214 | */ |
||
215 | public function equals(BaseCollection $collection): bool |
||
216 | { |
||
217 | if (!$collection instanceof self) { |
||
218 | throw new InvalidOperationException( |
||
219 | 'You should only compare an map against another map' |
||
220 | ); |
||
221 | } |
||
222 | |||
223 | return $this->all() == $collection->all(); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * |
||
228 | * @param callable $callback |
||
229 | * @return void |
||
230 | */ |
||
231 | public function forEach(callable $callback): void |
||
232 | { |
||
233 | $data = $this->all(); |
||
234 | array_walk($data, $callback); |
||
235 | |||
236 | $this->initializePairs($data); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Return the value for given key |
||
241 | * @param mixed $key |
||
242 | * @return T|null |
||
243 | */ |
||
244 | public function get(mixed $key): mixed |
||
245 | { |
||
246 | return $this->data->offsetExists($key) |
||
247 | ? $this->data->offsetGet($key)->getValue() |
||
248 | : null; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * {@inheritedoc} |
||
253 | * @param HashMap<T> $collection |
||
254 | * @return HashMap<T> |
||
255 | */ |
||
256 | public function merge(BaseCollection $collection): BaseCollection |
||
257 | { |
||
258 | TypeCheck::isEqual( |
||
259 | $this->getKeyType(), |
||
260 | $collection->getKeyType(), |
||
261 | sprintf( |
||
262 | 'The new map key should be of type %s', |
||
263 | $this->keyType |
||
264 | ) |
||
265 | ); |
||
266 | |||
267 | TypeCheck::isEqual( |
||
268 | $this->getValueType(), |
||
269 | $collection->getValueType(), |
||
270 | sprintf( |
||
271 | 'The new map value should be of type %s', |
||
272 | $this->valueType |
||
273 | ) |
||
274 | ); |
||
275 | |||
276 | return new $this( |
||
277 | $this->keyType, |
||
278 | $this->valueType, |
||
279 | array_merge($this->all(), $collection->all()) |
||
280 | ); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * {@inheritedoc} |
||
285 | */ |
||
286 | public function first(): mixed |
||
287 | { |
||
288 | throw new InvalidOperationException('Can not call this method in map'); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * {@inheritedoc} |
||
293 | */ |
||
294 | public function last(): mixed |
||
295 | { |
||
296 | throw new InvalidOperationException('Can not call this method in map'); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * {@inheritedoc} |
||
301 | */ |
||
302 | public function remove(mixed $key): void |
||
303 | { |
||
304 | if ($this->isEmpty()) { |
||
305 | throw new OutOfRangeException('The collection is empty'); |
||
306 | } |
||
307 | |||
308 | if (!$this->data->offsetExists($key)) { |
||
309 | throw new OutOfRangeException(sprintf( |
||
310 | 'The collection key [%s] does not exists', |
||
311 | $key |
||
312 | )); |
||
313 | } |
||
314 | |||
315 | $this->data->offsetUnset($key); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * |
||
320 | * @param int $offset |
||
321 | * @param int|null $length |
||
322 | * @return HashMap<T>|null |
||
323 | */ |
||
324 | public function slice(int $offset, ?int $length = null): ?BaseCollection |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * |
||
339 | * @param callable $callback |
||
340 | * @return HashMap<T>|null |
||
341 | */ |
||
342 | public function sort(callable $callback): ?BaseCollection |
||
343 | { |
||
344 | $data = $this->all(); |
||
345 | |||
346 | return uasort($data, $callback) |
||
347 | ? new $this( |
||
348 | $this->keyType, |
||
349 | $this->valueType, |
||
350 | $data |
||
351 | ) |
||
352 | : null; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * |
||
357 | * @param mixed $key |
||
358 | * @param T $value |
||
359 | * @return bool |
||
360 | * @throws OutOfRangeException |
||
361 | */ |
||
362 | public function update(mixed $key, mixed $value): bool |
||
363 | { |
||
364 | $this->validateEntry($key, $value); |
||
365 | |||
366 | if (!$this->data->offsetExists($key)) { |
||
367 | throw new OutOfRangeException(sprintf( |
||
368 | 'The collection key [%s] does not exists', |
||
369 | $key |
||
370 | )); |
||
371 | } |
||
372 | |||
373 | $this->data[$key]->setValue($value); |
||
374 | |||
375 | return $this->data[$key]->getValue() === $value; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * |
||
380 | * @return array<mixed, T> |
||
381 | */ |
||
382 | public function all(): array |
||
383 | { |
||
384 | $data = []; |
||
385 | foreach ($this->data as $pair) { |
||
386 | $data[$pair->getKey()] = $pair->getValue(); |
||
387 | } |
||
388 | |||
389 | return $data; |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * |
||
394 | * @return string |
||
395 | */ |
||
396 | public function toJson(): string |
||
397 | { |
||
398 | /* Thank to interface JsonSerializable */ |
||
399 | $json = json_encode($this); |
||
400 | return $json === false ? '' : $json; |
||
401 | } |
||
402 | |||
403 | |||
404 | /** |
||
405 | * Validate the type of key and value |
||
406 | * @param mixed $key |
||
407 | * @param mixed $value |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | protected function validateEntry(mixed $key, mixed $value): bool |
||
412 | { |
||
413 | TypeCheck::isValueOf( |
||
414 | $key, |
||
415 | $this->keyType, |
||
416 | sprintf( |
||
417 | 'The key type specified for this map is [%s], you cannot pass [%s]', |
||
418 | $this->keyType, |
||
419 | gettype($key) |
||
420 | ) |
||
421 | ); |
||
422 | |||
423 | TypeCheck::isValueOf( |
||
424 | $value, |
||
425 | $this->valueType, |
||
426 | sprintf( |
||
427 | 'The value type specified for this map is [%s], you cannot pass [%s]', |
||
428 | $this->valueType, |
||
429 | gettype($value) |
||
430 | ) |
||
431 | ); |
||
432 | |||
433 | return false; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Initialize the pair values |
||
438 | * @param array<mixed, mixed> $data |
||
439 | * @return void |
||
440 | */ |
||
441 | protected function initializePairs(array $data): void |
||
445 | } |
||
446 | } |
||
447 | } |
||
448 |
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