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 ArrayyAbstract 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 ArrayyAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | abstract class ArrayyAbstract |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected $array = array(); |
||
| 17 | |||
| 18 | //////////////////////////////////////////////////////////////////// |
||
| 19 | ///////////////////////////// ANALYZE ////////////////////////////// |
||
| 20 | //////////////////////////////////////////////////////////////////// |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Check if an array has a given key. |
||
| 24 | * |
||
| 25 | * @param mixed $key |
||
| 26 | * |
||
| 27 | * @return bool |
||
| 28 | */ |
||
| 29 | 9 | public function has($key) |
|
| 36 | |||
| 37 | //////////////////////////////////////////////////////////////////// |
||
| 38 | //////////////////////////// FETCH FROM //////////////////////////// |
||
| 39 | //////////////////////////////////////////////////////////////////// |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get a value from an array (optional using dot-notation). |
||
| 43 | * |
||
| 44 | * @param string $key The key to look for |
||
| 45 | * @param mixed $default Default value to fallback to |
||
| 46 | * @param array $array The array to get from, |
||
| 47 | * if it's set to "null" we use the current array from the class |
||
| 48 | * |
||
| 49 | * @return mixed |
||
| 50 | */ |
||
| 51 | 18 | public function get($key, $default = null, $array = null) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Set a value in a array using dot notation. |
||
| 81 | * |
||
| 82 | * @param string $key The key to set |
||
| 83 | * @param mixed $value Its value |
||
| 84 | * |
||
| 85 | * @return Arrayy |
||
| 86 | */ |
||
| 87 | 9 | public function set($key, $value) |
|
| 88 | { |
||
| 89 | 9 | $this->internalSet($key, $value); |
|
| 90 | |||
| 91 | 9 | return Arrayy::create($this->array); |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get a value from a array and set it if it was not. |
||
| 96 | * |
||
| 97 | * @param string $key The key |
||
| 98 | * @param mixed $default The default value to set if it isn't |
||
| 99 | * |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public function setAndGet($key, $default = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Remove a value from an array using dot notation. |
||
| 114 | * |
||
| 115 | * @param $key |
||
| 116 | * |
||
| 117 | * @return mixed |
||
| 118 | */ |
||
| 119 | public function remove($key) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Fetches all columns $property from a multimensionnal array. |
||
| 137 | * |
||
| 138 | * @param $property |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public function pluck($property) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property |
||
| 156 | * within that. |
||
| 157 | * |
||
| 158 | * @param $property |
||
| 159 | * @param $value |
||
| 160 | * @param string $comparisonOp |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function filterBy($property, $value, $comparisonOp = null) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * find by ... |
||
| 224 | * |
||
| 225 | * @param $property |
||
| 226 | * @param $value |
||
| 227 | * @param string $comparisonOp |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function findBy($property, $value, $comparisonOp = 'eq') |
||
| 235 | |||
| 236 | //////////////////////////////////////////////////////////////////// |
||
| 237 | ///////////////////////////// ANALYZE ////////////////////////////// |
||
| 238 | //////////////////////////////////////////////////////////////////// |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get all keys from the current array. |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function keys() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get all values from a array. |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function values() |
||
| 259 | |||
| 260 | //////////////////////////////////////////////////////////////////// |
||
| 261 | ////////////////////////////// ALTER /////////////////////////////// |
||
| 262 | //////////////////////////////////////////////////////////////////// |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Replace a key with a new key/value pair. |
||
| 266 | * |
||
| 267 | * @param $replace |
||
| 268 | * @param $key |
||
| 269 | * @param $value |
||
| 270 | * |
||
| 271 | * @return Arrayy |
||
| 272 | */ |
||
| 273 | public function replace($replace, $key, $value) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Sort a array by value, by a closure or by a property |
||
| 282 | * If the sorter is null, the array is sorted naturally. |
||
| 283 | * |
||
| 284 | * @param null $sorter |
||
| 285 | * @param string $direction |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function sort($sorter = null, $direction = 'asc') |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Group values from a array according to the results of a closure. |
||
| 323 | * |
||
| 324 | * @param string $grouper a callable function name |
||
| 325 | * @param bool $saveKeys |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function group($grouper, $saveKeys = false) |
||
| 354 | |||
| 355 | //////////////////////////////////////////////////////////////////// |
||
| 356 | ////////////////////////////// HELPERS ///////////////////////////// |
||
| 357 | //////////////////////////////////////////////////////////////////// |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Internal mechanic of set method. |
||
| 361 | * |
||
| 362 | * @param string $key |
||
| 363 | * @param mixed $value |
||
| 364 | * |
||
| 365 | * @return mixed |
||
| 366 | */ |
||
| 367 | 9 | protected function internalSet($key, $value) |
|
| 368 | { |
||
| 369 | 9 | if (null === $key) { |
|
| 370 | /** @noinspection OneTimeUseVariablesInspection */ |
||
| 371 | $array = $value; |
||
| 372 | |||
| 373 | return $array; |
||
| 374 | } |
||
| 375 | |||
| 376 | // Explode the keys |
||
| 377 | 9 | $keys = explode('.', $key); |
|
| 378 | |||
| 379 | // Crawl through the keys |
||
| 380 | 9 | View Code Duplication | while (count($keys) > 1) { |
| 381 | $key = array_shift($keys); |
||
| 382 | |||
| 383 | $this->array[$key] = $this->get(array(), null, $key); |
||
| 384 | $this->array = &$this->array[$key]; |
||
| 385 | } |
||
| 386 | |||
| 387 | // Bind final tree on the array |
||
| 388 | 9 | $key = array_shift($keys); |
|
| 389 | |||
| 390 | 9 | $this->array[$key] = $value; |
|
| 391 | 9 | } |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Internal mechanics of remove method. |
||
| 395 | * |
||
| 396 | * @param $key |
||
| 397 | * |
||
| 398 | * @return boolean |
||
| 399 | */ |
||
| 400 | protected function internalRemove($key) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Given a list, and an iteratee function that returns |
||
| 425 | * a key for each element in the list (or a property name), |
||
| 426 | * returns an object with an index of each item. |
||
| 427 | * Just like groupBy, but for when you know your keys are unique. |
||
| 428 | * |
||
| 429 | * @param mixed $key |
||
| 430 | * |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | public function indexBy($key) |
||
| 445 | } |
||
| 446 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: