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 |
||
15 | class Comments extends Module { |
||
16 | /** |
||
17 | * Sync module name. |
||
18 | * |
||
19 | * @access public |
||
20 | * |
||
21 | * @return string |
||
22 | */ |
||
23 | public function name() { |
||
26 | |||
27 | /** |
||
28 | * The id field in the database. |
||
29 | * |
||
30 | * @access public |
||
31 | * |
||
32 | * @return string |
||
33 | */ |
||
34 | public function id_field() { |
||
37 | |||
38 | /** |
||
39 | * The table in the database. |
||
40 | * |
||
41 | * @access public |
||
42 | * |
||
43 | * @return string|bool |
||
44 | */ |
||
45 | public function table_name() { |
||
48 | |||
49 | /** |
||
50 | * Retrieve a comment by its ID. |
||
51 | * |
||
52 | * @access public |
||
53 | * |
||
54 | * @param string $object_type Type of the sync object. |
||
55 | * @param int $id ID of the sync object. |
||
56 | * @return \WP_Comment|bool Filtered \WP_Comment object, or false if the object is not a comment. |
||
57 | */ |
||
58 | public function get_object_by_id( $object_type, $id ) { |
||
69 | |||
70 | /** |
||
71 | * Initialize comments action listeners. |
||
72 | * Also responsible for initializing comment meta listeners. |
||
73 | * |
||
74 | * @access public |
||
75 | * |
||
76 | * @param callable $callable Action handler callable. |
||
77 | */ |
||
78 | public function init_listeners( $callable ) { |
||
108 | |||
109 | /** |
||
110 | * Handler for any comment content updates. |
||
111 | * |
||
112 | * @access public |
||
113 | * |
||
114 | * @param array $new_comment The new, processed comment data. |
||
115 | * @param array $old_comment The old, unslashed comment data. |
||
116 | * @param array $new_comment_with_slashes The new, raw comment data. |
||
117 | * @return array The new, processed comment data. |
||
118 | */ |
||
119 | public function handle_comment_contents_modification( $new_comment, $old_comment, $new_comment_with_slashes ) { |
||
147 | |||
148 | /** |
||
149 | * Initialize comments action listeners for full sync. |
||
150 | * |
||
151 | * @access public |
||
152 | * |
||
153 | * @param callable $callable Action handler callable. |
||
154 | */ |
||
155 | public function init_full_sync_listeners( $callable ) { |
||
158 | |||
159 | /** |
||
160 | * Gets a filtered list of comment types that sync can hook into. |
||
161 | * |
||
162 | * @access public |
||
163 | * |
||
164 | * @return array Defaults to [ '', 'trackback', 'pingback' ]. |
||
165 | */ |
||
166 | public function get_whitelisted_comment_types() { |
||
179 | |||
180 | /** |
||
181 | * Initialize the module in the sender. |
||
182 | * |
||
183 | * @access public |
||
184 | */ |
||
185 | public function init_before_send() { |
||
204 | |||
205 | /** |
||
206 | * Enqueue the comments actions for full sync. |
||
207 | * |
||
208 | * @access public |
||
209 | * |
||
210 | * @param array $config Full sync configuration for this sync module. |
||
211 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
212 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
213 | * @return array Number of actions enqueued, and next module state. |
||
214 | */ |
||
215 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
219 | |||
220 | /** |
||
221 | * Retrieve an estimated number of actions that will be enqueued. |
||
222 | * |
||
223 | * @access public |
||
224 | * |
||
225 | * @param array $config Full sync configuration for this sync module. |
||
226 | * @return int Number of items yet to be enqueued. |
||
227 | */ |
||
228 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
244 | |||
245 | /** |
||
246 | * Retrieve the WHERE SQL clause based on the module config. |
||
247 | * |
||
248 | * @access public |
||
249 | * |
||
250 | * @param array $config Full sync configuration for this sync module. |
||
251 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
252 | */ |
||
253 | public function get_where_sql( $config ) { |
||
260 | |||
261 | /** |
||
262 | * Retrieve the actions that will be sent for this module during a full sync. |
||
263 | * |
||
264 | * @access public |
||
265 | * |
||
266 | * @return array Full sync actions of this module. |
||
267 | */ |
||
268 | public function get_full_sync_actions() { |
||
271 | |||
272 | /** |
||
273 | * Count all the actions that are going to be sent. |
||
274 | * |
||
275 | * @access public |
||
276 | * |
||
277 | * @param array $action_names Names of all the actions that will be sent. |
||
278 | * @return int Number of actions. |
||
279 | */ |
||
280 | public function count_full_sync_actions( $action_names ) { |
||
283 | |||
284 | /** |
||
285 | * Expand the comment status change before the data is serialized and sent to the server. |
||
286 | * |
||
287 | * @access public |
||
288 | * @todo This is not used currently - let's implement it. |
||
289 | * |
||
290 | * @param array $args The hook parameters. |
||
291 | * @return array The expanded hook parameters. |
||
292 | */ |
||
293 | public function expand_wp_comment_status_change( $args ) { |
||
296 | |||
297 | /** |
||
298 | * Expand the comment creation before the data is serialized and sent to the server. |
||
299 | * |
||
300 | * @access public |
||
301 | * |
||
302 | * @param array $args The hook parameters. |
||
303 | * @return array The expanded hook parameters. |
||
304 | */ |
||
305 | public function expand_wp_insert_comment( $args ) { |
||
308 | |||
309 | /** |
||
310 | * Filter a comment object to the fields we need. |
||
311 | * |
||
312 | * @access public |
||
313 | * |
||
314 | * @param \WP_Comment $comment The unfiltered comment object. |
||
315 | * @return \WP_Comment Filtered comment object. |
||
316 | */ |
||
317 | public function filter_comment( $comment ) { |
||
343 | |||
344 | /** |
||
345 | * Whether a certain comment meta key is whitelisted for sync. |
||
346 | * |
||
347 | * @access public |
||
348 | * |
||
349 | * @param string $meta_key Comment meta key. |
||
350 | * @return boolean Whether the meta key is whitelisted. |
||
351 | */ |
||
352 | public function is_whitelisted_comment_meta( $meta_key ) { |
||
355 | |||
356 | /** |
||
357 | * Handler for filtering out non-whitelisted comment meta. |
||
358 | * |
||
359 | * @access public |
||
360 | * |
||
361 | * @param array $args Hook args. |
||
362 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
363 | */ |
||
364 | public function filter_meta( $args ) { |
||
367 | |||
368 | /** |
||
369 | * Expand the comment IDs to comment objects and meta before being serialized and sent to the server. |
||
370 | * |
||
371 | * @access public |
||
372 | * |
||
373 | * @param array $args The hook parameters. |
||
374 | * @return array The expanded hook parameters. |
||
375 | */ |
||
376 | public function expand_comment_ids( $args ) { |
||
393 | } |
||
394 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: