Complex classes like SimpleArrayLibrary 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 SimpleArrayLibrary, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class SimpleArrayLibrary |
||
9 | { |
||
10 | /** |
||
11 | * @param array $config |
||
12 | * @param array $keys |
||
13 | * @param mixed $value |
||
14 | * |
||
15 | * @return array |
||
16 | */ |
||
17 | public static function addConfigRow(array $config, array $keys, $value) |
||
32 | |||
33 | /** |
||
34 | * Checks if all elements of the array have same value |
||
35 | * |
||
36 | * @param array $haystack |
||
37 | * @param mixed $needle |
||
38 | * |
||
39 | * @return boolean |
||
40 | */ |
||
41 | public static function allElementsEqual(array $haystack, $needle = null) |
||
60 | |||
61 | const TYPE_INT = 'int'; |
||
62 | const TYPE_STRING = 'string'; |
||
63 | const TYPE_FLOAT = 'float'; |
||
64 | const TYPE_BOOL = 'bool'; |
||
65 | const TYPE_ARRAY = 'array'; |
||
66 | const TYPE_OBJECT = 'object'; |
||
67 | |||
68 | /** |
||
69 | * @param array $matrix |
||
70 | * @param array $castMap |
||
71 | * @param bool $allKeysMustBePresent |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | public static function castColumns(array $matrix, array $castMap, $allKeysMustBePresent = true) |
||
120 | |||
121 | /** |
||
122 | * Counts maximum array depth recursively |
||
123 | * |
||
124 | * @param array $array |
||
125 | * |
||
126 | * @return int |
||
127 | */ |
||
128 | public static function countMaxDepth(array $array) |
||
141 | |||
142 | /** |
||
143 | * Counts maximum array depth iteratively |
||
144 | * |
||
145 | * @param array $array |
||
146 | * |
||
147 | * @return int |
||
148 | */ |
||
149 | public static function countMaxDepthIterative(array $array) |
||
174 | |||
175 | /** |
||
176 | * Counts maximum array depth |
||
177 | * |
||
178 | * @param mixed $potentialArray |
||
179 | * @param int $depth |
||
180 | * |
||
181 | * @return int |
||
182 | * @throws InvalidArgumentException |
||
183 | */ |
||
184 | public static function countMinDepth($potentialArray, $depth = 0) |
||
206 | |||
207 | /** |
||
208 | * @param array $matrix |
||
209 | * @param mixed $columns |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | public static function deleteColumns(array $matrix, array $columns) |
||
232 | |||
233 | /** |
||
234 | * Extracts a column from an array |
||
235 | * |
||
236 | * @param array $array |
||
237 | * @param array $columns |
||
238 | * @param bool $allRowsMustHaveAllColumns |
||
239 | * |
||
240 | * @return array |
||
241 | * @throws UnexpectedValueException |
||
242 | */ |
||
243 | public static function getColumns(array $array, array $columns, $allRowsMustHaveAllColumns = false) |
||
273 | |||
274 | /** |
||
275 | * Checks if an array is rectangular array and returns dimensions or -1 if it's not rectangular |
||
276 | * |
||
277 | * @param array $array |
||
278 | * |
||
279 | * @return int|array |
||
280 | */ |
||
281 | public static function getRectangularDimensions(array $array) |
||
308 | |||
309 | /** |
||
310 | * Checks if $array's keys contain all of $subArray's values |
||
311 | * |
||
312 | * @param array $haystack |
||
313 | * @param array $needles |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | public static function hasAllKeys(array $haystack, array $needles) |
||
321 | |||
322 | /** |
||
323 | * Checks if $array's keys contain all of $subArray's values |
||
324 | * |
||
325 | * @param array $haystack |
||
326 | * @param array $needles |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | public static function hasAllValues(array $haystack, array $needles) |
||
334 | |||
335 | /** |
||
336 | * Checks if $array's keys contain all of $subArray's values |
||
337 | * |
||
338 | * @param array $haystack |
||
339 | * @param array $needles |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | public static function hasAllValuesMultiDimensional(array $haystack, array $needles) |
||
355 | |||
356 | /** |
||
357 | * Checks whether array has only provided keys as indexes |
||
358 | * |
||
359 | * @param array $haystack |
||
360 | * @param array $needles |
||
361 | * |
||
362 | * @return bool |
||
363 | */ |
||
364 | public static function hasOnlyKeys(array $haystack, array $needles) |
||
368 | |||
369 | /** |
||
370 | * Checks if two arrays have all equal keys |
||
371 | * |
||
372 | * @param array $array1 |
||
373 | * @param array $array2 |
||
374 | * |
||
375 | * @return boolean |
||
376 | */ |
||
377 | public static function haveSameKeys(array $array1, array $array2) |
||
381 | |||
382 | /** |
||
383 | * Check if two arrays have all equal values |
||
384 | * |
||
385 | * @param array $array1 |
||
386 | * @param array $array2 |
||
387 | * |
||
388 | * @return bool |
||
389 | */ |
||
390 | public static function haveSameValues($array1, $array2) |
||
394 | |||
395 | /** |
||
396 | * @param mixed $array |
||
397 | * @param mixed $subArray |
||
398 | * @param bool $overwrite |
||
399 | * @param bool $ignoreIfExists |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | public static function insertSubArray($array, $subArray, $overwrite = false, $ignoreIfExists = false) |
||
431 | |||
432 | /** |
||
433 | * Checks whether array is associative or numeric |
||
434 | * |
||
435 | * @param array $array |
||
436 | * |
||
437 | * @return bool |
||
438 | */ |
||
439 | public static function isAssociative(array $array) |
||
443 | |||
444 | /** |
||
445 | * Checks whether $subArray is contained in $array |
||
446 | * |
||
447 | * @param array $array |
||
448 | * @param array $subArray |
||
449 | * @param bool $strictComparison |
||
450 | * |
||
451 | * @return bool |
||
452 | * @throws InvalidArgumentException |
||
453 | */ |
||
454 | public static function isSubArray(array $array, array $subArray, $strictComparison = true) |
||
476 | |||
477 | /** |
||
478 | * Selects random sub array |
||
479 | * |
||
480 | * @param array $array |
||
481 | * @param int $numberOfRequiredElements |
||
482 | * |
||
483 | * @return array |
||
484 | * @throws InvalidArgumentException |
||
485 | */ |
||
486 | public static function selectRandomArrayElements(array $array, $numberOfRequiredElements) |
||
507 | |||
508 | /** |
||
509 | * @param array $matrix |
||
510 | * @param mixed $column |
||
511 | * @param mixed $value |
||
512 | * @param bool $insertIfMissing |
||
513 | * @param bool $overwrite |
||
514 | * |
||
515 | * @return array |
||
516 | */ |
||
517 | public static function setColumn(array $matrix, $column, $value, $insertIfMissing = true, $overwrite = true) |
||
543 | |||
544 | /** |
||
545 | * Check whether casting a variable to int would conway useful information |
||
546 | * |
||
547 | * @param mixed $input |
||
548 | * |
||
549 | * @return bool |
||
550 | */ |
||
551 | private static function isLogicallyCastableToInt($input) |
||
555 | } |