Complex classes like CollectionMethods 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 CollectionMethods, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class CollectionMethods |
||
21 | { |
||
22 | //////////////////////////////////////////////////////////////////// |
||
23 | ///////////////////////////// ANALYZE ////////////////////////////// |
||
24 | //////////////////////////////////////////////////////////////////// |
||
25 | |||
26 | /** |
||
27 | * Check if an array has a given key. |
||
28 | * |
||
29 | * @param array $array |
||
30 | * @param mixed $key |
||
31 | * |
||
32 | * @return bool |
||
33 | */ |
||
34 | public static function has(array &$array, $key) |
||
41 | |||
42 | //////////////////////////////////////////////////////////////////// |
||
43 | //////////////////////////// FETCH FROM //////////////////////////// |
||
44 | //////////////////////////////////////////////////////////////////// |
||
45 | |||
46 | /** |
||
47 | * Get a value from an collection using dot-notation. |
||
48 | * |
||
49 | * @param array $collection The collection to get from |
||
50 | * @param string $key The key to look for |
||
51 | * @param mixed $default Default value to fallback to |
||
52 | * |
||
53 | * @return mixed |
||
54 | */ |
||
55 | public static function get(&$collection, $key, $default = null) |
||
80 | |||
81 | /** |
||
82 | * Set a value in a collection using dot notation. |
||
83 | * |
||
84 | * @param mixed $collection The collection |
||
85 | * @param string $key The key to set |
||
86 | * @param mixed $value Its value |
||
87 | * |
||
88 | * @return mixed |
||
89 | */ |
||
90 | public static function set($collection, $key, $value) |
||
96 | |||
97 | /** |
||
98 | * Get a value from a collection and set it if it wasn't. |
||
99 | * |
||
100 | * @param mixed $collection The collection |
||
101 | * @param string $key The key |
||
102 | * @param mixed $default The default value to set if it isn't |
||
103 | * |
||
104 | * @return mixed |
||
105 | */ |
||
106 | public static function setAndGet(&$collection, $key, $default = null) |
||
115 | |||
116 | /** |
||
117 | * Remove a value from an array using dot notation. |
||
118 | * |
||
119 | * @param $collection |
||
120 | * @param $key |
||
121 | * |
||
122 | * @return mixed |
||
123 | */ |
||
124 | public static function remove($collection, $key) |
||
139 | |||
140 | /** |
||
141 | * Fetches all columns $property from a multimensionnal array. |
||
142 | * |
||
143 | * @param $collection |
||
144 | * @param $property |
||
145 | * |
||
146 | * @return array|object |
||
147 | */ |
||
148 | public static function pluck($collection, $property) |
||
163 | |||
164 | /** |
||
165 | * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property |
||
166 | * within that. |
||
167 | * |
||
168 | * @param $collection |
||
169 | * @param $property |
||
170 | * @param $value |
||
171 | * @param null $comparisonOp |
||
172 | * |
||
173 | * @return array|object |
||
174 | */ |
||
175 | public static function filterBy($collection, $property, $value, $comparisonOp = null) |
||
234 | |||
235 | /** |
||
236 | * find by ... |
||
237 | * |
||
238 | * @param $collection |
||
239 | * @param $property |
||
240 | * @param $value |
||
241 | * @param string $comparisonOp |
||
242 | * |
||
243 | * @return Arrayy |
||
244 | */ |
||
245 | public static function findBy($collection, $property, $value, $comparisonOp = 'eq') |
||
253 | |||
254 | //////////////////////////////////////////////////////////////////// |
||
255 | ///////////////////////////// ANALYZE ////////////////////////////// |
||
256 | //////////////////////////////////////////////////////////////////// |
||
257 | |||
258 | /** |
||
259 | * Get all keys from a collection. |
||
260 | * |
||
261 | * @param $collection |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | public static function keys($collection) |
||
269 | |||
270 | /** |
||
271 | * Get all values from a collection. |
||
272 | * |
||
273 | * @param $collection |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | public static function values($collection) |
||
281 | |||
282 | //////////////////////////////////////////////////////////////////// |
||
283 | ////////////////////////////// ALTER /////////////////////////////// |
||
284 | //////////////////////////////////////////////////////////////////// |
||
285 | |||
286 | /** |
||
287 | * Replace a key with a new key/value pair. |
||
288 | * |
||
289 | * @param $collection |
||
290 | * @param $replace |
||
291 | * @param $key |
||
292 | * @param $value |
||
293 | * |
||
294 | * @return mixed |
||
295 | */ |
||
296 | public static function replace($collection, $replace, $key, $value) |
||
303 | |||
304 | /** |
||
305 | * Sort a collection by value, by a closure or by a property |
||
306 | * If the sorter is null, the collection is sorted naturally. |
||
307 | * |
||
308 | * @param $collection |
||
309 | * @param null $sorter |
||
310 | * @param string $direction |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | public static function sort($collection, $sorter = null, $direction = 'asc') |
||
345 | |||
346 | /** |
||
347 | * Group values from a collection according to the results of a closure. |
||
348 | * |
||
349 | * @param $collection |
||
350 | * @param $grouper |
||
351 | * @param bool $saveKeys |
||
352 | * |
||
353 | * @return array |
||
354 | */ |
||
355 | public static function group($collection, $grouper, $saveKeys = false) |
||
377 | |||
378 | //////////////////////////////////////////////////////////////////// |
||
379 | ////////////////////////////// HELPERS ///////////////////////////// |
||
380 | //////////////////////////////////////////////////////////////////// |
||
381 | |||
382 | /** |
||
383 | * Internal mechanic of set method. |
||
384 | * |
||
385 | * @param $collection |
||
386 | * @param $key |
||
387 | * @param $value |
||
388 | * |
||
389 | * @return mixed |
||
390 | */ |
||
391 | protected static function internalSet(&$collection, $key, $value) |
||
426 | |||
427 | /** |
||
428 | * Internal mechanics of remove method. |
||
429 | * |
||
430 | * @param $collection |
||
431 | * @param $key |
||
432 | * |
||
433 | * @return bool |
||
434 | */ |
||
435 | protected static function internalRemove(&$collection, $key) |
||
465 | |||
466 | /** |
||
467 | * Given a list, and an iteratee function that returns |
||
468 | * a key for each element in the list (or a property name), |
||
469 | * returns an object with an index of each item. |
||
470 | * Just like groupBy, but for when you know your keys are unique. |
||
471 | * |
||
472 | * @param array $array |
||
473 | * @param mixed $key |
||
474 | * |
||
475 | * @return array |
||
476 | */ |
||
477 | public static function indexBy(array $array, $key) |
||
489 | } |
||
490 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.