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 |
||
5 | class Arr |
||
6 | { |
||
7 | /** |
||
8 | * Retrieves the value of an array element or object property with the given key or property name. |
||
9 | * If the key does not exist in the array or object, the default value will be returned instead. |
||
10 | * |
||
11 | * The key may be specified in a dot format to retrieve the value of a sub-array or the property |
||
12 | * of an embedded object. In particular, if the key is `x.y.z`, then the returned value would |
||
13 | * be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']` |
||
14 | * or `$array->x` is neither an array nor an object, the default value will be returned. |
||
15 | * Note that if the array already has an element `x.y.z`, then its value will be returned |
||
16 | * instead of going through the sub-arrays. So it is better to be done specifying an array of key names |
||
17 | * like `['x', 'y', 'z']`. |
||
18 | * |
||
19 | * Below are some usage examples, |
||
20 | * |
||
21 | * ```php |
||
22 | * // working with array |
||
23 | * $username = \yii\helpers\ArrayHelper::getValue($_POST, 'username'); |
||
24 | * // working with object |
||
25 | * $username = \yii\helpers\ArrayHelper::getValue($user, 'username'); |
||
26 | * // working with anonymous function |
||
27 | * $fullName = \yii\helpers\ArrayHelper::getValue($user, function ($user, $defaultValue) { |
||
28 | * return $user->firstName . ' ' . $user->lastName; |
||
29 | * }); |
||
30 | * // using dot format to retrieve the property of embedded object |
||
31 | * $street = \yii\helpers\ArrayHelper::getValue($users, 'address.street'); |
||
32 | * // using an array of keys to retrieve the value |
||
33 | * $value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']); |
||
34 | * ``` |
||
35 | * |
||
36 | * @param array|object $array array or object to extract value from |
||
37 | * @param string|\Closure|array $key key name of the array element, an array of keys or property name of the object, |
||
38 | * or an anonymous function returning the value. The anonymous function signature should be: |
||
39 | * `function($array, $defaultValue)`. |
||
40 | * The possibility to pass an array of keys is available since version 2.0.4. |
||
41 | * @param mixed $default the default value to be returned if the specified array key does not exist. Not used when |
||
42 | * getting value from an object. |
||
43 | * @return mixed the value of the element if found, default value otherwise |
||
44 | */ |
||
45 | public static function getValue($array, $key, $default = null) |
||
78 | } |
||
79 |
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.