Complex classes like BaseCollection 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 BaseCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class BaseCollection implements \Iterator, \ArrayAccess, \Countable { |
||
12 | |||
13 | /** |
||
14 | * @var int |
||
15 | */ |
||
16 | protected $position = 0; |
||
17 | |||
18 | /** |
||
19 | * Array of objects |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $items = []; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * @param array $items |
||
28 | */ |
||
29 | public function __construct(array $items = []) { |
||
36 | |||
37 | |||
38 | /** |
||
39 | * |
||
40 | */ |
||
41 | public function __clone() { |
||
48 | |||
49 | |||
50 | /** |
||
51 | * Return number of items in this collection |
||
52 | * |
||
53 | * @return int |
||
54 | */ |
||
55 | public function count() { |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Add one item to begin of collection |
||
62 | * This item is accessible via `$collection->getFirst();` |
||
63 | * |
||
64 | * @param $item |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function prepend($item) { |
||
72 | |||
73 | |||
74 | /** |
||
75 | * Add one item to the end of collection |
||
76 | * This item is accessible via `$collection->getLast();` |
||
77 | * |
||
78 | * @param $item |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function append($item) { |
||
87 | |||
88 | |||
89 | /** |
||
90 | * @param int $index |
||
91 | * @param array $items |
||
92 | * @return $this |
||
93 | * @throws \InvalidArgumentException |
||
94 | */ |
||
95 | public function addAfter($index, $items) { |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Truncate current list of items and add new |
||
116 | * |
||
117 | * @param array $items |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function setItems($items) { |
||
133 | |||
134 | |||
135 | /** |
||
136 | * Remove part of items from collection |
||
137 | * Works as array_slice |
||
138 | * |
||
139 | * |
||
140 | * @param $offset |
||
141 | * @param null $length |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function slice($offset, $length = null) { |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Take part of items and return new collection |
||
152 | * Works as array_slice |
||
153 | * At this point items in 2 collection is same |
||
154 | * |
||
155 | * @param int $offset |
||
156 | * @param null $length |
||
157 | * @return self |
||
158 | */ |
||
159 | public function extractItems($offset, $length = null) { |
||
167 | |||
168 | |||
169 | /** |
||
170 | * Rewind current collection |
||
171 | */ |
||
172 | public function rewind() { |
||
176 | |||
177 | |||
178 | /** |
||
179 | * Return last item from collection |
||
180 | * |
||
181 | * @return mixed |
||
182 | */ |
||
183 | public function getLast() { |
||
186 | |||
187 | |||
188 | /** |
||
189 | * Return first item from collection |
||
190 | * @return mixed |
||
191 | */ |
||
192 | public function getFirst() { |
||
195 | |||
196 | |||
197 | /** |
||
198 | * Return next item from current |
||
199 | * Also can return item with position from current + $step |
||
200 | * |
||
201 | * @param int $step |
||
202 | * @return mixed |
||
203 | */ |
||
204 | public function getNext($step = 1) { |
||
208 | |||
209 | |||
210 | /** |
||
211 | * Return previous item |
||
212 | * Also can return previous from current position + $step |
||
213 | * |
||
214 | * @param int $step |
||
215 | * @return mixed |
||
216 | */ |
||
217 | public function getPrevious($step = 1) { |
||
221 | |||
222 | |||
223 | /** |
||
224 | * Return current item in collection |
||
225 | * |
||
226 | * @return object |
||
227 | */ |
||
228 | public function current() { |
||
231 | |||
232 | |||
233 | /** |
||
234 | * Return current position |
||
235 | * |
||
236 | * @return int |
||
237 | */ |
||
238 | public function key() { |
||
241 | |||
242 | |||
243 | /** |
||
244 | * Switch to next position |
||
245 | */ |
||
246 | public function next() { |
||
249 | |||
250 | |||
251 | /** |
||
252 | * Check if item exist in current position |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | public function valid() { |
||
259 | |||
260 | |||
261 | /** |
||
262 | * Add item to the end or modify item with given key |
||
263 | * |
||
264 | * @param int|null $offset |
||
265 | * @param object $item |
||
266 | * @return $this |
||
267 | */ |
||
268 | public function offsetSet($offset, $item) { |
||
280 | |||
281 | |||
282 | /** |
||
283 | * Check if item with given offset exists |
||
284 | * |
||
285 | * @param mixed $offset |
||
286 | * @return bool |
||
287 | */ |
||
288 | public function offsetExists($offset) { |
||
291 | |||
292 | |||
293 | /** |
||
294 | * Remove item from collection |
||
295 | * |
||
296 | * @param int $offset |
||
297 | */ |
||
298 | public function offsetUnset($offset) { |
||
301 | |||
302 | |||
303 | /** |
||
304 | * Get item from collection |
||
305 | * |
||
306 | * @param int $offset |
||
307 | * @return object |
||
308 | */ |
||
309 | public function offsetGet($offset) { |
||
312 | |||
313 | |||
314 | /** |
||
315 | * Return array of items connected to this collection |
||
316 | * |
||
317 | * Rewrite this method in you class |
||
318 | * |
||
319 | * <code> |
||
320 | * foreach($collection->getItems() as $item){ |
||
321 | * echo get_class($item)."\n; |
||
322 | * } |
||
323 | * </code> |
||
324 | * @return object[] |
||
325 | */ |
||
326 | public function getItems() { |
||
329 | |||
330 | |||
331 | /** |
||
332 | * Iterate over objects in collection |
||
333 | * |
||
334 | * <code> |
||
335 | * $collection->map(function($item, $index, $collection){ |
||
336 | * if ( $index > 0 ) { |
||
337 | * $item->remove(); |
||
338 | * } |
||
339 | * }) |
||
340 | * </code> |
||
341 | * |
||
342 | * @param callback $callback |
||
343 | * @return $this |
||
344 | * @throws \InvalidArgumentException |
||
345 | */ |
||
346 | public function map($callback) { |
||
360 | |||
361 | |||
362 | /** |
||
363 | * @param int $index |
||
364 | */ |
||
365 | protected function validateIndex($index) { |
||
370 | |||
371 | |||
372 | public function validateType($item) { |
||
377 | |||
378 | } |