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 |
||
12 | class ArrayImitator extends AbstractArray |
||
13 | { |
||
14 | // The public method list order by ASC |
||
15 | |||
16 | /** |
||
17 | * Add new element to the array. |
||
18 | * |
||
19 | * @param mixed $element |
||
20 | * |
||
21 | * @return $this |
||
22 | */ |
||
23 | 1 | public function add($element) |
|
24 | { |
||
25 | 1 | $this->elements[] = $element; |
|
26 | |||
27 | 1 | return $this; |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * Create a chunked version of this array. |
||
32 | * |
||
33 | * @param int $size Size of each chunk |
||
34 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
35 | * |
||
36 | * @return static A new array of chunks from the original array |
||
37 | */ |
||
38 | 4 | public function chunk($size, $preserveKeys = false) |
|
42 | |||
43 | /** |
||
44 | * Clear array. |
||
45 | * |
||
46 | * @return $this An empty array. |
||
47 | */ |
||
48 | 4 | public function clear() |
|
54 | |||
55 | /** |
||
56 | * Create an array using this array as values and the other array as keys. |
||
57 | * |
||
58 | * @param array $array Key array |
||
59 | * @deprecated Would be removed (or renamed) |
||
60 | * |
||
61 | * @return static A new array with keys from the other. |
||
62 | */ |
||
63 | public function combineTo(array $array) |
||
67 | |||
68 | /** |
||
69 | * Create an array using this array as keys and the other array as values. |
||
70 | * |
||
71 | * @param array $array Values array |
||
72 | * |
||
73 | * @return static A new array with values from the other array |
||
74 | */ |
||
75 | 1 | public function combineWith(array $array) |
|
79 | |||
80 | /** |
||
81 | * Compute the array of values not present in the other array. |
||
82 | * |
||
83 | * @param array $array Array for diff |
||
84 | * |
||
85 | * @return static A new array containing all the entries from this array |
||
86 | * that are not present in $array |
||
87 | */ |
||
88 | 4 | public function diffWith(array $array) |
|
92 | |||
93 | /** |
||
94 | * Filter the array for elements satisfying the predicate $func. |
||
95 | * |
||
96 | * @param callable $func |
||
97 | * |
||
98 | * @return static A new array with only element satisfying $func |
||
99 | */ |
||
100 | 4 | public function filter(callable $func) |
|
104 | |||
105 | /** |
||
106 | * Exchanges all array keys with their associated values. |
||
107 | * |
||
108 | * @return static The new instance with flipped elements |
||
109 | */ |
||
110 | 4 | public function flip() |
|
114 | |||
115 | /** |
||
116 | * Apply the given function to the every element of the array, |
||
117 | * collecting the results. |
||
118 | * |
||
119 | * @param callable $func |
||
120 | * |
||
121 | * @return static A new array with modified elements |
||
122 | */ |
||
123 | 4 | public function map(callable $func) |
|
127 | |||
128 | /** |
||
129 | * Merges array with the provided one. This array is overwriting. |
||
130 | * |
||
131 | * @param array $array Array to merge with (is overwritten) |
||
132 | * @param bool $recursively Whether array will be merged recursively or no |
||
133 | * @deprecated Would be removed (or renamed) |
||
134 | * |
||
135 | * @return static A new array with the keys/values |
||
136 | * from $array added, that weren't present in the original |
||
137 | */ |
||
138 | View Code Duplication | public function mergeTo(array $array, $recursively = false) |
|
146 | |||
147 | /** |
||
148 | * Merges this array with the provided one. Latter array is overwriting. |
||
149 | * |
||
150 | * @param array $array Array to merge with (overwrites) |
||
151 | * @param bool $recursively Whether array will be merged recursively or no |
||
152 | * |
||
153 | * @return static A new array with the keys/values from $array added |
||
154 | */ |
||
155 | 8 | View Code Duplication | public function mergeWith(array $array, $recursively = false) |
163 | |||
164 | /** |
||
165 | * Pad array to the specified size with a given value. |
||
166 | * |
||
167 | * @param int $size Size of the result array |
||
168 | * @param mixed $value Empty value by default |
||
169 | * |
||
170 | * @return static A new array padded to $size with $value |
||
171 | */ |
||
172 | 4 | public function pad($size, $value) |
|
176 | |||
177 | /** |
||
178 | * Create a numerically re-indexed array. |
||
179 | * |
||
180 | * @return static The new instance with re-indexed elements |
||
181 | */ |
||
182 | 4 | public function reindex() |
|
186 | |||
187 | /** |
||
188 | * Replace the entire array with the other one except keys present in both. |
||
189 | * For keys present in both arrays the value from this array will be used. |
||
190 | * |
||
191 | * @param array $array Array to replace with |
||
192 | * @param bool $recursively Whether array will be replaced recursively or no |
||
193 | * @deprecated Would be removed (or renamed) |
||
194 | * |
||
195 | * @return static A new array with keys from $array and values from both. |
||
196 | */ |
||
197 | View Code Duplication | public function replaceIn(array $array, $recursively = false) |
|
205 | |||
206 | /** |
||
207 | * Replace values in this array with values in the other array |
||
208 | * that have the same key. |
||
209 | * |
||
210 | * @param array $array Array of replacing values |
||
211 | * @param bool $recursively Whether array will be replaced recursively or no |
||
212 | * |
||
213 | * @return static A new array with the same keys but new values |
||
214 | */ |
||
215 | 8 | View Code Duplication | public function replaceWith(array $array, $recursively = false) |
223 | |||
224 | /** |
||
225 | * Reverse the order of the array values. |
||
226 | * |
||
227 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
228 | * |
||
229 | * @return static A new array with the order of the elements reversed |
||
230 | */ |
||
231 | 4 | public function reverse($preserveKeys = false) |
|
235 | |||
236 | /** |
||
237 | * Randomize element order. |
||
238 | * |
||
239 | * @return $this An array with the element order shuffled |
||
240 | */ |
||
241 | 4 | public function shuffle() |
|
247 | |||
248 | /** |
||
249 | * Extract a slice of the array. |
||
250 | * |
||
251 | * @param int $offset Slice begin index |
||
252 | * @param int|null $length Length of the slice |
||
253 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
254 | * |
||
255 | * @return static A slice of the original array with length $length |
||
256 | */ |
||
257 | 4 | public function slice($offset, $length = null, $preserveKeys = false) |
|
261 | |||
262 | /** |
||
263 | * Removes duplicate values from the array. |
||
264 | * |
||
265 | * @param int|null $sortFlags |
||
266 | * |
||
267 | * @return static A new array with only unique elements |
||
268 | */ |
||
269 | 4 | public function unique($sortFlags = null) |
|
273 | |||
274 | /** |
||
275 | * Apply the given function to every element in the array, |
||
276 | * discarding the results. |
||
277 | * |
||
278 | * @param callable $func |
||
279 | * @param bool $recursively Whether array will be walked recursively or no |
||
280 | * |
||
281 | * @return $this An array with modified elements |
||
282 | */ |
||
283 | 8 | public function walk(callable $func, $recursively = false) |
|
293 | } |
||
294 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.