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 | class ArrayHelper |
||
12 | { |
||
13 | /** |
||
14 | * Get an item from an array using "dot" notation. |
||
15 | * |
||
16 | * @param array $array |
||
17 | * @param string $key |
||
18 | * @param mixed $default |
||
19 | * @return mixed |
||
20 | */ |
||
21 | public static function arrayGet($array, $key, $default = null) |
||
39 | |||
40 | /** |
||
41 | * Check if an item exists in an array using "dot" notation. |
||
42 | * |
||
43 | * @param array $array |
||
44 | * @param string $key |
||
45 | * @return bool |
||
46 | */ |
||
47 | public static function arrayHas($array, $key) |
||
67 | |||
68 | /** |
||
69 | * Flatten a multi-dimensional associative array with dots. |
||
70 | * |
||
71 | * @param array $array |
||
72 | * @param string $prepend |
||
73 | * @return array |
||
74 | */ |
||
75 | public static function arrayDot($array, $prepend = '') |
||
88 | |||
89 | public static function arrayDiffAssocRecursive($array1, $array2) |
||
93 | |||
94 | public static function arraySame($array1, $array2) |
||
98 | |||
99 | |||
100 | /** |
||
101 | * Return the default value of the given value. |
||
102 | * |
||
103 | * @param mixed $value |
||
104 | * @return mixed |
||
105 | */ |
||
106 | public static function value($value) |
||
110 | } |
||
111 |
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.