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 | * Whether a comment type is allowed. |
||
| 258 | * A comment type is allowed if it's present in the comment type whitelist. |
||
| 259 | * |
||
| 260 | * @param int $comment_id ID of the comment. |
||
| 261 | * @return boolean Whether the comment type is allowed. |
||
| 262 | */ |
||
| 263 | public function is_comment_type_allowed( $comment_id ) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Initialize the module in the sender. |
||
| 274 | * |
||
| 275 | * @access public |
||
| 276 | */ |
||
| 277 | public function init_before_send() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Enqueue the comments actions for full sync. |
||
| 299 | * |
||
| 300 | * @access public |
||
| 301 | * |
||
| 302 | * @param array $config Full sync configuration for this sync module. |
||
| 303 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 304 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 305 | * @return array Number of actions enqueued, and next module state. |
||
| 306 | */ |
||
| 307 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 314 | * |
||
| 315 | * @access public |
||
| 316 | * |
||
| 317 | * @param array $config Full sync configuration for this sync module. |
||
| 318 | * @return int Number of items yet to be enqueued. |
||
| 319 | */ |
||
| 320 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Retrieve the WHERE SQL clause based on the module config. |
||
| 339 | * |
||
| 340 | * @access public |
||
| 341 | * |
||
| 342 | * @param array $config Full sync configuration for this sync module. |
||
| 343 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 344 | */ |
||
| 345 | public function get_where_sql( $config ) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 355 | * |
||
| 356 | * @access public |
||
| 357 | * |
||
| 358 | * @return array Full sync actions of this module. |
||
| 359 | */ |
||
| 360 | public function get_full_sync_actions() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Count all the actions that are going to be sent. |
||
| 366 | * |
||
| 367 | * @access public |
||
| 368 | * |
||
| 369 | * @param array $action_names Names of all the actions that will be sent. |
||
| 370 | * @return int Number of actions. |
||
| 371 | */ |
||
| 372 | public function count_full_sync_actions( $action_names ) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Expand the comment status change before the data is serialized and sent to the server. |
||
| 378 | * |
||
| 379 | * @access public |
||
| 380 | * @todo This is not used currently - let's implement it. |
||
| 381 | * |
||
| 382 | * @param array $args The hook parameters. |
||
| 383 | * @return array The expanded hook parameters. |
||
| 384 | */ |
||
| 385 | public function expand_wp_comment_status_change( $args ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Expand the comment creation before the data is serialized and sent to the server. |
||
| 391 | * |
||
| 392 | * @access public |
||
| 393 | * |
||
| 394 | * @param array $args The hook parameters. |
||
| 395 | * @return array The expanded hook parameters. |
||
| 396 | */ |
||
| 397 | public function expand_wp_insert_comment( $args ) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Filter a comment object to the fields we need. |
||
| 403 | * |
||
| 404 | * @access public |
||
| 405 | * |
||
| 406 | * @param \WP_Comment $comment The unfiltered comment object. |
||
| 407 | * @return \WP_Comment Filtered comment object. |
||
| 408 | */ |
||
| 409 | public function filter_comment( $comment ) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Whether a certain comment meta key is whitelisted for sync. |
||
| 438 | * |
||
| 439 | * @access public |
||
| 440 | * |
||
| 441 | * @param string $meta_key Comment meta key. |
||
| 442 | * @return boolean Whether the meta key is whitelisted. |
||
| 443 | */ |
||
| 444 | public function is_whitelisted_comment_meta( $meta_key ) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Handler for filtering out non-whitelisted comment meta. |
||
| 450 | * |
||
| 451 | * @access public |
||
| 452 | * |
||
| 453 | * @param array $args Hook args. |
||
| 454 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
| 455 | */ |
||
| 456 | public function filter_meta( $args ) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Expand the comment IDs to comment objects and meta before being serialized and sent to the server. |
||
| 466 | * |
||
| 467 | * @access public |
||
| 468 | * |
||
| 469 | * @param array $args The hook parameters. |
||
| 470 | * @return array The expanded hook parameters. |
||
| 471 | */ |
||
| 472 | public function expand_comment_ids( $args ) { |
||
| 489 | } |
||
| 490 |