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 |
||
14 | class Vale |
||
15 | { |
||
16 | /** |
||
17 | * @var Vale |
||
18 | */ |
||
19 | private static $instance; |
||
20 | |||
21 | /** |
||
22 | * @return Vale |
||
23 | */ |
||
24 | 1 | public static function instance() |
|
32 | |||
33 | /** |
||
34 | * @param mixed $data |
||
35 | * @param array|string|int $keys |
||
36 | * @param mixed $default |
||
37 | * |
||
38 | * @return mixed |
||
39 | */ |
||
40 | 1 | public static function get($data, $keys, $default = null) |
|
44 | |||
45 | /** |
||
46 | * @param mixed $data |
||
47 | * @param array|string|int $keys |
||
48 | * @param mixed $value |
||
49 | * |
||
50 | * @return mixed |
||
51 | */ |
||
52 | 1 | public static function set($data, $keys, $value) |
|
56 | |||
57 | /** |
||
58 | * @param mixed $data |
||
59 | * @param array|string|int $keys |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | 1 | public static function has($data, $keys) |
|
67 | |||
68 | /** |
||
69 | * @param mixed $data |
||
70 | * @param array|string|int $keys |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | 1 | public static function remove($data, $keys) |
|
78 | |||
79 | /** |
||
80 | * @param mixed $data |
||
81 | * @param array|string|int $keys |
||
82 | * @param mixed|null $default |
||
83 | * |
||
84 | * @return mixed |
||
85 | */ |
||
86 | 4 | public function getValue($data, $keys, $default = null) |
|
104 | |||
105 | /** |
||
106 | * @param mixed $data |
||
107 | * @param array|string|int $keys |
||
108 | * @param mixed $value |
||
109 | * |
||
110 | * @return mixed |
||
111 | */ |
||
112 | 5 | View Code Duplication | public function setValue($data, $keys, $value) |
146 | |||
147 | /** |
||
148 | * @param mixed $data |
||
149 | * @param array $keys |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 4 | public function hasValue($data, $keys) |
|
175 | |||
176 | /** |
||
177 | * @param mixed $data |
||
178 | * @param array|string|int $keys |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | 5 | View Code Duplication | public function removeValue($data, $keys) |
216 | |||
217 | /** |
||
218 | * @param string[]|string|null $keys |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | 1 | protected function isKeysEmpty($keys) |
|
226 | } |
||
227 |
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.