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 |
||
20 | class backup_key extends abstract_module |
||
21 | { |
||
22 | /** |
||
23 | * @var \phpbb\request\request_interface |
||
24 | */ |
||
25 | private $request; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $backup_registration_table; |
||
31 | |||
32 | /** |
||
33 | * Number of keys that is generated |
||
34 | */ |
||
35 | const NUMBER_OF_KEYS = 6; |
||
36 | |||
37 | /** |
||
38 | * @var \phpbb\passwords\manager |
||
39 | */ |
||
40 | private $password_manager; |
||
41 | |||
42 | /** |
||
43 | * backup_key constructor. |
||
44 | * |
||
45 | * @param \phpbb\db\driver\driver_interface $db |
||
46 | * @param \phpbb\user $user |
||
47 | * @param \phpbb\request\request_interface $request |
||
48 | * @param \phpbb\template\template $template |
||
49 | * @param \phpbb\passwords\manager $password_manager |
||
50 | * @param string $backup_registration_table |
||
51 | */ |
||
52 | View Code Duplication | public function __construct(driver_interface $db, user $user, request_interface $request, template $template, manager $password_manager, $backup_registration_table) |
|
61 | |||
62 | /** |
||
63 | * Get a language key for this specific module. |
||
64 | * @return string |
||
65 | */ |
||
66 | public function get_translatable_name() |
||
70 | |||
71 | /** |
||
72 | * Return the name of the current module |
||
73 | * This is for internal use only |
||
74 | * @return string |
||
75 | */ |
||
76 | public function get_name() |
||
80 | |||
81 | /** |
||
82 | * Return if this module is enabled by the admin |
||
83 | * (And all server requirements are met). |
||
84 | * |
||
85 | * Do not return false in case a specific user disabled this module, |
||
86 | * OR if the user is unable to use this specific module, |
||
87 | * OR if a browser specific item is missing/incorrect. |
||
88 | * @return boolean |
||
89 | */ |
||
90 | public function is_enabled() |
||
94 | |||
95 | /** |
||
96 | * Check if the current user is able to use this module. |
||
97 | * |
||
98 | * This means that the user enabled it in the UCP, |
||
99 | * And has it setup up correctly. |
||
100 | * This method will be called during login, not during registration/ |
||
101 | * |
||
102 | * @param int $user_id |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function is_usable($user_id) |
||
110 | |||
111 | /** |
||
112 | * Check if the user can potentially use this. |
||
113 | * This method is called at registration page. |
||
114 | * |
||
115 | * You can, for example, check if the current browser is suitable. |
||
116 | * |
||
117 | * @param int|boolean $user_id Use false to ignore user |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | public function is_potentially_usable($user_id = false) |
||
125 | |||
126 | /** |
||
127 | * Get the priority for this module. |
||
128 | * A lower priority means more chance it gets selected as default option |
||
129 | * |
||
130 | * There can be only one module with a specific priority! |
||
131 | * If there is already a module registered with this priority, |
||
132 | * a Exception might be thrown |
||
133 | * |
||
134 | * @return int |
||
135 | */ |
||
136 | public function get_priority() |
||
140 | |||
141 | /** |
||
142 | * Start of the login procedure. |
||
143 | * |
||
144 | * @param int $user_id |
||
145 | * |
||
146 | * @return array with data to be assign to the template. |
||
147 | */ |
||
148 | public function login_start($user_id) |
||
152 | |||
153 | /** |
||
154 | * Actual login procedure |
||
155 | * |
||
156 | * @param int $user_id |
||
157 | * |
||
158 | * @return boolean |
||
159 | */ |
||
160 | public function login($user_id) |
||
164 | |||
165 | /** |
||
166 | * If this module can add new keys (Or other things) |
||
167 | * |
||
168 | * @return boolean |
||
169 | */ |
||
170 | public function can_register() |
||
174 | |||
175 | /** |
||
176 | * Start with the registration of a new security key. |
||
177 | * This page should return a name of a template, and |
||
178 | * it should assign the required variables for this template. |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function register_start() |
||
203 | |||
204 | /** |
||
205 | * Do the actual registration of a new security key. |
||
206 | * |
||
207 | * @return boolean Result of the registration. |
||
208 | * @throws BadRequestHttpException |
||
209 | */ |
||
210 | public function register() |
||
215 | |||
216 | /** |
||
217 | * This method is called to show the UCP page. |
||
218 | * You can assign template variables to the template, or do anything else here. |
||
219 | */ |
||
220 | public function show_ucp() |
||
224 | |||
225 | /** |
||
226 | * Delete a specific row from the UCP. |
||
227 | * The data is based on the data provided in show_ucp. |
||
228 | * |
||
229 | * @param int $key |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | View Code Duplication | public function delete($key) |
|
241 | } |
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.