Complex classes like Collection 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Collection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Collection implements \Iterator, \ArrayAccess, \Countable { |
||
14 | |||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | protected $position = 0; |
||
19 | |||
20 | /** |
||
21 | * Array of objects |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $items = []; |
||
26 | |||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $initialContentHash; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * @param array $items |
||
36 | */ |
||
37 | 450 | public function __construct(array $items = []) { |
|
43 | |||
44 | |||
45 | /** |
||
46 | * |
||
47 | */ |
||
48 | 144 | public function __clone() { |
|
55 | |||
56 | |||
57 | /** |
||
58 | * Extract each value from token |
||
59 | 435 | * |
|
60 | 435 | * @return string |
|
61 | 435 | */ |
|
62 | public function __toString() { |
||
65 | |||
66 | |||
67 | /** |
||
68 | 9 | * @codeCoverageIgnore |
|
69 | 9 | * @deprecated |
|
70 | * @see createFromString |
||
71 | * @param string $string |
||
72 | * @return Collection |
||
73 | */ |
||
74 | public static function initFromString($string) { |
||
78 | |||
79 | |||
80 | /** |
||
81 | * |
||
82 | * @param string $string |
||
83 | * @return Collection |
||
84 | * @throws Exception |
||
85 | */ |
||
86 | 441 | public static function createFromString($string) : Collection { |
|
90 | |||
91 | |||
92 | /** |
||
93 | * Return number of items in this collection |
||
94 | 450 | * |
|
95 | 450 | * @return int |
|
96 | */ |
||
97 | 450 | public function count() { |
|
100 | |||
101 | 435 | ||
102 | 300 | /** |
|
103 | * Add one item to begin of collection |
||
104 | 450 | * This item is accessible via `$collection->getFirst();` |
|
105 | * |
||
106 | * @param $item |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function prepend(Token $item) : self { |
||
113 | |||
114 | 9 | ||
115 | 9 | /** |
|
116 | 9 | * Add one item to the end of collection |
|
117 | * This item is accessible via `$collection->getLast();` |
||
118 | 9 | * |
|
119 | 9 | * @param $item |
|
120 | * @return $this |
||
121 | 9 | */ |
|
122 | 9 | public function append(Token $item) : self { |
|
126 | |||
127 | |||
128 | /** |
||
129 | * @param int $index |
||
130 | 3 | * @param array $items |
|
131 | 3 | * @return $this |
|
132 | 3 | * @throws \InvalidArgumentException |
|
133 | 3 | */ |
|
134 | 2 | public function addAfter($index, $items) : self { |
|
155 | |||
156 | |||
157 | /** |
||
158 | * Truncate current list of items and add new |
||
159 | * |
||
160 | * @param array $items |
||
161 | * @return $this |
||
162 | */ |
||
163 | public function setItems(array $items) : self { |
||
174 | |||
175 | 3 | ||
176 | 6 | /** |
|
177 | * Remove part of items from collection |
||
178 | 9 | * Works as array_slice |
|
179 | * |
||
180 | * |
||
181 | * @param int $offset |
||
182 | * @param int|null $length |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function slice(int $offset, int $length = null) : self { |
||
189 | 30 | ||
190 | 30 | ||
191 | 20 | /** |
|
192 | 30 | * Take part of items and return new collection |
|
193 | * Works as array_slice |
||
194 | * At this point items in 2 collection is same |
||
195 | * |
||
196 | * @param int $offset |
||
197 | * @param null $length |
||
198 | * @return Collection |
||
199 | */ |
||
200 | public function extractItems(int $offset, $length = null) : Collection { |
||
204 | 177 | ||
205 | 177 | ||
206 | /** |
||
207 | 177 | * Rewind current collection |
|
208 | 177 | */ |
|
209 | 177 | public function rewind() { |
|
213 | |||
214 | 177 | ||
215 | /** |
||
216 | * Return last item from collection |
||
217 | * |
||
218 | * @return Token|false |
||
219 | */ |
||
220 | public function getLast() { |
||
223 | 15 | ||
224 | 15 | ||
225 | /** |
||
226 | * Return first item from collection |
||
227 | * @return Token|false |
||
228 | */ |
||
229 | public function getFirst() { |
||
232 | 450 | ||
233 | 450 | ||
234 | /** |
||
235 | * Return next item from current |
||
236 | * Also can return item with position from current + $step |
||
237 | * |
||
238 | * @param int $step |
||
239 | * @return Token |
||
240 | */ |
||
241 | public function getNext(int $step = 1) : Token { |
||
245 | |||
246 | |||
247 | /** |
||
248 | * Return previous item |
||
249 | * Also can return previous from current position + $step |
||
250 | * |
||
251 | * @param int $step |
||
252 | * @return Token |
||
253 | */ |
||
254 | public function getPrevious(int $step = 1) : Token { |
||
258 | |||
259 | |||
260 | /** |
||
261 | * Return current item in collection |
||
262 | * |
||
263 | * @return Token|null |
||
264 | */ |
||
265 | public function current() { |
||
268 | |||
269 | |||
270 | /** |
||
271 | * Return current position |
||
272 | * |
||
273 | * @return int |
||
274 | */ |
||
275 | public function key() { |
||
278 | |||
279 | |||
280 | /** |
||
281 | * Switch to next position |
||
282 | */ |
||
283 | public function next() { |
||
286 | |||
287 | |||
288 | /** |
||
289 | * Check if item exist in current position |
||
290 | * |
||
291 | * @return bool |
||
292 | */ |
||
293 | public function valid() : bool { |
||
296 | |||
297 | |||
298 | /** |
||
299 | * Add item to the end or modify item with given key |
||
300 | * |
||
301 | * @param int|null $offset |
||
302 | * @param Token $item |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function offsetSet($offset, $item) { |
||
322 | |||
323 | |||
324 | /** |
||
325 | * Check if item with given offset exists |
||
326 | * |
||
327 | * @param int $offset |
||
328 | * @return bool |
||
329 | */ |
||
330 | public function offsetExists($offset) { |
||
333 | |||
334 | |||
335 | /** |
||
336 | * Remove item from collection |
||
337 | * |
||
338 | * @param int $offset |
||
339 | */ |
||
340 | public function offsetUnset($offset) { |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Get item from collection |
||
347 | * |
||
348 | * @param int $offset |
||
349 | * @return Token|null |
||
350 | */ |
||
351 | public function offsetGet($offset) { |
||
354 | |||
355 | |||
356 | /** |
||
357 | * Return array of items connected to this collection |
||
358 | * |
||
359 | * Rewrite this method in you class |
||
360 | * |
||
361 | * <code> |
||
362 | * foreach($collection->getTokens() as $item){ |
||
363 | * echo get_class($item)."\n; |
||
364 | * } |
||
365 | * </code> |
||
366 | * @return Token[] |
||
367 | */ |
||
368 | public function getTokens() : array { |
||
371 | |||
372 | |||
373 | public function getItems() { |
||
377 | |||
378 | |||
379 | /** |
||
380 | * Iterate over objects in collection |
||
381 | * |
||
382 | * <code> |
||
383 | * $collection->each(function($item, $index, $collection){ |
||
384 | * if ( $index > 0 ) { |
||
385 | * $item->remove(); |
||
386 | * } |
||
387 | * }) |
||
388 | * </code> |
||
389 | * |
||
390 | * @param callable $callback |
||
391 | * @return $this |
||
392 | * @throws \InvalidArgumentException |
||
393 | */ |
||
394 | public function each(callable $callback) : self { |
||
408 | |||
409 | |||
410 | public function map(callable $callback) { |
||
414 | |||
415 | |||
416 | /** |
||
417 | * Remove all tokens in collection |
||
418 | * |
||
419 | * @return $this |
||
420 | */ |
||
421 | public function remove() : self { |
||
427 | |||
428 | |||
429 | /** |
||
430 | * @param Query $query |
||
431 | * @return Collection |
||
432 | */ |
||
433 | public function find(Query $query) { |
||
437 | |||
438 | |||
439 | /** |
||
440 | * Remove all invalid tokens in collection |
||
441 | * Refresh index. |
||
442 | * |
||
443 | * @return Collection |
||
444 | */ |
||
445 | public function refresh() : self { |
||
455 | |||
456 | |||
457 | /** |
||
458 | * @param Token $tokenStart |
||
459 | * @param Token $tokenEnd |
||
460 | * @return Collection |
||
461 | */ |
||
462 | public function extractByTokens(Token $tokenStart, Token $tokenEnd) : Collection { |
||
477 | |||
478 | |||
479 | /** |
||
480 | * @return $this |
||
481 | */ |
||
482 | public function storeContentHash() : self { |
||
486 | |||
487 | |||
488 | /** |
||
489 | * @return bool |
||
490 | */ |
||
491 | public function isChanged() : bool { |
||
494 | |||
495 | |||
496 | /** |
||
497 | * @return string |
||
498 | */ |
||
499 | private function getContentHash() : string { |
||
502 | |||
503 | |||
504 | /** |
||
505 | * @return string |
||
506 | */ |
||
507 | public function assemble() : string { |
||
519 | |||
520 | |||
521 | /** |
||
522 | * Remove invalid tokens from collection |
||
523 | * |
||
524 | * @return $this |
||
525 | */ |
||
526 | protected function cleanCollection() : self { |
||
536 | |||
537 | } |