Total Complexity | 47 |
Total Lines | 366 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Map 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 Map, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Mbh\Collection; |
||
28 | class Map implements ArrayAccess, CollectionInterface, IteratorAggregate |
||
29 | { |
||
30 | use Traits\Collection; |
||
31 | use Traits\Functional; |
||
32 | use Traits\SquaredCapacity; |
||
33 | |||
34 | const MIN_CAPACITY = 8.0; |
||
35 | |||
36 | /** |
||
37 | * @var FixedArray internal array to store pairs |
||
38 | */ |
||
39 | private $pairs; |
||
40 | |||
41 | /** |
||
42 | * Creates a new instance. |
||
43 | * |
||
44 | * @param array|Traversable $pairs |
||
45 | */ |
||
46 | public function __construct($pairs = []) |
||
47 | { |
||
48 | FixedArray::fromArray([]); |
||
49 | |||
50 | $this->putAll($pairs); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @inheritDoc |
||
55 | */ |
||
56 | public function clear() |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @inheritDoc |
||
64 | */ |
||
65 | public function count(): int |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Completely removes a pair from the internal array by position. It is |
||
72 | * important to remove it from the array and not just use 'unset'. |
||
73 | */ |
||
74 | private function delete(int $position) |
||
75 | { |
||
76 | $pair = $this->pairs->remove($position); |
||
77 | |||
78 | $this->checkCapacity(); |
||
79 | return $pair->value; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Return the first Pair from the Map |
||
84 | * |
||
85 | * @return Pair |
||
86 | * |
||
87 | * @throws UnderflowException |
||
88 | */ |
||
89 | public function first(): Pair |
||
90 | { |
||
91 | if ($this->isEmpty()) { |
||
92 | throw new UnderflowException(); |
||
93 | } |
||
94 | |||
95 | return $this->pairs->first(); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Returns the value associated with a key, or an optional default if the |
||
100 | * key is not associated with a value. |
||
101 | * |
||
102 | * @param mixed $key |
||
103 | * @param mixed $default |
||
104 | * |
||
105 | * @return mixed The associated value or fallback default if provided. |
||
106 | * |
||
107 | * @throws OutOfBoundsException if no default was provided and the key is |
||
108 | * not associated with a value. |
||
109 | */ |
||
110 | public function get($key, $default = null) |
||
111 | { |
||
112 | if (($pair = $this->lookupKey($key))) { |
||
113 | return $pair->value; |
||
114 | } |
||
115 | |||
116 | // Check if a default was provided. |
||
117 | if (func_num_args() === 1) { |
||
118 | throw new OutOfBoundsException(); |
||
119 | } |
||
120 | |||
121 | return $default; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Returns whether an association a given key exists. |
||
126 | * |
||
127 | * @param mixed $key |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function hasKey($key): bool |
||
132 | { |
||
133 | return $this->lookupKey($key) !== null; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Returns whether an association for a given value exists. |
||
138 | * |
||
139 | * @param mixed $value |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function hasValue($value): bool |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Returns a set of all the keys in the map. |
||
150 | * |
||
151 | * @return Set |
||
152 | */ |
||
153 | public function keys(): Set |
||
154 | { |
||
155 | return new Set($this->pairs->map(function($pair) { |
||
156 | return $pair->key; |
||
157 | })); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Determines whether two keys are equal. |
||
162 | * |
||
163 | * @param mixed $a |
||
164 | * @param mixed $b |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | private function keysAreEqual($a, $b): bool |
||
169 | { |
||
170 | if (is_object($a) && $a instanceof HashableInterface) { |
||
171 | return get_class($a) === get_class($b) && $a->equals($b); |
||
172 | } |
||
173 | |||
174 | return $a === $b; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Return the last Pair from the Map |
||
179 | * |
||
180 | * @return Pair |
||
181 | * |
||
182 | * @throws UnderflowException |
||
183 | */ |
||
184 | public function last(): Pair |
||
185 | { |
||
186 | if ($this->isEmpty()) { |
||
187 | throw new UnderflowException(); |
||
188 | } |
||
189 | |||
190 | return $this->pairs->last(); |
||
191 | } |
||
192 | |||
193 | |||
194 | /** |
||
195 | * Attempts to look up a key in the table. |
||
196 | * |
||
197 | * @param $key |
||
198 | * |
||
199 | * @return Pair|null |
||
200 | */ |
||
201 | private function lookupKey($key) |
||
202 | { |
||
203 | foreach ($this->pairs as $pair) { |
||
204 | if ($this->keysAreEqual($pair->key, $key)) { |
||
205 | return $pair; |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Attempts to look up a key in the table. |
||
212 | * |
||
213 | * @param $value |
||
214 | * |
||
215 | * @return Pair|null |
||
216 | */ |
||
217 | private function lookupValue($value) |
||
218 | { |
||
219 | foreach ($this->pairs as $pair) { |
||
220 | if ($pair->value === $value) { |
||
221 | return $pair; |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Returns a sequence of pairs representing all associations. |
||
228 | * |
||
229 | * @return SequenceableInterface |
||
230 | */ |
||
231 | public function pairs(): SequenceableInterface |
||
235 | }); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Associates a key with a value, replacing a previous association if there |
||
240 | * was one. |
||
241 | * |
||
242 | * @param mixed $key |
||
243 | * @param mixed $value |
||
244 | */ |
||
245 | public function put($key, $value) |
||
246 | { |
||
247 | $pair = $this->lookupKey($key); |
||
248 | if ($pair) { |
||
249 | $pair->value = $value; |
||
250 | } else { |
||
251 | $this->checkCapacity(); |
||
252 | $this->pairs[] = new Pair($key, $value); |
||
253 | } |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Creates associations for all keys and corresponding values of either an |
||
258 | * array or iterable object. |
||
259 | * |
||
260 | * @param Traversable|array $values |
||
261 | */ |
||
262 | public function putAll($values) |
||
263 | { |
||
264 | foreach ($values as $key => $value) { |
||
265 | $this->put($key, $value); |
||
266 | } |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Removes a key's association from the map and returns the associated value |
||
271 | * or a provided default if provided. |
||
272 | * |
||
273 | * @param mixed $key |
||
274 | * @param mixed $default |
||
275 | * |
||
276 | * @return mixed The associated value or fallback default if provided. |
||
277 | * |
||
278 | * @throws OutOfBoundsException if no default was provided and the key is |
||
279 | * not associated with a value. |
||
280 | */ |
||
281 | public function remove($key, $default = null) |
||
282 | { |
||
283 | foreach ($this->pairs as $position => $pair) { |
||
284 | if ($this->keysAreEqual($pair->key, $key)) { |
||
285 | return $this->delete($position); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | // Check if a default was provided |
||
290 | if (func_num_args() === 1) { |
||
291 | throw new OutOfBoundsException(); |
||
292 | } |
||
293 | |||
294 | return $default; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Return the pair at a specified position in the Map |
||
299 | * |
||
300 | * @param int $position |
||
301 | * |
||
302 | * @return Pair |
||
303 | * |
||
304 | * @throws OutOfRangeException |
||
305 | */ |
||
306 | public function skip(int $position): Pair |
||
307 | { |
||
308 | if ($position < 0 || $position >= count($this->pairs)) { |
||
309 | throw new OutOfRangeException(); |
||
310 | } |
||
311 | |||
312 | return $this->pairs[$position]->copy(); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @inheritDoc |
||
317 | */ |
||
318 | public function toArray(): array |
||
319 | { |
||
320 | $array = []; |
||
321 | foreach ($this->pairs as $pair) { |
||
322 | $array[$pair->key] = $pair->value; |
||
323 | } |
||
324 | |||
325 | return $array; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Returns a sequence of all the associated values in the Map. |
||
330 | * |
||
331 | * @return SequenceableInterface |
||
332 | */ |
||
333 | public function values(): SequenceableInterface |
||
334 | { |
||
335 | return $this->pairs->map(function($pair) { |
||
336 | return $pair->value; |
||
337 | }); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @inheritDoc |
||
342 | */ |
||
343 | public function getIterator() |
||
344 | { |
||
345 | foreach ($this->pairs as $pair) { |
||
346 | yield $pair->key => $pair->value; |
||
347 | } |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Returns a representation to be used for var_dump and print_r. |
||
352 | */ |
||
353 | public function __debugInfo() |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * @inheritdoc |
||
360 | */ |
||
361 | public function offsetSet($offset, $value) |
||
362 | { |
||
363 | $this->put($offset, $value); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * @inheritdoc |
||
368 | * |
||
369 | * @throws OutOfBoundsException |
||
370 | */ |
||
371 | public function &offsetGet($offset) |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * @inheritdoc |
||
382 | */ |
||
383 | public function offsetUnset($offset) |
||
384 | { |
||
385 | $this->remove($offset, null); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @inheritdoc |
||
390 | */ |
||
391 | public function offsetExists($offset) |
||
394 | } |
||
395 | } |
||
396 |