Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like UArray 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 UArray, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class UArray { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Tests if array is associative |
||
| 14 | * @param array $array |
||
| 15 | * @return boolean |
||
| 16 | */ |
||
| 17 | public static function isAssociative($array) { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Returns a new array with the keys $keys |
||
| 23 | * @param array $array an associative array |
||
| 24 | * @param array $keys some keys |
||
| 25 | * @return array |
||
| 26 | */ |
||
| 27 | public static function extractKeys($array,$keys){ |
||
| 36 | |||
| 37 | public static function getValue($array, $key, $pos) { |
||
| 45 | |||
| 46 | public static function getDefaultValue($array, $key, $default) { |
||
| 52 | |||
| 53 | public static function asPhpArray($array, $prefix="",$depth=1,$format=false) { |
||
| 77 | |||
| 78 | public static function asJSON($array){ |
||
| 81 | |||
| 82 | View Code Duplication | public static function remove($array, $search) { |
|
|
|
|||
| 83 | if (\is_array($search)) { |
||
| 84 | foreach ( $search as $val ) { |
||
| 85 | $array=self::removeOne($array, $val); |
||
| 86 | } |
||
| 87 | } else { |
||
| 88 | $array=self::removeOne($array, $search); |
||
| 89 | } |
||
| 90 | return array_values($array); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Removes from array by key |
||
| 95 | * @param array $array |
||
| 96 | * @param int|string $key |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public static function removeByKey($array,$key){ |
||
| 105 | |||
| 106 | public static function removeByKeys($array,$keys){ |
||
| 113 | |||
| 114 | public static function removeOne($array, $search) { |
||
| 120 | |||
| 121 | public static function update(&$array, $search, $newValue) { |
||
| 127 | |||
| 128 | public static function doubleBackSlashes(&$array){ |
||
| 131 | |||
| 132 | private static function parseValue($v, $prefix="",$depth=1,$format=false) { |
||
| 147 | |||
| 148 | public static function iSearch($needle,$haystack,$strict=null){ |
||
| 151 | |||
| 152 | View Code Duplication | public static function iRemove($array, $search) { |
|
| 162 | |||
| 163 | public static function iRemoveOne($array, $search) { |
||
| 169 | } |
||
| 170 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.