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 |
||
9 | class Collection implements ArrayAccess, Countable |
||
10 | { |
||
11 | /** |
||
12 | * The items contained in the collection. |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $items = []; |
||
17 | |||
18 | /** |
||
19 | * Create a new collection. |
||
20 | * |
||
21 | * @param array $items |
||
22 | */ |
||
23 | 198 | public function __construct($items = []) |
|
27 | |||
28 | /** |
||
29 | * Create a new collection instance if the value isn't one already. |
||
30 | * |
||
31 | * @param mixed $items |
||
32 | * @return static |
||
33 | */ |
||
34 | 33 | public static function make($items = []) |
|
38 | |||
39 | /** |
||
40 | * Count the number of items in the collection. |
||
41 | * |
||
42 | * @return int |
||
43 | */ |
||
44 | 21 | public function count() |
|
48 | |||
49 | /** |
||
50 | * Determine if an item exists at an offset. |
||
51 | * |
||
52 | * @param mixed $key |
||
53 | * @return bool |
||
54 | */ |
||
55 | 99 | public function offsetExists($key) |
|
59 | |||
60 | /** |
||
61 | * Get an item at a given offset. |
||
62 | * |
||
63 | * @param mixed $key |
||
64 | * @return mixed |
||
65 | */ |
||
66 | 9 | public function offsetGet($key) |
|
70 | |||
71 | /** |
||
72 | * Set the item at a given offset. |
||
73 | * |
||
74 | * @param mixed $key |
||
75 | * @param mixed $value |
||
76 | * @return void |
||
77 | */ |
||
78 | 6 | public function offsetSet($key, $value) |
|
86 | |||
87 | /** |
||
88 | * Unset the item at a given offset. |
||
89 | * |
||
90 | * @param string $key |
||
91 | * @return void |
||
92 | */ |
||
93 | 6 | public function offsetUnset($key) |
|
97 | |||
98 | /** |
||
99 | * Get all of the items in the collection. |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | 24 | public function all() |
|
107 | |||
108 | /** |
||
109 | * Change key of the collection to |
||
110 | * upper or lowecase. |
||
111 | * |
||
112 | * @param int $case |
||
113 | * @return static |
||
114 | */ |
||
115 | 6 | public function changeKeyCase($case = CASE_LOWER) |
|
119 | |||
120 | /** |
||
121 | * Determine if an item exists in the collection. |
||
122 | * |
||
123 | * @param mixed $key |
||
124 | * @return bool |
||
125 | */ |
||
126 | 99 | public function contains($key) |
|
135 | |||
136 | /** |
||
137 | * Execute a callback over each item. |
||
138 | * |
||
139 | * @param callable $callback |
||
140 | * @return $this |
||
141 | */ |
||
142 | 3 | public function each(callable $callback) |
|
152 | |||
153 | /** |
||
154 | * Run a filter over each of the items. |
||
155 | * |
||
156 | * @param callable|null $callback |
||
157 | * @return static |
||
158 | */ |
||
159 | 105 | public function filter(callable $callback = null) |
|
167 | |||
168 | /** |
||
169 | * Get the first item from the collection. |
||
170 | * |
||
171 | * @param callable|null $callback |
||
172 | * @param mixed $default |
||
173 | * @return mixed |
||
174 | */ |
||
175 | 30 | public function first(callable $callback = null, $default = null) |
|
194 | |||
195 | /** |
||
196 | * Get an item from the collection by key. |
||
197 | * |
||
198 | * @param mixed $key |
||
199 | * @param mixed $default |
||
200 | * @return mixed |
||
201 | */ |
||
202 | 87 | public function get($key, $default = null) |
|
210 | |||
211 | /** |
||
212 | * Determine if an item exists in the collection by key. |
||
213 | * |
||
214 | * @param mixed $key |
||
215 | * @return bool |
||
216 | */ |
||
217 | 30 | public function has($key) |
|
229 | |||
230 | /** |
||
231 | * Concatenate values of a given key as a string. |
||
232 | * |
||
233 | * @param string $seperator |
||
234 | * @return string |
||
235 | */ |
||
236 | 99 | public function implode($seperator) |
|
240 | |||
241 | /** |
||
242 | * Get the last item from the collection. |
||
243 | * |
||
244 | * @param mixed $default |
||
245 | * @return mixed |
||
246 | */ |
||
247 | 12 | public function last($default = null) |
|
251 | |||
252 | /** |
||
253 | * Run a map over each of the items. |
||
254 | * |
||
255 | * @param callable $callback |
||
256 | * @return static |
||
257 | */ |
||
258 | 21 | public function map(callable $callback) |
|
266 | |||
267 | /** |
||
268 | * Run an associative map over each of the items. |
||
269 | * |
||
270 | * The callback should return an associative array with a single key/value pair. |
||
271 | * |
||
272 | * @param callable $callback |
||
273 | * @return static |
||
274 | */ |
||
275 | 21 | public function mapWithKeys(callable $callback) |
|
288 | |||
289 | /** |
||
290 | * Create a collection of all elements that do not pass a given truth test. |
||
291 | * |
||
292 | * @param callable|mixed $callback |
||
293 | * @return static |
||
294 | */ |
||
295 | 99 | public function reject($callback) |
|
307 | |||
308 | /** |
||
309 | * Slice the underlying collection array. |
||
310 | * |
||
311 | * @param int $offset |
||
312 | * @param int $length |
||
313 | * @return static |
||
314 | */ |
||
315 | 120 | public function slice($offset, $length = null) |
|
319 | |||
320 | /** |
||
321 | * Take the first or last {$limit} items. |
||
322 | * |
||
323 | * @param int $limit |
||
324 | * @return static |
||
325 | */ |
||
326 | 102 | public function take($limit) |
|
334 | |||
335 | /** |
||
336 | * Get the collection of items as a plain array. |
||
337 | * |
||
338 | * @return array |
||
339 | */ |
||
340 | 33 | public function toArray() |
|
344 | |||
345 | /** |
||
346 | * Determine if the given value is callable, but not a string. |
||
347 | * |
||
348 | * @param mixed $value |
||
349 | * @return bool |
||
350 | */ |
||
351 | 102 | protected function useAsCallable($value) |
|
355 | |||
356 | /** |
||
357 | * Reset the keys on the underlying array. |
||
358 | * |
||
359 | * @return static |
||
360 | */ |
||
361 | 30 | public function values() |
|
365 | } |
||
366 |