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 | * Retrieve a comment by its ID. |
||
29 | * |
||
30 | * @access public |
||
31 | * |
||
32 | * @param string $object_type Type of the sync object. |
||
33 | * @param int $id ID of the sync object. |
||
34 | * @return \WP_Comment|bool Filtered \WP_Comment object, or false if the object is not a comment. |
||
35 | */ |
||
36 | public function get_object_by_id( $object_type, $id ) { |
||
47 | |||
48 | /** |
||
49 | * Initialize comments action listeners. |
||
50 | * Also responsible for initializing comment meta listeners. |
||
51 | * |
||
52 | * @access public |
||
53 | * |
||
54 | * @param callable $callable Action handler callable. |
||
55 | */ |
||
56 | public function init_listeners( $callable ) { |
||
57 | add_action( 'wp_insert_comment', $callable, 10, 2 ); |
||
58 | add_action( 'deleted_comment', $callable ); |
||
59 | add_action( 'trashed_comment', $callable ); |
||
60 | add_action( 'spammed_comment', $callable ); |
||
61 | add_action( 'trashed_post_comments', $callable, 10, 2 ); |
||
62 | add_action( 'untrash_post_comments', $callable ); |
||
63 | add_action( 'comment_approved_to_unapproved', $callable ); |
||
64 | add_action( 'comment_unapproved_to_approved', $callable ); |
||
65 | add_action( 'jetpack_modified_comment_contents', $callable, 10, 2 ); |
||
66 | add_action( 'untrashed_comment', $callable, 10, 2 ); |
||
67 | add_action( 'unspammed_comment', $callable, 10, 2 ); |
||
68 | add_filter( 'wp_update_comment_data', array( $this, 'handle_comment_contents_modification' ), 10, 3 ); |
||
69 | |||
70 | /** |
||
71 | * Even though it's messy, we implement these hooks because |
||
72 | * the edit_comment hook doesn't include the data |
||
73 | * so this saves us a DB read for every comment event. |
||
74 | */ |
||
75 | foreach ( $this->get_whitelisted_comment_types() as $comment_type ) { |
||
76 | foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { |
||
77 | $comment_action_name = "comment_{$comment_status}_{$comment_type}"; |
||
78 | add_action( $comment_action_name, $callable, 10, 2 ); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | // Listen for meta changes. |
||
83 | $this->init_listeners_for_meta_type( 'comment', $callable ); |
||
84 | $this->init_meta_whitelist_handler( 'comment', array( $this, 'filter_meta' ) ); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Handler for any comment content updates. |
||
89 | * |
||
90 | * @access public |
||
91 | * |
||
92 | * @param array $new_comment The new, processed comment data. |
||
93 | * @param array $old_comment The old, unslashed comment data. |
||
94 | * @param array $new_comment_with_slashes The new, raw comment data. |
||
95 | * @return array The new, processed comment data. |
||
96 | */ |
||
97 | public function handle_comment_contents_modification( $new_comment, $old_comment, $new_comment_with_slashes ) { |
||
125 | |||
126 | /** |
||
127 | * Initialize comments action listeners for full sync. |
||
128 | * |
||
129 | * @access public |
||
130 | * |
||
131 | * @param callable $callable Action handler callable. |
||
132 | */ |
||
133 | public function init_full_sync_listeners( $callable ) { |
||
136 | |||
137 | /** |
||
138 | * Gets a filtered list of comment types that sync can hook into. |
||
139 | * |
||
140 | * @access public |
||
141 | * |
||
142 | * @return array Defaults to [ '', 'trackback', 'pingback' ]. |
||
143 | */ |
||
144 | public function get_whitelisted_comment_types() { |
||
145 | /** |
||
146 | * Comment types present in this list will sync their status changes to WordPress.com. |
||
147 | * |
||
148 | * @since 7.6.0 |
||
149 | * |
||
150 | * @param array A list of comment types. |
||
151 | */ |
||
152 | return apply_filters( |
||
153 | 'jetpack_sync_whitelisted_comment_types', |
||
154 | array( '', 'trackback', 'pingback' ) |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Initialize the module in the sender. |
||
160 | * |
||
161 | * @access public |
||
162 | */ |
||
163 | public function init_before_send() { |
||
182 | |||
183 | /** |
||
184 | * Enqueue the comments actions for full sync. |
||
185 | * |
||
186 | * @access public |
||
187 | * |
||
188 | * @param array $config Full sync configuration for this sync module. |
||
189 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
190 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
191 | * @return array Number of actions enqueued, and next module state. |
||
192 | */ |
||
193 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
197 | |||
198 | /** |
||
199 | * Retrieve an estimated number of actions that will be enqueued. |
||
200 | * |
||
201 | * @access public |
||
202 | * |
||
203 | * @param array $config Full sync configuration for this sync module. |
||
204 | * @return int Number of items yet to be enqueued. |
||
205 | */ |
||
206 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
222 | |||
223 | /** |
||
224 | * Retrieve the WHERE SQL clause based on the module config. |
||
225 | * |
||
226 | * @access private |
||
227 | * |
||
228 | * @param array $config Full sync configuration for this sync module. |
||
229 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
230 | */ |
||
231 | private function get_where_sql( $config ) { |
||
238 | |||
239 | /** |
||
240 | * Retrieve the actions that will be sent for this module during a full sync. |
||
241 | * |
||
242 | * @access public |
||
243 | * |
||
244 | * @return array Full sync actions of this module. |
||
245 | */ |
||
246 | public function get_full_sync_actions() { |
||
249 | |||
250 | /** |
||
251 | * Count all the actions that are going to be sent. |
||
252 | * |
||
253 | * @access public |
||
254 | * |
||
255 | * @param array $action_names Names of all the actions that will be sent. |
||
256 | * @return int Number of actions. |
||
257 | */ |
||
258 | public function count_full_sync_actions( $action_names ) { |
||
261 | |||
262 | /** |
||
263 | * Expand the comment status change before the data is serialized and sent to the server. |
||
264 | * |
||
265 | * @access public |
||
266 | * @todo This is not used currently - let's implement it. |
||
267 | * |
||
268 | * @param array $args The hook parameters. |
||
269 | * @return array The expanded hook parameters. |
||
270 | */ |
||
271 | public function expand_wp_comment_status_change( $args ) { |
||
274 | |||
275 | /** |
||
276 | * Expand the comment creation before the data is serialized and sent to the server. |
||
277 | * |
||
278 | * @access public |
||
279 | * |
||
280 | * @param array $args The hook parameters. |
||
281 | * @return array The expanded hook parameters. |
||
282 | */ |
||
283 | public function expand_wp_insert_comment( $args ) { |
||
286 | |||
287 | /** |
||
288 | * Filter a comment object to the fields we need. |
||
289 | * |
||
290 | * @access public |
||
291 | * |
||
292 | * @param \WP_Comment $comment The unfiltered comment object. |
||
293 | * @return \WP_Comment Filtered comment object. |
||
294 | */ |
||
295 | public function filter_comment( $comment ) { |
||
321 | |||
322 | /** |
||
323 | * Whether a certain comment meta key is whitelisted for sync. |
||
324 | * |
||
325 | * @access public |
||
326 | * |
||
327 | * @param string $meta_key Comment meta key. |
||
328 | * @return boolean Whether the meta key is whitelisted. |
||
329 | */ |
||
330 | public function is_whitelisted_comment_meta( $meta_key ) { |
||
333 | |||
334 | /** |
||
335 | * Handler for filtering out non-whitelisted comment meta. |
||
336 | * |
||
337 | * @access public |
||
338 | * |
||
339 | * @param array $args Hook args. |
||
340 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
341 | */ |
||
342 | public function filter_meta( $args ) { |
||
345 | |||
346 | /** |
||
347 | * Expand the comment IDs to comment objects and meta before being serialized and sent to the server. |
||
348 | * |
||
349 | * @access public |
||
350 | * |
||
351 | * @param array $args The hook parameters. |
||
352 | * @return array The expanded hook parameters. |
||
353 | */ |
||
354 | public function expand_comment_ids( $args ) { |
||
371 | } |
||
372 |
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: