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 whether array has only provided keys as indexes |
||
337 | * |
||
338 | * @param array $haystack |
||
339 | * @param array $needles |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | public static function hasOnlyKeys(array $haystack, array $needles) |
||
347 | |||
348 | /** |
||
349 | * Checks if two arrays have all equal keys |
||
350 | * |
||
351 | * @deprecated |
||
352 | * |
||
353 | * @param array $array1 |
||
354 | * @param array $array2 |
||
355 | * |
||
356 | * @return boolean |
||
357 | */ |
||
358 | public static function haveEqualKeys(array $array1, array $array2) |
||
362 | |||
363 | /** |
||
364 | * Check if two arrays have all equal values |
||
365 | * |
||
366 | * @deprecated |
||
367 | * |
||
368 | * @param array $array1 |
||
369 | * @param array $array2 |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | public static function haveEqualValues($array1, $array2) |
||
377 | |||
378 | /** |
||
379 | * Checks if two arrays have all equal keys |
||
380 | * |
||
381 | * @param array $array1 |
||
382 | * @param array $array2 |
||
383 | * |
||
384 | * @return boolean |
||
385 | */ |
||
386 | public static function haveSameKeys(array $array1, array $array2) |
||
390 | |||
391 | /** |
||
392 | * Check if two arrays have all equal values |
||
393 | * |
||
394 | * @param array $array1 |
||
395 | * @param array $array2 |
||
396 | * |
||
397 | * @return bool |
||
398 | */ |
||
399 | public static function haveSameValues($array1, $array2) |
||
403 | |||
404 | /** |
||
405 | * @param mixed $array |
||
406 | * @param mixed $subArray |
||
407 | * @param bool $overwrite |
||
408 | * @param bool $ignoreIfExists |
||
409 | * |
||
410 | * @return array |
||
411 | */ |
||
412 | public static function insertSubArray($array, $subArray, $overwrite = false, $ignoreIfExists = false) |
||
440 | |||
441 | /** |
||
442 | * Checks whether array is associative or numeric |
||
443 | * |
||
444 | * @param array $array |
||
445 | * |
||
446 | * @return bool |
||
447 | */ |
||
448 | public static function isAssociative(array $array) |
||
452 | |||
453 | /** |
||
454 | * Checks whether $subArray is contained in $array |
||
455 | * |
||
456 | * @param array $array |
||
457 | * @param array $subArray |
||
458 | * @param bool $strictComparison |
||
459 | * |
||
460 | * @return bool |
||
461 | * @throws InvalidArgumentException |
||
462 | */ |
||
463 | public static function isSubArray(array $array, array $subArray, $strictComparison = true) |
||
485 | |||
486 | /** |
||
487 | * Selects random sub array |
||
488 | * |
||
489 | * @param array $array |
||
490 | * @param int $numberOfRequiredElements |
||
491 | * |
||
492 | * @return array |
||
493 | * @throws InvalidArgumentException |
||
494 | */ |
||
495 | public static function selectRandomArrayElements(array $array, $numberOfRequiredElements) |
||
516 | |||
517 | /** |
||
518 | * @param array $matrix |
||
519 | * @param mixed $column |
||
520 | * @param mixed $value |
||
521 | * @param bool $insertIfMissing |
||
522 | * @param bool $overwrite |
||
523 | * |
||
524 | * @return array |
||
525 | */ |
||
526 | public static function setColumn(array $matrix, $column, $value, $insertIfMissing = true, $overwrite = true) |
||
552 | |||
553 | /** |
||
554 | * Check whether casting a variable to int would conway useful information |
||
555 | * |
||
556 | * @param mixed $input |
||
557 | * |
||
558 | * @return bool |
||
559 | */ |
||
560 | private static function isLogicallyCastableToInt($input) |
||
564 | } |
||
565 |