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 |
||
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 | 18 | 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 | 31 | public function get($key, $default = null, $array = null) |
|
52 | { |
||
53 | 31 | if (is_array($array) === true) { |
|
54 | 4 | $usedArray = $array; |
|
55 | 4 | } else { |
|
56 | 28 | $usedArray = $this->array; |
|
57 | } |
||
58 | |||
59 | 31 | if (null === $key) { |
|
60 | 1 | return $usedArray; |
|
61 | } |
||
62 | |||
63 | 31 | if (isset($usedArray[$key])) { |
|
64 | 22 | return $usedArray[$key]; |
|
65 | } |
||
66 | |||
67 | // Crawl through array, get key according to object or not |
||
68 | 16 | foreach (explode('.', $key) as $segment) { |
|
69 | 16 | if (!isset($usedArray[$segment])) { |
|
70 | 16 | return $default instanceof Closure ? $default() : $default; |
|
71 | } |
||
72 | |||
73 | $usedArray = $usedArray[$segment]; |
||
74 | } |
||
75 | |||
76 | return $usedArray; |
||
77 | } |
||
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 | 14 | public function set($key, $value) |
|
93 | |||
94 | /** |
||
95 | * Get a value from a array and set it if it was not. |
||
96 | * |
||
97 | * WARNING: this method only set the value, if the $key is not already set |
||
98 | * |
||
99 | * @param string $key The key |
||
100 | * @param mixed $default The default value to set if it isn't |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | 9 | public function setAndGet($key, $default = null) |
|
113 | |||
114 | /** |
||
115 | * Remove a value from an array using dot notation. |
||
116 | * |
||
117 | * @param mixed $key |
||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | 10 | public function remove($key) |
|
136 | |||
137 | /** |
||
138 | * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property |
||
139 | * within that. |
||
140 | * |
||
141 | * @param $property |
||
142 | * @param $value |
||
143 | * @param string $comparisonOp |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | 1 | public function filterBy($property, $value, $comparisonOp = null) |
|
204 | |||
205 | /** |
||
206 | * find by ... |
||
207 | * |
||
208 | * @param $property |
||
209 | * @param $value |
||
210 | * @param string $comparisonOp |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | public function findBy($property, $value, $comparisonOp = 'eq') |
||
218 | |||
219 | //////////////////////////////////////////////////////////////////// |
||
220 | ///////////////////////////// ANALYZE ////////////////////////////// |
||
221 | //////////////////////////////////////////////////////////////////// |
||
222 | |||
223 | /** |
||
224 | * Get all keys from the current array. |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | 1 | public function keys() |
|
232 | |||
233 | /** |
||
234 | * Get all values from a array. |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | 1 | public function values() |
|
242 | |||
243 | //////////////////////////////////////////////////////////////////// |
||
244 | ////////////////////////////// ALTER /////////////////////////////// |
||
245 | //////////////////////////////////////////////////////////////////// |
||
246 | |||
247 | /** |
||
248 | * Replace a key with a new key/value pair. |
||
249 | * |
||
250 | * @param $replace |
||
251 | * @param $key |
||
252 | * @param $value |
||
253 | * |
||
254 | * @return Arrayy |
||
255 | */ |
||
256 | 1 | public function replace($replace, $key, $value) |
|
262 | |||
263 | /** |
||
264 | * Sort a array by value, by a closure or by a property |
||
265 | * If the sorter is null, the array is sorted naturally. |
||
266 | * |
||
267 | * @param null $sorter |
||
268 | * @param string $direction |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | 1 | public function sort($sorter = null, $direction = 'asc') |
|
303 | |||
304 | /** |
||
305 | * Group values from a array according to the results of a closure. |
||
306 | * |
||
307 | * @param string $grouper a callable function name |
||
308 | * @param bool $saveKeys |
||
309 | * |
||
310 | * @return array |
||
311 | */ |
||
312 | 3 | public function group($grouper, $saveKeys = false) |
|
337 | |||
338 | //////////////////////////////////////////////////////////////////// |
||
339 | ////////////////////////////// HELPERS ///////////////////////////// |
||
340 | //////////////////////////////////////////////////////////////////// |
||
341 | |||
342 | /** |
||
343 | * Internal mechanic of set method. |
||
344 | * |
||
345 | * @param string $key |
||
346 | * @param mixed $value |
||
347 | * |
||
348 | * @return mixed |
||
349 | */ |
||
350 | 14 | protected function internalSet($key, $value) |
|
375 | |||
376 | /** |
||
377 | * Internal mechanics of remove method. |
||
378 | * |
||
379 | * @param $key |
||
380 | * |
||
381 | * @return boolean |
||
382 | */ |
||
383 | 10 | protected function internalRemove($key) |
|
405 | |||
406 | /** |
||
407 | * Given a list, and an iteratee function that returns |
||
408 | * a key for each element in the list (or a property name), |
||
409 | * returns an object with an index of each item. |
||
410 | * Just like groupBy, but for when you know your keys are unique. |
||
411 | * |
||
412 | * @param mixed $key |
||
413 | * |
||
414 | * @return array |
||
415 | */ |
||
416 | 3 | public function indexBy($key) |
|
428 | } |
||
429 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: