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 ArrayAccess, Iterator, Countable, JsonSerializable |
||
14 | { |
||
15 | /** @var array */ |
||
16 | protected $items; |
||
17 | |||
18 | /** |
||
19 | * Collection constructor. |
||
20 | * |
||
21 | * @param array $items |
||
22 | */ |
||
23 | 31 | public function __construct(array $items = []) |
|
28 | |||
29 | /** |
||
30 | * Generate a collection from an array of items. |
||
31 | * I created this method so that it's possible to extend a collection more easily. |
||
32 | * |
||
33 | * @param array $items |
||
34 | * |
||
35 | * @return Collection |
||
36 | */ |
||
37 | 8 | public static function factory(array $items = []) |
|
41 | |||
42 | /** |
||
43 | * Get collection as an array |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | 10 | public function toArray() |
|
51 | |||
52 | /** |
||
53 | * Determine if collection has a given key |
||
54 | * |
||
55 | * @param mixed $key The key to look for |
||
56 | * |
||
57 | * @return bool |
||
58 | */ |
||
59 | 18 | public function has($key) |
|
63 | |||
64 | /** |
||
65 | * Does collection have item at position? |
||
66 | * |
||
67 | * Determine if collection has an item at a particular position (indexed from one). |
||
68 | * Position can be positive and start from the beginning or it can be negative and |
||
69 | * start from the end. |
||
70 | * |
||
71 | * @param int $position |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | 2 | public function hasValueAt($position) |
|
84 | |||
85 | /** |
||
86 | * Get key at given position |
||
87 | * |
||
88 | * Returns the key at the given position, starting from one. Position can be positive (start from beginning) or |
||
89 | * negative (start from the end). |
||
90 | * |
||
91 | * If the position does not exist, a RuntimeException is thrown. |
||
92 | * |
||
93 | * @param int $position |
||
94 | * |
||
95 | * @return string |
||
96 | * |
||
97 | * @throws RuntimeException |
||
98 | */ |
||
99 | 6 | public function getKeyAt($position) |
|
113 | |||
114 | /** |
||
115 | * Get value at given position |
||
116 | * |
||
117 | * Returns the value at the given position, starting from one. Position can be positive (start from beginning) or |
||
118 | * negative (start from the end). |
||
119 | * |
||
120 | * If the position does not exist, a RuntimeException is thrown. |
||
121 | * |
||
122 | * @param int $position |
||
123 | * |
||
124 | * @return mixed |
||
125 | * |
||
126 | * @throws RuntimeException |
||
127 | */ |
||
128 | 2 | public function getValueAt($position) |
|
132 | |||
133 | /** |
||
134 | * Get item by key, with an optional default return value |
||
135 | * |
||
136 | * @param mixed $key |
||
137 | * @param mixed $default |
||
138 | * |
||
139 | * @return mixed |
||
140 | */ |
||
141 | 7 | public function get($key, $default = null) |
|
149 | |||
150 | /** |
||
151 | * Add an item with no regard to key |
||
152 | * |
||
153 | * @param mixed $value |
||
154 | * |
||
155 | * @return $this |
||
156 | */ |
||
157 | 3 | public function add($value) |
|
163 | |||
164 | /** |
||
165 | * Set an item at a given key |
||
166 | * |
||
167 | * @param mixed $key |
||
168 | * @param mixed $value |
||
169 | * |
||
170 | * @return $this |
||
171 | */ |
||
172 | 3 | public function set($key, $value) |
|
178 | |||
179 | /** |
||
180 | * Delete an item by key |
||
181 | * |
||
182 | * @param mixed $key |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | 3 | public function delete($key) |
|
192 | |||
193 | /** |
||
194 | * Clear the collection of all its items. |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | 1 | public function clear() |
|
204 | |||
205 | /** |
||
206 | * Determine if collection contains given value |
||
207 | * |
||
208 | * @param mixed $val |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | 1 | public function contains($val) |
|
216 | |||
217 | /** |
||
218 | * Fetch item from collection by key and remove it from collection |
||
219 | * |
||
220 | * @param mixed $key |
||
221 | * |
||
222 | * @return mixed |
||
223 | */ |
||
224 | 1 | public function pull($key) |
|
232 | |||
233 | /** |
||
234 | * Join collection items using a delimiter |
||
235 | * |
||
236 | * @param string $delim |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 1 | public function join($delim = '') |
|
244 | |||
245 | /** |
||
246 | * Determine if collection has any items |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | 3 | public function isEmpty() |
|
254 | |||
255 | /** |
||
256 | * Get a collection of only this collection's values (without its keys) |
||
257 | * |
||
258 | * @return Collection |
||
259 | */ |
||
260 | 1 | public function values() |
|
264 | |||
265 | /** |
||
266 | * Get a collection of only this collection's keys |
||
267 | * |
||
268 | * @return Collection |
||
269 | */ |
||
270 | 1 | public function keys() |
|
274 | |||
275 | /** |
||
276 | * Get a collection with order reversed |
||
277 | * |
||
278 | * @return Collection |
||
279 | */ |
||
280 | 3 | public function reverse() |
|
284 | |||
285 | /** |
||
286 | * Get a collection with keys and values flipped |
||
287 | * |
||
288 | * @return Collection |
||
289 | */ |
||
290 | 1 | public function flip() |
|
298 | |||
299 | /** |
||
300 | * Shuffle the order of this collection's values |
||
301 | * |
||
302 | * @return Collection |
||
303 | */ |
||
304 | 1 | public function shuffle() |
|
309 | |||
310 | /** |
||
311 | * Get a random value from the collection |
||
312 | * |
||
313 | * @return mixed |
||
314 | */ |
||
315 | 1 | public function random() |
|
319 | |||
320 | /** |
||
321 | * Sort the collection |
||
322 | * |
||
323 | * @param mixed $algo |
||
324 | * |
||
325 | * @return Collection |
||
326 | */ |
||
327 | public function sort($algo = null) |
||
331 | |||
332 | /** |
||
333 | * Get a new collection with only distinct values |
||
334 | * |
||
335 | * @return Collection |
||
336 | */ |
||
337 | public function distict() |
||
341 | |||
342 | /** |
||
343 | * Remove all duplicate values from collection in-place |
||
344 | * |
||
345 | * @return Collection |
||
346 | */ |
||
347 | public function deduplicate() |
||
351 | |||
352 | /** ++++ ++++ **/ |
||
353 | /** ++ Interface Compliance ++ **/ |
||
354 | /** ++++ ++++ **/ |
||
355 | |||
356 | /** |
||
357 | * @return array |
||
358 | */ |
||
359 | 1 | public function jsonSerialize() |
|
363 | |||
364 | /** ++++ ++++ **/ |
||
365 | /** ++ Array Access Methods ++ **/ |
||
366 | /** ++++ ++++ **/ |
||
367 | |||
368 | /** |
||
369 | * {@inheritDoc} |
||
370 | */ |
||
371 | 1 | public function offsetExists($offset) |
|
375 | |||
376 | /** |
||
377 | * {@inheritDoc} |
||
378 | */ |
||
379 | 2 | public function offsetGet($offset) |
|
387 | |||
388 | /** |
||
389 | * {@inheritDoc} |
||
390 | */ |
||
391 | 1 | public function offsetUnset($offset) |
|
395 | |||
396 | /** |
||
397 | * {@inheritDoc} |
||
398 | */ |
||
399 | 1 | public function offsetSet($offset, $value) |
|
407 | |||
408 | /** ++++ ++++ **/ |
||
409 | /** ++ Iterator Methods ++ **/ |
||
410 | /** ++++ ++++ **/ |
||
411 | |||
412 | /** |
||
413 | * {@inheritDoc} |
||
414 | */ |
||
415 | 8 | public function current() |
|
419 | |||
420 | /** |
||
421 | * {@inheritDoc} |
||
422 | */ |
||
423 | 8 | public function key() |
|
427 | |||
428 | /** |
||
429 | * {@inheritDoc} |
||
430 | */ |
||
431 | 8 | public function next() |
|
435 | |||
436 | /** |
||
437 | * {@inheritDoc} |
||
438 | */ |
||
439 | 31 | public function rewind() |
|
443 | |||
444 | /** |
||
445 | * {@inheritDoc} |
||
446 | */ |
||
447 | 8 | public function valid() |
|
451 | |||
452 | /** ++++ ++++ **/ |
||
453 | /** ++ Countable Method ++ **/ |
||
454 | /** ++++ ++++ **/ |
||
455 | |||
456 | /** |
||
457 | * {@inheritDoc} |
||
458 | */ |
||
459 | 5 | public function count() |
|
463 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.