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 |
||
13 | class ArrayImitator extends AbstractArray |
||
14 | { |
||
15 | // The public method list ordered by ASC |
||
16 | |||
17 | /** |
||
18 | * Default value to return if no required key doesn't exist. |
||
19 | * |
||
20 | * @var mixed |
||
21 | */ |
||
22 | protected $defaultValue = null; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Add a new element to the current array. |
||
27 | * |
||
28 | * @param mixed $element |
||
29 | * |
||
30 | * @return ArrayImitator The current array with added value |
||
31 | */ |
||
32 | 1 | public function add($element) |
|
38 | |||
39 | /** |
||
40 | * Create a chunked version of current array. |
||
41 | * |
||
42 | * @param int $size Size of each chunk |
||
43 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
44 | * |
||
45 | * @return ArrayImitator A new array of chunks from the current array |
||
46 | */ |
||
47 | 4 | public function chunk($size, $preserveKeys = false) |
|
51 | |||
52 | /** |
||
53 | * Clear the current array. |
||
54 | * |
||
55 | * @return ArrayImitator The current empty array |
||
56 | */ |
||
57 | 4 | public function clear() |
|
63 | |||
64 | /** |
||
65 | * Create an array using the current array as keys and the other array as values. |
||
66 | * |
||
67 | * @param array $array Values array |
||
68 | * |
||
69 | * @return ArrayImitator A new array with values from the other array |
||
70 | */ |
||
71 | 1 | public function combine(array $array) |
|
75 | |||
76 | /** |
||
77 | * Compute the current array values which not present in the given one. |
||
78 | * |
||
79 | * @param array $array Array for diff |
||
80 | * |
||
81 | * @return ArrayImitator A new array containing all the entries from this array |
||
82 | * that are not present in $array |
||
83 | */ |
||
84 | 4 | public function diff(array $array) |
|
88 | |||
89 | /** |
||
90 | * Filter the current array for elements satisfying the predicate $func. |
||
91 | * |
||
92 | * @param callable $func |
||
93 | * |
||
94 | * @return ArrayImitator A new array with only element satisfying $func |
||
95 | */ |
||
96 | 4 | public function filter(callable $func) |
|
100 | |||
101 | /** |
||
102 | * Exchanges all keys of current array with their associated values. |
||
103 | * |
||
104 | * @return ArrayImitator A new array with flipped elements |
||
105 | */ |
||
106 | 4 | public function flip() |
|
110 | |||
111 | /** |
||
112 | * Compute the current array values which present in the given one. |
||
113 | * |
||
114 | * @param array $array Array for intersect |
||
115 | * |
||
116 | * @return ArrayImitator An array with containing all the entries from this array |
||
117 | * that are present in $array |
||
118 | */ |
||
119 | 4 | public function intersect(array $array) |
|
123 | |||
124 | /** |
||
125 | * Compute the current array values with additional index check |
||
126 | * |
||
127 | * @param array $array Array for intersect |
||
128 | * |
||
129 | * @return ArrayImitator An array with containing all the entries from this array |
||
130 | * that are present in $array. Note that the keys are also used in the comparison |
||
131 | * unlike in intersect(). |
||
132 | */ |
||
133 | 4 | public function intersectAssoc(array $array) |
|
137 | |||
138 | /** |
||
139 | * Compute the current array using keys for comparison which present in the given one. |
||
140 | * |
||
141 | * @param array $array Array for intersect |
||
142 | * |
||
143 | * @return ArrayImitator An array with containing all the entries from this array |
||
144 | * which have keys that are present in $array. |
||
145 | */ |
||
146 | 4 | public function intersectKey(array $array) |
|
150 | |||
151 | /** |
||
152 | * Apply the given function to the every element of the current array, |
||
153 | * collecting the results. |
||
154 | * |
||
155 | * @param callable $func |
||
156 | * |
||
157 | * @return ArrayImitator A new array with modified elements |
||
158 | */ |
||
159 | 4 | public function map(callable $func) |
|
163 | |||
164 | /** |
||
165 | * Merge the current array with the provided one. The latter array is overwriting. |
||
166 | * |
||
167 | * @param array $array Array to merge with (overwrites) |
||
168 | * @param bool $recursively Whether array will be merged recursively or no |
||
169 | * |
||
170 | * @return ArrayImitator A new array with the keys/values from $array added |
||
171 | */ |
||
172 | 8 | View Code Duplication | public function merge(array $array, $recursively = false) |
180 | |||
181 | /** |
||
182 | * Pad the current array to the specified size with a given value. |
||
183 | * |
||
184 | * @param int $size Size of the result array |
||
185 | * @param mixed $value Empty value by default |
||
186 | * |
||
187 | * @return ArrayImitator A new array padded to $size with $value |
||
188 | */ |
||
189 | 4 | public function pad($size, $value) |
|
193 | |||
194 | /** |
||
195 | * Create a numerically re-indexed array based on the current array. |
||
196 | * |
||
197 | * @return ArrayImitator A new array with re-indexed elements |
||
198 | */ |
||
199 | 4 | public function reindex() |
|
203 | |||
204 | /** |
||
205 | * Replace values in the current array with values in the given one |
||
206 | * that have the same key. |
||
207 | * |
||
208 | * @param array $array Array of replacing values |
||
209 | * @param bool $recursively Whether array will be replaced recursively or no |
||
210 | * |
||
211 | * @return ArrayImitator A new array with the same keys but new values |
||
212 | */ |
||
213 | 8 | View Code Duplication | public function replace(array $array, $recursively = false) |
221 | |||
222 | /** |
||
223 | * Reverse the values order of the current array. |
||
224 | * |
||
225 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
226 | * |
||
227 | * @return ArrayImitator A new array with the order of the elements reversed |
||
228 | */ |
||
229 | 4 | public function reverse($preserveKeys = false) |
|
233 | |||
234 | /** |
||
235 | * Randomize elements order of the current array. |
||
236 | * |
||
237 | * @return ArrayImitator The current array with the shuffled elements order |
||
238 | */ |
||
239 | 4 | public function shuffle() |
|
245 | |||
246 | /** |
||
247 | * Extract a slice of the current array. |
||
248 | * |
||
249 | * @param int $offset Slice begin index |
||
250 | * @param int|null $length Length of the slice |
||
251 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
252 | * |
||
253 | * @return ArrayImitator A new array, which is slice of the current array |
||
254 | * with specified $length |
||
255 | */ |
||
256 | 4 | public function slice($offset, $length = null, $preserveKeys = false) |
|
260 | |||
261 | /** |
||
262 | * Remove duplicate values from the current array. |
||
263 | * |
||
264 | * @param int|null $sortFlags |
||
265 | * |
||
266 | * @return ArrayImitator A new array with only unique elements |
||
267 | */ |
||
268 | 4 | public function unique($sortFlags = null) |
|
272 | |||
273 | /** |
||
274 | * Apply the given function to the every element of the current array, |
||
275 | * discarding the results. |
||
276 | * |
||
277 | * @param callable $func |
||
278 | * @param bool $recursively Whether array will be walked recursively or no |
||
279 | * |
||
280 | * @return ArrayImitator The current array with modified elements |
||
281 | */ |
||
282 | 8 | public function walk(callable $func, $recursively = false) |
|
292 | |||
293 | /** |
||
294 | * Allow use classes described with namespace Arrayzy\Extend\<ucfirst name called function> |
||
295 | * |
||
296 | * @param $name |
||
297 | * @param $arguments |
||
298 | * @return mixed |
||
299 | */ |
||
300 | 4 | public function __call($name, $arguments) |
|
309 | |||
310 | /** |
||
311 | * Set default value. |
||
312 | * |
||
313 | * @param mixed $value |
||
314 | */ |
||
315 | 4 | public function setDefaultValue($value) |
|
319 | |||
320 | /** |
||
321 | * @return mixed |
||
322 | */ |
||
323 | 6 | public function getDefaultValue() |
|
327 | } |
||
328 |