Complex classes like AbstractCollection 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 AbstractCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | abstract class AbstractCollection implements Collection { |
||
12 | |||
13 | protected $collection = []; |
||
14 | |||
15 | public function contains($element) { |
||
18 | |||
19 | public function size() { |
||
22 | |||
23 | public function isEmpty() { |
||
26 | |||
27 | public function clear() { |
||
30 | |||
31 | public function toArray() { |
||
34 | |||
35 | /** |
||
36 | * Applies the callback to the elements |
||
37 | * |
||
38 | * @param callable $callback the applied callback function |
||
39 | * @return static |
||
40 | */ |
||
41 | public function map(callable $callback) { |
||
44 | |||
45 | /** |
||
46 | * Filters elements using a callback function |
||
47 | * |
||
48 | * @param callable $callback the filter function |
||
49 | * @return static |
||
50 | */ |
||
51 | public function filter(callable $callback) { |
||
54 | |||
55 | /** |
||
56 | * Tests whether all elements in the collection pass the test implemented by the provided function. |
||
57 | * |
||
58 | * Returns <code>true</code> for an empty collection. |
||
59 | * |
||
60 | * @param callable $callback |
||
61 | * @return boolean |
||
62 | */ |
||
63 | public function every(callable $callback) { |
||
71 | |||
72 | /** |
||
73 | * Tests whether at least one element in the collection passes the test implemented by the provided function. |
||
74 | * |
||
75 | * Returns <code>false</code> for an empty collection. |
||
76 | * |
||
77 | * @param callable $callback |
||
78 | * @return boolean |
||
79 | */ |
||
80 | public function some(callable $callback) { |
||
88 | |||
89 | /** |
||
90 | * Searches the collection for query using the callback function on each element |
||
91 | * |
||
92 | * The callback function takes one or two parameters: |
||
93 | * |
||
94 | * function ($element [, $query]) {} |
||
95 | * |
||
96 | * The callback must return a boolean |
||
97 | * |
||
98 | * @param mixed $query (optional) |
||
99 | * @param callable $callback |
||
100 | * @return boolean |
||
101 | */ |
||
102 | public function search() { |
||
119 | |||
120 | /** |
||
121 | * Searches the collection with a given callback and returns the first element if found. |
||
122 | * |
||
123 | * The callback function takes one or two parameters: |
||
124 | * |
||
125 | * function ($element [, $query]) {} |
||
126 | * |
||
127 | * The callback must return a boolean |
||
128 | * |
||
129 | * @param mixed $query OPTIONAL the provided query |
||
130 | * @param callable $callback the callback function |
||
131 | * @return mixed|null the found element or null if it hasn't been found |
||
132 | */ |
||
133 | public function find() { |
||
151 | |||
152 | /** |
||
153 | * Searches the collection with a given callback and returns the last element if found. |
||
154 | * |
||
155 | * The callback function takes one or two parameters: |
||
156 | * |
||
157 | * function ($element [, $query]) {} |
||
158 | * |
||
159 | * The callback must return a boolean |
||
160 | * |
||
161 | * @param mixed $query OPTIONAL the provided query |
||
162 | * @param callable $callback the callback function |
||
163 | * @return mixed|null the found element or null if it hasn't been found |
||
164 | */ |
||
165 | public function findLast() { |
||
184 | |||
185 | /** |
||
186 | * Searches the collection with a given callback and returns all matching elements. |
||
187 | * |
||
188 | * The callback function takes one or two parameters: |
||
189 | * |
||
190 | * function ($element [, $query]) {} |
||
191 | * |
||
192 | * The callback must return a boolean |
||
193 | * |
||
194 | * @param mixed $query OPTIONAL the provided query |
||
195 | * @param callable $callback the callback function |
||
196 | * @return mixed|null the found element or null if it hasn't been found |
||
197 | */ |
||
198 | public function findAll() { |
||
217 | |||
218 | /** |
||
219 | * Internal sort function |
||
220 | * |
||
221 | * @param array $collection the collection on which is operated on |
||
222 | * @param Comparator|callable|null $cmp the compare function |
||
223 | * @param callable $usort the sort function for user passed $cmd |
||
224 | * @param callable $sort the default sort function |
||
225 | */ |
||
226 | protected function doSort(&$collection, $cmp, callable $usort, callable $sort) { |
||
237 | |||
238 | /** |
||
239 | * @internal |
||
240 | */ |
||
241 | public function rewind() { |
||
244 | |||
245 | /** |
||
246 | * @internal |
||
247 | */ |
||
248 | public function current() { |
||
251 | |||
252 | /** |
||
253 | * @internal |
||
254 | */ |
||
255 | public function key() { |
||
258 | |||
259 | /** |
||
260 | * @internal |
||
261 | */ |
||
262 | public function next() { |
||
265 | |||
266 | /** |
||
267 | * @internal |
||
268 | */ |
||
269 | public function valid() { |
||
272 | |||
273 | /** |
||
274 | * @internal |
||
275 | */ |
||
276 | public function __clone() { |
||
279 | |||
280 | } |