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 | 3 | $usedArray = $array; |
|
55 | 3 | } else { |
|
56 | 29 | $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 Arrayy |
||
146 | */ |
||
147 | 1 | public function filterBy($property, $value, $comparisonOp = null) |
|
148 | { |
||
149 | 1 | if (!$comparisonOp) { |
|
|
|||
150 | 1 | $comparisonOp = is_array($value) ? 'contains' : 'eq'; |
|
151 | 1 | } |
|
152 | |||
153 | $ops = array( |
||
154 | 'eq' => function ($item, $prop, $value) { |
||
155 | 1 | return $item[$prop] === $value; |
|
156 | 1 | }, |
|
157 | 'gt' => function ($item, $prop, $value) { |
||
158 | return $item[$prop] > $value; |
||
159 | 1 | }, |
|
160 | 'gte' => function ($item, $prop, $value) { |
||
161 | return $item[$prop] >= $value; |
||
162 | 1 | }, |
|
163 | 'lt' => function ($item, $prop, $value) { |
||
164 | 1 | return $item[$prop] < $value; |
|
165 | 1 | }, |
|
166 | 'lte' => function ($item, $prop, $value) { |
||
167 | return $item[$prop] <= $value; |
||
168 | 1 | }, |
|
169 | 'ne' => function ($item, $prop, $value) { |
||
170 | return $item[$prop] !== $value; |
||
171 | 1 | }, |
|
172 | 'contains' => function ($item, $prop, $value) { |
||
173 | 1 | return in_array($item[$prop], (array)$value, true); |
|
174 | 1 | }, |
|
175 | 'notContains' => function ($item, $prop, $value) { |
||
176 | return !in_array($item[$prop], (array)$value, true); |
||
177 | 1 | }, |
|
178 | 'newer' => function ($item, $prop, $value) { |
||
179 | return strtotime($item[$prop]) > strtotime($value); |
||
180 | 1 | }, |
|
181 | 'older' => function ($item, $prop, $value) { |
||
182 | return strtotime($item[$prop]) < strtotime($value); |
||
183 | 1 | }, |
|
184 | 1 | ); |
|
185 | |||
186 | 1 | $result = array_values( |
|
187 | 1 | array_filter( |
|
188 | 1 | (array)$this->array, |
|
189 | function ($item) use ( |
||
190 | 1 | $property, |
|
191 | 1 | $value, |
|
192 | 1 | $ops, |
|
193 | 1 | $comparisonOp |
|
194 | ) { |
||
195 | 1 | $item = (array)$item; |
|
196 | 1 | $itemArrayy = new Arrayy($item); |
|
197 | 1 | $item[$property] = $itemArrayy->get($property, array()); |
|
198 | |||
199 | 1 | return $ops[$comparisonOp]($item, $property, $value); |
|
200 | } |
||
201 | 1 | ) |
|
202 | 1 | ); |
|
203 | |||
204 | 1 | return Arrayy::create($result); |
|
205 | } |
||
206 | |||
207 | /** |
||
208 | * find by ... |
||
209 | * |
||
210 | * @param $property |
||
211 | * @param $value |
||
212 | * @param string $comparisonOp |
||
213 | * |
||
214 | * @return Arrayy |
||
215 | */ |
||
216 | public function findBy($property, $value, $comparisonOp = 'eq') |
||
222 | |||
223 | //////////////////////////////////////////////////////////////////// |
||
224 | ///////////////////////////// ANALYZE ////////////////////////////// |
||
225 | //////////////////////////////////////////////////////////////////// |
||
226 | |||
227 | /** |
||
228 | * Get all keys from the current array. |
||
229 | * |
||
230 | * @return Arrayy |
||
231 | */ |
||
232 | 1 | public function keys() |
|
238 | |||
239 | /** |
||
240 | * Get all values from a array. |
||
241 | * |
||
242 | * @return Arrayy |
||
243 | */ |
||
244 | 1 | public function values() |
|
250 | |||
251 | //////////////////////////////////////////////////////////////////// |
||
252 | ////////////////////////////// ALTER /////////////////////////////// |
||
253 | //////////////////////////////////////////////////////////////////// |
||
254 | |||
255 | /** |
||
256 | * Replace a key with a new key/value pair. |
||
257 | * |
||
258 | * @param $replace |
||
259 | * @param $key |
||
260 | * @param $value |
||
261 | * |
||
262 | * @return Arrayy |
||
263 | */ |
||
264 | 1 | public function replace($replace, $key, $value) |
|
270 | |||
271 | /** |
||
272 | * Sort a array by value, by a closure or by a property |
||
273 | * If the sorter is null, the array is sorted naturally. |
||
274 | * |
||
275 | * @param null $sorter |
||
276 | * @param string $direction |
||
277 | * |
||
278 | * @return Arrayy |
||
279 | */ |
||
280 | 1 | public function sort($sorter = null, $direction = 'asc') |
|
311 | |||
312 | /** |
||
313 | * Group values from a array according to the results of a closure. |
||
314 | * |
||
315 | * @param string $grouper a callable function name |
||
316 | * @param bool $saveKeys |
||
317 | * |
||
318 | * @return Arrayy |
||
319 | */ |
||
320 | 3 | public function group($grouper, $saveKeys = false) |
|
345 | |||
346 | //////////////////////////////////////////////////////////////////// |
||
347 | ////////////////////////////// HELPERS ///////////////////////////// |
||
348 | //////////////////////////////////////////////////////////////////// |
||
349 | |||
350 | /** |
||
351 | * Internal mechanic of set method. |
||
352 | * |
||
353 | * @param string $key |
||
354 | * @param mixed $value |
||
355 | * |
||
356 | * @return mixed |
||
357 | */ |
||
358 | 14 | protected function internalSet($key, $value) |
|
383 | |||
384 | /** |
||
385 | * Internal mechanics of remove method. |
||
386 | * |
||
387 | * @param $key |
||
388 | * |
||
389 | * @return boolean |
||
390 | */ |
||
391 | 10 | protected function internalRemove($key) |
|
413 | |||
414 | /** |
||
415 | * Given a list, and an iteratee function that returns |
||
416 | * a key for each element in the list (or a property name), |
||
417 | * returns an object with an index of each item. |
||
418 | * Just like groupBy, but for when you know your keys are unique. |
||
419 | * |
||
420 | * @param mixed $key |
||
421 | * |
||
422 | * @return Arrayy |
||
423 | */ |
||
424 | 3 | public function indexBy($key) |
|
436 | } |
||
437 |
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: