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 boolean $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 | * Divide an array into two arrays. One with keys and the other with values. |
||
57 | * |
||
58 | * @param array $array |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | public function divide($array) |
||
66 | |||
67 | /** |
||
68 | * Reindexes a list of values. |
||
69 | * |
||
70 | * @param array $array |
||
71 | * @param array $map An map of correspondances of the form |
||
72 | * ['currentIndex' => 'newIndex']. |
||
73 | * @param boole $keepUnmapped Whether or not to keep keys that are not |
||
74 | * remapped. |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public function reindex(array $array, array $map, $keepUnmapped = true) |
||
92 | |||
93 | /** |
||
94 | * Merges two arrays recursively. |
||
95 | * |
||
96 | * @param array $first Original data. |
||
97 | * @param array $second Data to be merged. |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | public function merge(array $first, array $second) |
||
117 | |||
118 | /** |
||
119 | * Flatten a multi-dimensional associative array with dots. |
||
120 | * |
||
121 | * @param array $array |
||
122 | * @param string $prepend |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public function dot($array, $prepend = '') |
||
146 | |||
147 | /** |
||
148 | * Flatten a nested array to a separated key. |
||
149 | * |
||
150 | * @param array $array |
||
151 | * @param string|null $separator |
||
152 | * @param string $prepend |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | public function flatten(array $array, $separator = null, $prepend = '') |
||
170 | } |
||
171 |
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.