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 |
||
49 | class provider implements metadataprovider, pluginprovider { |
||
50 | |||
51 | // This trait must be included. |
||
52 | use \core_privacy\local\legacy_polyfill; |
||
53 | |||
54 | /** |
||
55 | * Returns metadata. |
||
56 | * |
||
57 | * @param collection $collection The initialised collection to add items to. |
||
58 | * @return collection A listing of user data stored through this system. |
||
59 | */ |
||
60 | public static function _get_metadata(collection $collection) { |
||
90 | |||
91 | /** |
||
92 | * Get the list of contexts that contain user information for the specified user. |
||
93 | * |
||
94 | * @param int $userid The user to search. |
||
95 | * @return contextlist $contextlist The list of contexts used in this plugin. |
||
96 | */ |
||
97 | View Code Duplication | public static function _get_contexts_for_userid(int $userid) { |
|
|
|||
98 | // If user was already deleted, do nothing. |
||
99 | if (!\core_user::get_user($userid)) { |
||
100 | return; |
||
101 | } |
||
102 | // Fetch all bigbluebuttonbn logs. |
||
103 | $sql = "SELECT c.id |
||
104 | FROM {context} c |
||
105 | INNER JOIN {course_modules} cm |
||
106 | ON cm.id = c.instanceid |
||
107 | AND c.contextlevel = :contextlevel |
||
108 | INNER JOIN {modules} m |
||
109 | ON m.id = cm.module |
||
110 | AND m.name = :modname |
||
111 | INNER JOIN {bigbluebuttonbn} bigbluebuttonbn |
||
112 | ON bigbluebuttonbn.id = cm.instance |
||
113 | INNER JOIN {bigbluebuttonbn_logs} bigbluebuttonbnlogs |
||
114 | ON bigbluebuttonbnlogs.bigbluebuttonbnid = bigbluebuttonbn.id |
||
115 | WHERE bigbluebuttonbnlogs.userid = :userid"; |
||
116 | |||
117 | $params = [ |
||
118 | 'modname' => 'bigbluebuttonbn', |
||
119 | 'contextlevel' => CONTEXT_MODULE, |
||
120 | 'userid' => $userid, |
||
121 | ]; |
||
122 | $contextlist = new contextlist(); |
||
123 | $contextlist->add_from_sql($sql, $params); |
||
124 | return $contextlist; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Export personal data for the given approved_contextlist. User and context information is contained within the contextlist. |
||
129 | * |
||
130 | * @param approved_contextlist $contextlist a list of contexts approved for export. |
||
131 | */ |
||
132 | public static function _export_user_data(approved_contextlist $contextlist) { |
||
135 | |||
136 | /** |
||
137 | * Delete all data for all users in the specified context. |
||
138 | * |
||
139 | * @param \context $context the context to delete in. |
||
140 | */ |
||
141 | public static function _delete_data_for_all_users_in_context(\context $context) { |
||
151 | |||
152 | /** |
||
153 | * Delete all user data for the specified user, in the specified contexts. |
||
154 | * |
||
155 | * @param approved_contextlist $contextlist a list of contexts approved for deletion. |
||
156 | */ |
||
157 | public static function _delete_data_for_user(approved_contextlist $contextlist) { |
||
172 | |||
173 | /** |
||
174 | * Export personal data for the given approved_contextlist related to bigbluebuttonbn logs. |
||
175 | * |
||
176 | * @param approved_contextlist $contextlist a list of contexts approved for export. |
||
177 | */ |
||
178 | protected static function _export_user_data_bigbliebuttonbn_logs(approved_contextlist $contextlist) { |
||
222 | |||
223 | /** |
||
224 | * Return a dict of bigbluebuttonbn IDs mapped to their course module ID. |
||
225 | * |
||
226 | * @param array $cmids The course module IDs. |
||
227 | * @return array In the form of [$bigbluebuttonbnid => $cmid]. |
||
228 | */ |
||
229 | protected static function get_instance_ids_to_cmids_from_cmids(array $cmids) { |
||
245 | |||
246 | /** |
||
247 | * Loop and export from a recordset. |
||
248 | * |
||
249 | * @param \moodle_recordset $recordset The recordset. |
||
250 | * @param string $splitkey The record key to determine when to export. |
||
251 | * @param mixed $initial The initial data to reduce from. |
||
252 | * @param callable $reducer The function to return the dataset, receives current dataset, and the current record. |
||
253 | * @param callable $export The function to export the dataset, receives the last value from $splitkey and the dataset. |
||
254 | * @return void |
||
255 | */ |
||
256 | protected static function recordset_loop_and_export(\moodle_recordset $recordset, $splitkey, $initial, |
||
275 | |||
276 | /** |
||
277 | * Get the list of users who have data within a context. |
||
278 | * |
||
279 | * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination. |
||
280 | */ |
||
281 | View Code Duplication | public static function get_users_in_context(\core_privacy\local\request\userlist $userlist) { |
|
303 | |||
304 | /** |
||
305 | * Delete multiple users within a single context. |
||
306 | * |
||
307 | * @param approved_userlist $userlist The approved context and user information to delete information for. |
||
308 | */ |
||
309 | public static function delete_data_for_users(\core_privacy\local\request\approved_userlist $userlist) { |
||
322 | } |
||
323 |
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.