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 |
||
16 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
17 | { |
||
18 | /** @var array */ |
||
19 | protected $items; |
||
20 | |||
21 | /** |
||
22 | * Collection constructor. |
||
23 | * |
||
24 | * @param array $items |
||
25 | */ |
||
26 | 43 | public function __construct(array $items = []) |
|
31 | |||
32 | /** |
||
33 | * Generate a collection from an array of items. |
||
34 | * I created this method so that it's possible to extend a collection more easily. |
||
35 | * |
||
36 | * @param array $items |
||
37 | * |
||
38 | * @return Collection |
||
39 | */ |
||
40 | 10 | public static function factory(array $items = []) |
|
44 | |||
45 | /** |
||
46 | * Get collection as an array |
||
47 | * |
||
48 | * @return array |
||
49 | */ |
||
50 | 16 | public function toArray() |
|
54 | |||
55 | /** |
||
56 | * Determine if collection has a given key |
||
57 | * |
||
58 | * @param mixed $key The key to look for |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | 26 | public function has($key) |
|
66 | |||
67 | /** |
||
68 | * Does collection have item at position? |
||
69 | * |
||
70 | * Determine if collection has an item at a particular position (indexed from one). |
||
71 | * Position can be positive and start from the beginning or it can be negative and |
||
72 | * start from the end. |
||
73 | * |
||
74 | * @param int $position |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | 2 | public function hasValueAt($position) |
|
87 | |||
88 | /** |
||
89 | * Get key at given position |
||
90 | * |
||
91 | * Returns the key at the given position, starting from one. Position can be positive (start from beginning) or |
||
92 | * negative (start from the end). |
||
93 | * |
||
94 | * If the position does not exist, a RuntimeException is thrown. |
||
95 | * |
||
96 | * @param int $position |
||
97 | * |
||
98 | * @return string |
||
99 | * |
||
100 | * @throws RuntimeException |
||
101 | */ |
||
102 | 6 | public function getKeyAt($position) |
|
116 | |||
117 | /** |
||
118 | * Get value at given position |
||
119 | * |
||
120 | * Returns the value at the given position, starting from one. Position can be positive (start from beginning) or |
||
121 | * negative (start from the end). |
||
122 | * |
||
123 | * If the position does not exist, a RuntimeException is thrown. |
||
124 | * |
||
125 | * @param int $position |
||
126 | * |
||
127 | * @return mixed |
||
128 | * |
||
129 | * @throws RuntimeException |
||
130 | */ |
||
131 | 2 | public function getValueAt($position) |
|
135 | |||
136 | /** |
||
137 | * Get the key of the first item found matching $item |
||
138 | * |
||
139 | * @param mixed $item |
||
140 | * |
||
141 | * @return mixed|null |
||
142 | */ |
||
143 | 1 | public function keyOf($item) |
|
153 | |||
154 | /** |
||
155 | * Get the offset (index) of the first item found that matches $item |
||
156 | * |
||
157 | * @param mixed $item |
||
158 | * |
||
159 | * @return int|null |
||
160 | */ |
||
161 | 1 | public function indexOf($item) |
|
173 | |||
174 | /** |
||
175 | * Get item by key, with an optional default return value |
||
176 | * |
||
177 | * @param mixed $key |
||
178 | * @param mixed $default |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | 8 | public function get($key, $default = null) |
|
190 | |||
191 | /** |
||
192 | * Add an item with no regard to key |
||
193 | * |
||
194 | * @param mixed $value |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | 3 | public function add($value) |
|
204 | |||
205 | /** |
||
206 | * Set an item at a given key |
||
207 | * |
||
208 | * @param mixed $key |
||
209 | * @param mixed $value |
||
210 | * @param bool $overwrite If false, do not overwrite existing key |
||
211 | * |
||
212 | * @return $this |
||
213 | */ |
||
214 | 9 | public function set($key, $value, $overwrite = true) |
|
222 | |||
223 | /** |
||
224 | * Delete an item by key |
||
225 | * |
||
226 | * @param mixed $key |
||
227 | * |
||
228 | * @return $this |
||
229 | */ |
||
230 | 3 | public function delete($key) |
|
236 | |||
237 | /** |
||
238 | * Clear the collection of all its items. |
||
239 | * |
||
240 | * @return $this |
||
241 | */ |
||
242 | 1 | public function clear() |
|
248 | |||
249 | /** |
||
250 | * Determine if collection contains given value |
||
251 | * |
||
252 | * @param mixed $val |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | 2 | public function contains($val) |
|
260 | |||
261 | /** |
||
262 | * Fetch item from collection by key and remove it from collection |
||
263 | * |
||
264 | * @param mixed $key |
||
265 | * |
||
266 | * @return mixed |
||
267 | */ |
||
268 | 1 | public function pull($key) |
|
276 | |||
277 | /** |
||
278 | * Join collection items using a delimiter |
||
279 | * |
||
280 | * @param string $delim |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | 1 | public function join($delim = '') |
|
288 | |||
289 | /** |
||
290 | * Determine if collection has any items |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | 3 | public function isEmpty() |
|
298 | |||
299 | /** |
||
300 | * Get a collection of only this collection's values (without its keys) |
||
301 | * |
||
302 | * @return Collection |
||
303 | */ |
||
304 | 1 | public function values() |
|
308 | |||
309 | /** |
||
310 | * Get a collection of only this collection's keys |
||
311 | * |
||
312 | * @return Collection |
||
313 | */ |
||
314 | 1 | public function keys() |
|
318 | |||
319 | /** |
||
320 | * Get a collection with order reversed |
||
321 | * |
||
322 | * @return Collection |
||
323 | */ |
||
324 | 3 | public function reverse() |
|
328 | |||
329 | /** |
||
330 | * Get a collection with keys and values flipped |
||
331 | * |
||
332 | * @return Collection |
||
333 | */ |
||
334 | 1 | public function flip() |
|
342 | |||
343 | /** |
||
344 | * Shuffle the order of this collection's values |
||
345 | * |
||
346 | * @return Collection |
||
347 | */ |
||
348 | 1 | public function shuffle() |
|
353 | |||
354 | /** |
||
355 | * Get a random value from the collection |
||
356 | * |
||
357 | * @return mixed |
||
358 | */ |
||
359 | 1 | public function random() |
|
363 | |||
364 | /** |
||
365 | * Sort the collection |
||
366 | * |
||
367 | * @param mixed $algo |
||
368 | * |
||
369 | * @return Collection |
||
370 | */ |
||
371 | public function sort($algo = null) |
||
375 | |||
376 | /** |
||
377 | * Get a new collection with only distinct values |
||
378 | * |
||
379 | * @return Collection |
||
380 | */ |
||
381 | 1 | public function distinct() |
|
391 | |||
392 | /** |
||
393 | * Remove all duplicate values from collection in-place |
||
394 | * |
||
395 | * @return Collection |
||
396 | */ |
||
397 | 1 | public function deduplicate() |
|
403 | |||
404 | /** |
||
405 | * Return a new collection with only filtered keys/values |
||
406 | * |
||
407 | * The callback accepts value, key, index and should return true if the item should be added to the returned |
||
408 | * collection |
||
409 | * |
||
410 | * @param callable $callback |
||
411 | * |
||
412 | * @return Collection |
||
413 | */ |
||
414 | 1 | public function filter(callable $callback) |
|
426 | |||
427 | /** |
||
428 | * Fold collection into a single value |
||
429 | * |
||
430 | * Loop through collection calling a callback function and passing the result to the next iteration, eventually |
||
431 | * returning a single value. |
||
432 | * |
||
433 | * @param callable $callback |
||
434 | * @param mixed $initial |
||
435 | * |
||
436 | * @return null |
||
437 | */ |
||
438 | 1 | public function fold(callable $callback, $initial = null) |
|
447 | |||
448 | /** |
||
449 | * Return a merge of this collection and $items |
||
450 | * |
||
451 | * @param array|Traversable $items |
||
452 | * |
||
453 | * @return Collection |
||
454 | */ |
||
455 | 3 | public function merge($items) |
|
467 | |||
468 | /** |
||
469 | * Create a new collection with a union of this collection and $items |
||
470 | * |
||
471 | * This method is similar to merge, except that existing items will not be overwritten. |
||
472 | * |
||
473 | * @param $items |
||
474 | */ |
||
475 | 2 | public function union($items) |
|
487 | |||
488 | /** ++++ ++++ **/ |
||
489 | /** ++ Interface Compliance ++ **/ |
||
490 | /** ++++ ++++ **/ |
||
491 | |||
492 | /** |
||
493 | * @return array |
||
494 | */ |
||
495 | 1 | public function jsonSerialize() |
|
499 | |||
500 | /** ++++ ++++ **/ |
||
501 | /** ++ Array Access Methods ++ **/ |
||
502 | /** ++++ ++++ **/ |
||
503 | |||
504 | /** |
||
505 | * {@inheritDoc} |
||
506 | */ |
||
507 | 1 | public function offsetExists($offset) |
|
511 | |||
512 | /** |
||
513 | * {@inheritDoc} |
||
514 | */ |
||
515 | 2 | public function offsetGet($offset) |
|
523 | |||
524 | /** |
||
525 | * {@inheritDoc} |
||
526 | */ |
||
527 | 1 | public function offsetUnset($offset) |
|
531 | |||
532 | /** |
||
533 | * {@inheritDoc} |
||
534 | */ |
||
535 | 1 | public function offsetSet($offset, $value) |
|
543 | |||
544 | /** ++++ ++++ **/ |
||
545 | /** ++ Iterator Methods ++ **/ |
||
546 | /** ++++ ++++ **/ |
||
547 | |||
548 | /** |
||
549 | * {@inheritDoc} |
||
550 | */ |
||
551 | 15 | public function current() |
|
555 | |||
556 | /** |
||
557 | * {@inheritDoc} |
||
558 | */ |
||
559 | 15 | public function key() |
|
563 | |||
564 | /** |
||
565 | * {@inheritDoc} |
||
566 | */ |
||
567 | 15 | public function next() |
|
571 | |||
572 | /** |
||
573 | * {@inheritDoc} |
||
574 | */ |
||
575 | 43 | public function rewind() |
|
579 | |||
580 | /** |
||
581 | * {@inheritDoc} |
||
582 | */ |
||
583 | 15 | public function valid() |
|
587 | |||
588 | /** ++++ ++++ **/ |
||
589 | /** ++ Countable Method ++ **/ |
||
590 | /** ++++ ++++ **/ |
||
591 | |||
592 | /** |
||
593 | * {@inheritDoc} |
||
594 | */ |
||
595 | 5 | public function count() |
|
599 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.