Total Complexity | 58 |
Total Lines | 504 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like LinkedList 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 LinkedList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class LinkedList implements IteratorAggregate, Countable |
||
15 | { |
||
16 | /** |
||
17 | * @var LinkedListItem|null first element of list |
||
18 | */ |
||
19 | protected ?LinkedListItem $first = null; |
||
20 | /** |
||
21 | * @var LinkedListItem|null last element of list |
||
22 | */ |
||
23 | protected ?LinkedListItem $last = null; |
||
24 | /** |
||
25 | * @var int List size |
||
26 | */ |
||
27 | protected int $length = 0; |
||
28 | |||
29 | /** |
||
30 | * Create new list by merging several another lists |
||
31 | * @param LinkedList ...$lists |
||
32 | * @return LinkedList |
||
33 | */ |
||
34 | public static function merge(LinkedList ...$lists): LinkedList |
||
35 | { |
||
36 | $result = new LinkedList(); |
||
37 | |||
38 | foreach($lists as $list) { |
||
39 | foreach($list as $value) { |
||
40 | $result->pushBack($value); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | return $result; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * LinkedList constructor. |
||
49 | * @param array $input default list content |
||
50 | */ |
||
51 | public function __construct(array $input = []) |
||
52 | { |
||
53 | foreach($input as $item) { |
||
54 | $this->pushBack($item); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Pushes element to front of list |
||
60 | * @param mixed $data data value of element |
||
61 | * @return LinkedListItem |
||
62 | */ |
||
63 | public function pushFront($data): LinkedListItem |
||
64 | { |
||
65 | return $this->pushAfter(null, $data); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Pushes element to back of list |
||
70 | * @param mixed $data data value of element |
||
71 | * @return LinkedListItem |
||
72 | */ |
||
73 | public function pushBack($data): LinkedListItem |
||
74 | { |
||
75 | return $this->pushBefore(null, $data); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Pushes new element to after target element position |
||
80 | * @param LinkedListItem|null $item target element position |
||
81 | * @param mixed $data data value of new element |
||
82 | * @return LinkedListItem |
||
83 | */ |
||
84 | public function pushAfter(?LinkedListItem $item, $data): LinkedListItem |
||
85 | { |
||
86 | $newItem = new LinkedListItem($data, null, null); |
||
87 | |||
88 | if($item === null) { |
||
89 | if($this->first !== null) { |
||
90 | return $this->pushBefore($this->first, $data); |
||
91 | } |
||
92 | $this->first = $newItem; |
||
93 | $this->last = $newItem; |
||
94 | } else { |
||
95 | $bufNext = $item->getNext(); |
||
96 | $item->setNext($newItem); |
||
97 | $newItem->setPrev($item); |
||
98 | $newItem->setNext($bufNext); |
||
99 | |||
100 | if($bufNext === null) { |
||
101 | $this->last = $newItem; |
||
102 | } else { |
||
103 | $bufNext->setPrev($newItem); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $this->length++; |
||
108 | |||
109 | return $newItem; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Pushes new element to before target element position |
||
114 | * @param LinkedListItem|null $item target element position |
||
115 | * @param mixed $data data value of new element |
||
116 | * @return LinkedListItem |
||
117 | */ |
||
118 | public function pushBefore(?LinkedListItem $item, $data): LinkedListItem |
||
119 | { |
||
120 | $newItem = new LinkedListItem($data, null, null); |
||
121 | |||
122 | if($item === null) { |
||
123 | if($this->last !== null) { |
||
124 | return $this->pushAfter($this->last, $data); |
||
125 | } |
||
126 | $this->first = $newItem; |
||
127 | $this->last = $newItem; |
||
128 | } else { |
||
129 | $bufPrev = $item->getPrev(); |
||
130 | $item->setPrev($newItem); |
||
131 | $newItem->setNext($item); |
||
132 | $newItem->setPrev($bufPrev); |
||
133 | |||
134 | if($bufPrev === null) { |
||
135 | $this->first = $newItem; |
||
136 | } else { |
||
137 | $bufPrev->setNext($newItem); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | $this->length++; |
||
142 | |||
143 | return $newItem; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Removes element from the front of list |
||
148 | * @return mixed data value of removed element |
||
149 | * @throws LinkedListException |
||
150 | */ |
||
151 | public function popFront() |
||
152 | { |
||
153 | if(!$this->length) { |
||
154 | throw new LinkedListException('empty', LinkedListException::STATUS_EMPTY); |
||
155 | } |
||
156 | return $this->popFrontPosition()->getData(); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Removes element from the back of list |
||
161 | * @return mixed data value of removed element |
||
162 | * @throws LinkedListException |
||
163 | */ |
||
164 | public function popBack() |
||
165 | { |
||
166 | return $this->popBackPosition()->getData(); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Removes element from the front of list |
||
171 | * @return LinkedListItem removed element position |
||
172 | * @throws LinkedListException |
||
173 | */ |
||
174 | public function popFrontPosition(): LinkedListItem |
||
175 | { |
||
176 | if(!$this->length) { |
||
177 | throw new LinkedListException('empty', LinkedListException::STATUS_EMPTY); |
||
178 | } |
||
179 | return $this->delete($this->first); |
||
|
|||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Removes element from the back of list |
||
184 | * @return LinkedListItem removed element position |
||
185 | * @throws LinkedListException |
||
186 | */ |
||
187 | public function popBackPosition(): LinkedListItem |
||
188 | { |
||
189 | if(!$this->length) { |
||
190 | throw new LinkedListException('empty', LinkedListException::STATUS_EMPTY); |
||
191 | } |
||
192 | return $this->delete($this->last); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Removes element from target element position |
||
197 | * @param LinkedListItem $item target element position |
||
198 | * @return LinkedListItem old position of element |
||
199 | */ |
||
200 | public function delete(LinkedListItem $item): LinkedListItem |
||
201 | { |
||
202 | $prev = $item->getPrev(); |
||
203 | $next = $item->getNext(); |
||
204 | |||
205 | if($prev !== null) { |
||
206 | $prev->setNext($next); |
||
207 | } else { |
||
208 | $this->first = $next; |
||
209 | } |
||
210 | |||
211 | if($next !== null) { |
||
212 | $next->setPrev($prev); |
||
213 | } else { |
||
214 | $this->last = $prev; |
||
215 | } |
||
216 | |||
217 | $this->length--; |
||
218 | |||
219 | return $item; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Swaps two elements |
||
224 | * @param LinkedListItem $lhs first element position |
||
225 | * @param LinkedListItem $rhs second element position |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function swap(LinkedListItem $lhs, LinkedListItem $rhs): self |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Clears list |
||
274 | * @return $this |
||
275 | */ |
||
276 | public function clear(): self |
||
277 | { |
||
278 | $this->first = null; |
||
279 | $this->last = null; |
||
280 | $this->length = 0; |
||
281 | return $this; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Sorts element via comparator callback |
||
286 | * @param callable $comparator comparator callback |
||
287 | * @return $this |
||
288 | * @throws Exception |
||
289 | */ |
||
290 | public function sort(callable $comparator): self |
||
291 | { |
||
292 | if($this->length <= 1) { |
||
293 | return $this; |
||
294 | } |
||
295 | |||
296 | do { |
||
297 | $flagStop = true; |
||
298 | $it = $this->getIterator(); |
||
299 | $it->rewind(); |
||
300 | for($i=0; $i<$this->length-1; $i++) { |
||
301 | $lhs = $it->current(); |
||
302 | /** @var LinkedListItem $lhsItem */ |
||
303 | $lhsItem = $it->key(); |
||
304 | |||
305 | $it->next(); |
||
306 | $rhs = $it->current(); |
||
307 | /** @var LinkedListItem $rhsItem */ |
||
308 | $rhsItem = $it->key(); |
||
309 | |||
310 | if($comparator($lhs, $rhs, $lhsItem, $rhsItem)) { |
||
311 | $this->swap($lhsItem, $rhsItem); |
||
312 | $flagStop = false; |
||
313 | } |
||
314 | } |
||
315 | } while(!$flagStop); |
||
316 | |||
317 | return $this; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Converts list to array |
||
322 | * @return array |
||
323 | */ |
||
324 | public function toArray(): array |
||
325 | { |
||
326 | $result = []; |
||
327 | foreach($this as $val) { |
||
328 | $result[] = $val; |
||
329 | } |
||
330 | |||
331 | return $result; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Returns first position of list |
||
336 | * @return LinkedListItem|null |
||
337 | */ |
||
338 | public function getFirst(): ?LinkedListItem |
||
339 | { |
||
340 | return $this->first; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Returns last position of list |
||
345 | * @return LinkedListItem|null |
||
346 | */ |
||
347 | public function getLast(): ?LinkedListItem |
||
348 | { |
||
349 | return $this->last; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Returns list iterator |
||
354 | * @return LinkedListIterator |
||
355 | */ |
||
356 | public function getIterator(): LinkedListIterator |
||
357 | { |
||
358 | return new LinkedListIterator($this); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Returns list size |
||
363 | * @return int |
||
364 | */ |
||
365 | public function count(): int |
||
366 | { |
||
367 | return $this->length; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Inserts element before specified position |
||
372 | * @param LinkedListItem|null $positionFor position to insert before |
||
373 | * @param LinkedListItem $element element to insert |
||
374 | * @return $this |
||
375 | */ |
||
376 | protected function setPrevFor(?LinkedListItem $positionFor, LinkedListItem $element): self |
||
377 | { |
||
378 | if($positionFor !== null) { |
||
379 | $positionFor->setPrev($element); |
||
380 | } else { |
||
381 | $this->last = $element; |
||
382 | } |
||
383 | |||
384 | return $this; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Inserts element after specified position |
||
389 | * @param LinkedListItem|null $positionFor position to insert after |
||
390 | * @param LinkedListItem $element element to insert |
||
391 | * @return $this |
||
392 | */ |
||
393 | protected function setNextFor(?LinkedListItem $positionFor, LinkedListItem $element): self |
||
394 | { |
||
395 | if($positionFor !== null) { |
||
396 | $positionFor->setNext($element); |
||
397 | } else { |
||
398 | $this->first = $element; |
||
399 | } |
||
400 | |||
401 | return $this; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Returns positions array |
||
406 | * @return LinkedListItem[] |
||
407 | */ |
||
408 | public function getPositionsArray(?callable $mapper = null): array |
||
409 | { |
||
410 | $result = []; |
||
411 | |||
412 | foreach($this as $pos => $val) { |
||
413 | if(is_callable($mapper)) { |
||
414 | $result[$mapper($pos)] = $pos; |
||
415 | } else { |
||
416 | $result[] = $pos; |
||
417 | } |
||
418 | } |
||
419 | |||
420 | return $result; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Returns index of position in list |
||
425 | * @param LinkedListItem $position position to get index |
||
426 | * @return int |
||
427 | */ |
||
428 | public function getIndexOf(LinkedListItem $position): int |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * Debug method to check integrity of list structure |
||
443 | * @return $this |
||
444 | * @throws LinkedListException |
||
445 | */ |
||
446 | public function checkIntegrity(): self |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Clones object |
||
502 | */ |
||
503 | public function __clone() |
||
518 | } |
||
519 | } |
||
520 | } |
||
521 |