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 |
||
27 | class session_helper implements session_helper_interface |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * @var driver_interface |
||
32 | */ |
||
33 | private $db; |
||
34 | |||
35 | /** |
||
36 | * @var config |
||
37 | */ |
||
38 | private $config; |
||
39 | |||
40 | /** |
||
41 | * @var user |
||
42 | */ |
||
43 | private $user; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $modules = array(); |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $registration_table; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | private $user_table; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | private $user_array = array(); |
||
64 | |||
65 | /** |
||
66 | * @var \phpbb\template\template |
||
67 | */ |
||
68 | private $template; |
||
69 | |||
70 | /** |
||
71 | * @var \phpbb\controller\helper |
||
72 | */ |
||
73 | private $controller_helper; |
||
74 | |||
75 | /** |
||
76 | * Constructor |
||
77 | * |
||
78 | * @access public |
||
79 | * |
||
80 | * @param driver_interface $db |
||
81 | * @param config $config |
||
82 | * @param user $user |
||
83 | * @param service_collection $modules |
||
84 | * @param \phpbb\template\template $template |
||
85 | * @param \phpbb\controller\helper $controller_helper |
||
86 | * @param string $registration_table |
||
87 | * @param string $user_table |
||
88 | */ |
||
89 | View Code Duplication | public function __construct(driver_interface $db, config $config, user $user, service_collection $modules, template $template, helper $controller_helper, $registration_table, $user_table) |
|
102 | |||
103 | /** |
||
104 | * Register the tagged modules if they are enabled. |
||
105 | * @param service_collection $modules |
||
106 | */ |
||
107 | private function validateModules(service_collection $modules) |
||
129 | |||
130 | /** |
||
131 | * @param $requested_module |
||
132 | * @return null|module_interface |
||
133 | */ |
||
134 | public function findModule($requested_module) |
||
148 | |||
149 | /** |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getModules() |
||
156 | |||
157 | /** |
||
158 | * @param int $user_id |
||
159 | * @param bool $admin |
||
160 | * @param array $userdata |
||
161 | * @param bool $try |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function isTfaRequired($user_id, $admin = false, $userdata = array(), $try = false) |
||
188 | |||
189 | /** |
||
190 | * Check if the user has two factor authentication added to his account. |
||
191 | * |
||
192 | * @param int $user_id |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function isTfaRegistered($user_id) |
||
214 | |||
215 | /** |
||
216 | * @param int $user_id |
||
217 | * @param bool $admin |
||
218 | * @param bool $auto_login |
||
219 | * @param bool $viewonline |
||
220 | * @param string $redirect |
||
221 | */ |
||
222 | public function generate_page($user_id, $admin, $auto_login, $viewonline, $redirect) |
||
279 | |||
280 | /** |
||
281 | * Return the userdata for a specific user. |
||
282 | * |
||
283 | * @param int $user_id |
||
284 | * @param array $userdata |
||
285 | * @return array |
||
286 | */ |
||
287 | private function user_data($user_id, $userdata = array()) |
||
298 | |||
299 | /** |
||
300 | * @param int $user_id |
||
301 | * @param array $userdata |
||
302 | * @param string|array $permission |
||
303 | * @param bool $admin |
||
304 | * @param bool $try |
||
305 | * @return bool |
||
306 | */ |
||
307 | private function do_permission_check($user_id, $userdata, $permission, $admin, $try) |
||
330 | } |
||
331 |
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.