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:
| 1 | <?php |
||
| 7 | class Transform |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Dotted array cache. |
||
| 11 | * |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | protected $dotted = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Swap two elements between positions. |
||
| 18 | * |
||
| 19 | * @param array $array array to swap |
||
| 20 | * @param string $swapA |
||
| 21 | * @param string $swapB |
||
| 22 | * |
||
| 23 | * @return array|null |
||
| 24 | */ |
||
| 25 | public function swap(array $array, $swapA, $swapB) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Indexes an array depending on the values it contains. |
||
| 32 | * |
||
| 33 | * @param array $array |
||
| 34 | * @param callable $cb Function to combine values. |
||
| 35 | * @param bool $overwrite Should duplicate keys be overwritten? |
||
| 36 | * |
||
| 37 | * @return array Indexed values. |
||
| 38 | */ |
||
| 39 | public function combine(array $array, callable $cb, $overwrite = true) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Collapse an array of arrays into a single array. |
||
| 57 | * |
||
| 58 | * @param array $array |
||
| 59 | * |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | public function collapse(array $array) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Divide an array into two arrays. One with keys and the other with values. |
||
| 79 | * |
||
| 80 | * @param array $array |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | public function divide($array) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Reindexes a list of values. |
||
| 91 | * |
||
| 92 | * @param array $array |
||
| 93 | * @param array $map An map of correspondances of the form |
||
| 94 | * ['currentIndex' => 'newIndex']. |
||
| 95 | * @param boole $keepUnmapped Whether or not to keep keys that are not |
||
| 96 | * remapped. |
||
| 97 | * |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public function reindex(array $array, array $map, $keepUnmapped = true) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Merges two arrays recursively. |
||
| 117 | * |
||
| 118 | * @param array $first Original data. |
||
| 119 | * @param array $second Data to be merged. |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function merge(array $first, array $second) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Extend one array with another. |
||
| 142 | * |
||
| 143 | * @param array $arrays |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function extend(/*args...*/) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Flatten a multi-dimensional associative array with dots. |
||
| 166 | * |
||
| 167 | * @param array $array |
||
| 168 | * @param string $prepend |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function dot($array, $prepend = '') |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Flatten a nested array to a separated key. |
||
| 195 | * |
||
| 196 | * @param array $array |
||
| 197 | * @param string|null $separator |
||
| 198 | * @param string $prepend |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function flatten(array $array, $separator = null, $prepend = '') |
||
| 216 | } |
||
| 217 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.