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 Nip_Helper_Arrays 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 Nip_Helper_Arrays, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Nip_Helper_Arrays extends Nip\Helpers\AbstractHelper |
||
|
|
|||
| 4 | { |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Determine whether the given value is array accessible. |
||
| 8 | * |
||
| 9 | * @param mixed $value |
||
| 10 | * @return bool |
||
| 11 | */ |
||
| 12 | public static function accessible($value) |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Determine if the given key exists in the provided array. |
||
| 19 | * |
||
| 20 | * @param \ArrayAccess|array $array |
||
| 21 | * @param string|int $key |
||
| 22 | * @return bool |
||
| 23 | */ |
||
| 24 | public static function exists($array, $key) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Return the first element in an array passing a given truth test. |
||
| 34 | * |
||
| 35 | * @param array $array |
||
| 36 | * @param callable|null $callback |
||
| 37 | * @param mixed $default |
||
| 38 | * @return mixed |
||
| 39 | */ |
||
| 40 | public static function first($array, callable $callback = null, $default = null) |
||
| 57 | |||
| 58 | public function toXLS($array, $filename, $labels = array()) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get an item from an array using "dot" notation. |
||
| 100 | * |
||
| 101 | * @param \ArrayAccess|array $array |
||
| 102 | * @param string $key |
||
| 103 | * @param mixed $default |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | public static function get($array, $key, $default = null) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Produces a new version of the array that does not contain any of the specified values |
||
| 129 | * |
||
| 130 | * @param array $array |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function without($array) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Produces a new version of the array that does not contain any of the specified keys |
||
| 149 | * |
||
| 150 | * @param array $array |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | View Code Duplication | public function withoutKeys($array) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Fetch the same property for all the elements. |
||
| 169 | * |
||
| 170 | * @param array $array |
||
| 171 | * @param string $property |
||
| 172 | * @return array The property values |
||
| 173 | */ |
||
| 174 | public function changeKey($array, $property) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Fetch the same property for all the elements. |
||
| 189 | * |
||
| 190 | * @param array|\Nip\Records\Collections\Collection $array |
||
| 191 | * @param string $property |
||
| 192 | * @param bool|string $return |
||
| 193 | * @return array The property values |
||
| 194 | */ |
||
| 195 | public function pluck($array, $property, &$return = false) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Fetch the same property for all the elements. |
||
| 214 | * |
||
| 215 | * @param array $array |
||
| 216 | * @param string $property |
||
| 217 | */ |
||
| 218 | public function pluckFromArray($array, $property) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Finds array item that matches $params |
||
| 231 | * |
||
| 232 | * @param ArrayAccess $array |
||
| 233 | * @param array $params |
||
| 234 | * @return mixed |
||
| 235 | */ |
||
| 236 | public function find($array, $params) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Finds all array items that match $params |
||
| 258 | * |
||
| 259 | * @param array $array |
||
| 260 | * @param array $params |
||
| 261 | * @param string $key |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function findAll($array, $params, $returnKey = false) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Transposes a bidimensional array (matrix) |
||
| 291 | * |
||
| 292 | * @param array $array |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public function transpose($array) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document. |
||
| 312 | * |
||
| 313 | * @param array $data |
||
| 314 | * @param string $rootNodeName - what you want the root node to be - defaults to data |
||
| 315 | * @param SimpleXMLElement $xml - should only be used recursively |
||
| 316 | * @return string XML |
||
| 317 | */ |
||
| 318 | public function toXML($data, $rootNodeName = 'ResultSet', &$xml = null) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Determine if a variable is an associative array |
||
| 367 | * |
||
| 368 | * @param array $array |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | public static function isAssoc($array) |
||
| 375 | |||
| 376 | function merge_distinct(array &$array1, array &$array2) |
||
| 390 | } |
||
| 391 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.