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:
Complex classes like Comments often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Comments, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Comments extends Module { |
||
| 17 | /** |
||
| 18 | * Sync module name. |
||
| 19 | * |
||
| 20 | * @access public |
||
| 21 | * |
||
| 22 | * @return string |
||
| 23 | */ |
||
| 24 | public function name() { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The id field in the database. |
||
| 30 | * |
||
| 31 | * @access public |
||
| 32 | * |
||
| 33 | * @return string |
||
| 34 | */ |
||
| 35 | public function id_field() { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The table in the database. |
||
| 41 | * |
||
| 42 | * @access public |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function table_name() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Retrieve a comment by its ID. |
||
| 52 | * |
||
| 53 | * @access public |
||
| 54 | * |
||
| 55 | * @param string $object_type Type of the sync object. |
||
| 56 | * @param int $id ID of the sync object. |
||
| 57 | * @return \WP_Comment|bool Filtered \WP_Comment object, or false if the object is not a comment. |
||
| 58 | */ |
||
| 59 | public function get_object_by_id( $object_type, $id ) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Initialize comments action listeners. |
||
| 73 | * Also responsible for initializing comment meta listeners. |
||
| 74 | * |
||
| 75 | * @access public |
||
| 76 | * |
||
| 77 | * @param callable $callable Action handler callable. |
||
| 78 | */ |
||
| 79 | public function init_listeners( $callable ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Handler for any comment content updates. |
||
| 128 | * |
||
| 129 | * @access public |
||
| 130 | * |
||
| 131 | * @param array $new_comment The new, processed comment data. |
||
| 132 | * @param array $old_comment The old, unslashed comment data. |
||
| 133 | * @param array $new_comment_with_slashes The new, raw comment data. |
||
| 134 | * @return array The new, processed comment data. |
||
| 135 | */ |
||
| 136 | public function handle_comment_contents_modification( $new_comment, $old_comment, $new_comment_with_slashes ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Initialize comments action listeners for full sync. |
||
| 167 | * |
||
| 168 | * @access public |
||
| 169 | * |
||
| 170 | * @param callable $callable Action handler callable. |
||
| 171 | */ |
||
| 172 | public function init_full_sync_listeners( $callable ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Gets a filtered list of comment types that sync can hook into. |
||
| 178 | * |
||
| 179 | * @access public |
||
| 180 | * |
||
| 181 | * @return array Defaults to [ '', 'trackback', 'pingback' ]. |
||
| 182 | */ |
||
| 183 | public function get_whitelisted_comment_types() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Prevents any comment types that are not in the whitelist from being enqueued and sent to WordPress.com. |
||
| 199 | * |
||
| 200 | * @param array $args Arguments passed to wp_insert_comment, deleted_comment, spammed_comment, etc. |
||
| 201 | * |
||
| 202 | * @return bool or array $args Arguments passed to wp_insert_comment, deleted_comment, spammed_comment, etc. |
||
| 203 | */ |
||
| 204 | public function only_allow_white_listed_comment_types( $args ) { |
||
| 205 | $comment = false; |
||
| 206 | |||
| 207 | if ( isset( $args[1] ) ) { |
||
| 208 | // comment object is available. |
||
| 209 | $comment = $args[1]; |
||
| 210 | } elseif ( is_numeric( $args[0] ) ) { |
||
| 211 | // comment_id is available. |
||
| 212 | $comment = get_comment( $args[0] ); |
||
| 213 | } |
||
| 214 | |||
| 215 | if ( false !== $comment && ! in_array( $comment->comment_type, $this->get_whitelisted_comment_types(), true ) ) { |
||
| 216 | return false; |
||
| 217 | } |
||
| 218 | |||
| 219 | return $args; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Filter all blacklisted post types. |
||
| 224 | * |
||
| 225 | * @param array $args Hook arguments. |
||
| 226 | * @return array|false Hook arguments, or false if the post type is a blacklisted one. |
||
| 227 | */ |
||
| 228 | public function filter_blacklisted_post_types( $args ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Prevents any comment types that are not in the whitelist from being enqueued and sent to WordPress.com. |
||
| 241 | * |
||
| 242 | * @param array $args Arguments passed to wp_{old_status}_to_{new_status}. |
||
| 243 | * |
||
| 244 | * @return bool or array $args Arguments passed to wp_{old_status}_to_{new_status} |
||
| 245 | */ |
||
| 246 | public function only_allow_white_listed_comment_type_transitions( $args ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Initialize the module in the sender. |
||
| 258 | * |
||
| 259 | * @access public |
||
| 260 | */ |
||
| 261 | public function init_before_send() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Enqueue the comments actions for full sync. |
||
| 283 | * |
||
| 284 | * @access public |
||
| 285 | * |
||
| 286 | * @param array $config Full sync configuration for this sync module. |
||
| 287 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 288 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 289 | * @return array Number of actions enqueued, and next module state. |
||
| 290 | */ |
||
| 291 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 298 | * |
||
| 299 | * @access public |
||
| 300 | * |
||
| 301 | * @param array $config Full sync configuration for this sync module. |
||
| 302 | * @return int Number of items yet to be enqueued. |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Retrieve the WHERE SQL clause based on the module config. |
||
| 323 | * |
||
| 324 | * @access public |
||
| 325 | * |
||
| 326 | * @param array $config Full sync configuration for this sync module. |
||
| 327 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 328 | */ |
||
| 329 | public function get_where_sql( $config ) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 339 | * |
||
| 340 | * @access public |
||
| 341 | * |
||
| 342 | * @return array Full sync actions of this module. |
||
| 343 | */ |
||
| 344 | public function get_full_sync_actions() { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Count all the actions that are going to be sent. |
||
| 350 | * |
||
| 351 | * @access public |
||
| 352 | * |
||
| 353 | * @param array $action_names Names of all the actions that will be sent. |
||
| 354 | * @return int Number of actions. |
||
| 355 | */ |
||
| 356 | public function count_full_sync_actions( $action_names ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Expand the comment status change before the data is serialized and sent to the server. |
||
| 362 | * |
||
| 363 | * @access public |
||
| 364 | * @todo This is not used currently - let's implement it. |
||
| 365 | * |
||
| 366 | * @param array $args The hook parameters. |
||
| 367 | * @return array The expanded hook parameters. |
||
| 368 | */ |
||
| 369 | public function expand_wp_comment_status_change( $args ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Expand the comment creation before the data is serialized and sent to the server. |
||
| 375 | * |
||
| 376 | * @access public |
||
| 377 | * |
||
| 378 | * @param array $args The hook parameters. |
||
| 379 | * @return array The expanded hook parameters. |
||
| 380 | */ |
||
| 381 | public function expand_wp_insert_comment( $args ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Filter a comment object to the fields we need. |
||
| 387 | * |
||
| 388 | * @access public |
||
| 389 | * |
||
| 390 | * @param \WP_Comment $comment The unfiltered comment object. |
||
| 391 | * @return \WP_Comment Filtered comment object. |
||
| 392 | */ |
||
| 393 | public function filter_comment( $comment ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Whether a certain comment meta key is whitelisted for sync. |
||
| 422 | * |
||
| 423 | * @access public |
||
| 424 | * |
||
| 425 | * @param string $meta_key Comment meta key. |
||
| 426 | * @return boolean Whether the meta key is whitelisted. |
||
| 427 | */ |
||
| 428 | public function is_whitelisted_comment_meta( $meta_key ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Handler for filtering out non-whitelisted comment meta. |
||
| 434 | * |
||
| 435 | * @access public |
||
| 436 | * |
||
| 437 | * @param array $args Hook args. |
||
| 438 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
| 439 | */ |
||
| 440 | public function filter_meta( $args ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Expand the comment IDs to comment objects and meta before being serialized and sent to the server. |
||
| 446 | * |
||
| 447 | * @access public |
||
| 448 | * |
||
| 449 | * @param array $args The hook parameters. |
||
| 450 | * @return array The expanded hook parameters. |
||
| 451 | */ |
||
| 452 | public function expand_comment_ids( $args ) { |
||
| 469 | } |
||
| 470 |