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 Arr 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 Arr, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Arr |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * convenience function to see if an array definately has a key in an efficient way |
||
| 26 | * @param $arr |
||
| 27 | * @param $key |
||
| 28 | * @return bool |
||
| 29 | */ |
||
| 30 | 1 | static public function hasKey(&$arr, $key) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Convenience function to get the last part of a key from an array by a division character |
||
| 42 | * With this functions an array of ['node_edit_form' = 'whatever', 'user_edit_form' = new stdClass ] |
||
| 43 | * would return an array of ['form','form'] |
||
| 44 | * @param $arr |
||
| 45 | * @param $division |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | 2 | View Code Duplication | static public function getKeysByLastDivision(&$arr, $division) |
| 60 | |||
| 61 | /** |
||
| 62 | * Convenience function to get keys containing a string |
||
| 63 | * @param $arr array to test |
||
| 64 | * @param $contains string to test for |
||
| 65 | * @return array members of $arr whos key contains $contains |
||
| 66 | */ |
||
| 67 | View Code Duplication | static public function getKeyContains(&$arr, $contains) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Convenience function to get the first part of a key from an array by a division character |
||
| 81 | * With this functions an array of ['node_edit_form' = 'whatever', 'user_edit_form' = new stdClass ] |
||
| 82 | * would return an array of ['node','user'] |
||
| 83 | * @param $arr |
||
| 84 | * @param $division |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | 1 | View Code Duplication | static public function getKeysByFirstDivision(&$arr, $division) |
| 100 | |||
| 101 | /** |
||
| 102 | * Convenience function to rekey an array by a division |
||
| 103 | * With this functions an array of ['node_edit_form' = 'whatever', 'user_edit_form' = new stdClass ] |
||
| 104 | * would return an array of ['form' = stdClass] |
||
| 105 | * If your keys share a final part of their key they will get overwritten. Sucks to be you |
||
| 106 | * @param $arr |
||
| 107 | * @param $division |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | 1 | View Code Duplication | static public function reKeyByLastKeyDivision(&$arr, $division) |
| 122 | |||
| 123 | /** |
||
| 124 | * Convenience function to rekey an array by a division |
||
| 125 | * With this functions an array of |
||
| 126 | * ['node_edit_form' = 'whatever', 'user_edit_form' = new stdClass , 'nodelimiterhere' = 0] |
||
| 127 | * with division '_' |
||
| 128 | * would return an array of ['node' = 'whatever','user' = stdClass] |
||
| 129 | * @param $arr |
||
| 130 | * @param $division |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | 1 | View Code Duplication | static public function reKeyByFirstKeyDivision(&$arr, $division) |
| 146 | |||
| 147 | /** |
||
| 148 | * Convenience function to filter arrays by their key containing a string |
||
| 149 | * @param $arr array to test |
||
| 150 | * @param $contains string to test for |
||
| 151 | * @return array members of $arr whos key contains $contains |
||
| 152 | */ |
||
| 153 | 1 | View Code Duplication | static public function filterKeyContains(&$arr, $contains) |
| 164 | |||
| 165 | /** |
||
| 166 | * Convenience function to filter arrays by their key !containing a string |
||
| 167 | * @param $arr array to test |
||
| 168 | * @param $contains string to test for |
||
| 169 | * @return array members of $arr whos key !contains $contains |
||
| 170 | */ |
||
| 171 | 1 | View Code Duplication | static public function filterKeyNotContains(&$arr, $contains) |
| 182 | |||
| 183 | /** |
||
| 184 | * return an array of values filtered by the key starting with $startswith |
||
| 185 | * @param $arr [] |
||
| 186 | * @param $startwith string |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | 1 | View Code Duplication | static public function filterKeyStartsWith(&$arr, $startwith) |
| 200 | |||
| 201 | /** |
||
| 202 | * return an array of values filtered by the key ending with $endswith |
||
| 203 | * @param $arr [] |
||
| 204 | * @param $endswith string |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | 1 | View Code Duplication | static public function filterKeyEndsWith(&$arr, $endswith) |
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Filter an array by value |
||
| 222 | * @param $arr |
||
| 223 | * @param $value |
||
| 224 | * @return array Array of entries that contain $value |
||
| 225 | */ |
||
| 226 | 1 | View Code Duplication | static public function filterHasValue(&$arr, $value) |
| 243 | |||
| 244 | /** |
||
| 245 | * Filter an array by !value |
||
| 246 | * @param $arr |
||
| 247 | * @param $value |
||
| 248 | * @return array Array of entries that do not contain $value |
||
| 249 | */ |
||
| 250 | 1 | View Code Duplication | static public function filterHasNotValue(&$arr, $value) |
| 267 | |||
| 268 | /** |
||
| 269 | * Filter by a type, takes internal type or fully qualified class name |
||
| 270 | * @param $arr |
||
| 271 | * @param $type |
||
| 272 | * @return array Array of entries that are of $type |
||
| 273 | */ |
||
| 274 | 1 | static public function filterByType(&$arr, $type) |
|
| 297 | |||
| 298 | } |