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 Posts 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 Posts, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Posts extends Module { |
||
| 18 | /** |
||
| 19 | * The post IDs of posts that were just published but not synced yet. |
||
| 20 | * |
||
| 21 | * @access private |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private $just_published = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The previous status of posts that we use for calculating post status transitions. |
||
| 29 | * |
||
| 30 | * @access private |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $previous_status = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Action handler callable. |
||
| 38 | * |
||
| 39 | * @access private |
||
| 40 | * |
||
| 41 | * @var callable |
||
| 42 | */ |
||
| 43 | private $action_handler; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Import end. |
||
| 47 | * |
||
| 48 | * @access private |
||
| 49 | * |
||
| 50 | * @todo This appears to be unused - let's remove it. |
||
| 51 | * |
||
| 52 | * @var boolean |
||
| 53 | */ |
||
| 54 | private $import_end = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Default previous post state. |
||
| 58 | * Used for default previous post status. |
||
| 59 | * |
||
| 60 | * @access public |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | const DEFAULT_PREVIOUS_STATE = 'new'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Sync module name. |
||
| 68 | * |
||
| 69 | * @access public |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function name() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The table in the database. |
||
| 79 | * |
||
| 80 | * @access public |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | public function table_name() { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Retrieve a post by its ID. |
||
| 90 | * |
||
| 91 | * @access public |
||
| 92 | * |
||
| 93 | * @param string $object_type Type of the sync object. |
||
| 94 | * @param int $id ID of the sync object. |
||
| 95 | * @return \WP_Post|bool Filtered \WP_Post object, or false if the object is not a post. |
||
| 96 | */ |
||
| 97 | public function get_object_by_id( $object_type, $id ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Initialize posts action listeners. |
||
| 110 | * |
||
| 111 | * @access public |
||
| 112 | * |
||
| 113 | * @param callable $callable Action handler callable. |
||
| 114 | */ |
||
| 115 | public function init_listeners( $callable ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Before Akismet's daily cleanup of spam detection metadata. |
||
| 139 | * |
||
| 140 | * @access public |
||
| 141 | * |
||
| 142 | * @param array $feedback_ids IDs of feedback posts. |
||
| 143 | */ |
||
| 144 | public function daily_akismet_meta_cleanup_before( $feedback_ids ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * After Akismet's daily cleanup of spam detection metadata. |
||
| 161 | * |
||
| 162 | * @access public |
||
| 163 | * |
||
| 164 | * @param array $feedback_ids IDs of feedback posts. |
||
| 165 | */ |
||
| 166 | public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Initialize posts action listeners for full sync. |
||
| 172 | * |
||
| 173 | * @access public |
||
| 174 | * |
||
| 175 | * @param callable $callable Action handler callable. |
||
| 176 | */ |
||
| 177 | public function init_full_sync_listeners( $callable ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Initialize the module in the sender. |
||
| 183 | * |
||
| 184 | * @access public |
||
| 185 | */ |
||
| 186 | public function init_before_send() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Enqueue the posts actions for full sync. |
||
| 195 | * |
||
| 196 | * @access public |
||
| 197 | * |
||
| 198 | * @param array $config Full sync configuration for this sync module. |
||
| 199 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 200 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 201 | * @return array Number of actions enqueued, and next module state. |
||
| 202 | */ |
||
| 203 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 211 | * |
||
| 212 | * @access public |
||
| 213 | * |
||
| 214 | * @todo Use $wpdb->prepare for the SQL query. |
||
| 215 | * |
||
| 216 | * @param array $config Full sync configuration for this sync module. |
||
| 217 | * @return array Number of items yet to be enqueued. |
||
| 218 | */ |
||
| 219 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Retrieve the WHERE SQL clause based on the module config. |
||
| 231 | * |
||
| 232 | * @access public |
||
| 233 | * |
||
| 234 | * @param array $config Full sync configuration for this sync module. |
||
| 235 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 236 | */ |
||
| 237 | View Code Duplication | public function get_where_sql( $config ) { |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 250 | * |
||
| 251 | * @access public |
||
| 252 | * |
||
| 253 | * @return array Full sync actions of this module. |
||
| 254 | */ |
||
| 255 | public function get_full_sync_actions() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Process content before send. |
||
| 261 | * |
||
| 262 | * @param array $args Arguments of the `wp_insert_post` hook. |
||
| 263 | * |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function expand_jetpack_sync_save_post( $args ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Filter all blacklisted post types. |
||
| 273 | * |
||
| 274 | * @param array $args Hook arguments. |
||
| 275 | * @return array|false Hook arguments, or false if the post type is a blacklisted one. |
||
| 276 | */ |
||
| 277 | public function filter_blacklisted_post_types( $args ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Filter all meta that is not blacklisted, or is stored for a disallowed post type. |
||
| 289 | * |
||
| 290 | * @param array $args Hook arguments. |
||
| 291 | * @return array|false Hook arguments, or false if meta was filtered. |
||
| 292 | */ |
||
| 293 | public function filter_meta( $args ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Whether a post meta key is whitelisted. |
||
| 303 | * |
||
| 304 | * @param string $meta_key Meta key. |
||
| 305 | * @return boolean Whether the post meta key is whitelisted. |
||
| 306 | */ |
||
| 307 | public function is_whitelisted_post_meta( $meta_key ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Whether a post type is allowed. |
||
| 314 | * A post type will be disallowed if it's present in the post type blacklist. |
||
| 315 | * |
||
| 316 | * @param int $post_id ID of the post. |
||
| 317 | * @return boolean Whether the post type is allowed. |
||
| 318 | */ |
||
| 319 | public function is_post_type_allowed( $post_id ) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Remove the embed shortcode. |
||
| 330 | * |
||
| 331 | * @global $wp_embed |
||
| 332 | */ |
||
| 333 | View Code Duplication | public function remove_embed() { |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Add the embed shortcode. |
||
| 344 | * |
||
| 345 | * @global $wp_embed |
||
| 346 | */ |
||
| 347 | View Code Duplication | public function add_embed() { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Expands wp_insert_post to include filtered content |
||
| 358 | * |
||
| 359 | * @param \WP_Post $post_object Post object. |
||
| 360 | */ |
||
| 361 | public function filter_post_content_and_add_links( $post_object ) { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Handle transition from another post status to a published one. |
||
| 469 | * |
||
| 470 | * @param string $new_status New post status. |
||
| 471 | * @param string $old_status Old post status. |
||
| 472 | * @param \WP_Post $post Post object. |
||
| 473 | */ |
||
| 474 | public function save_published( $new_status, $old_status, $post ) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * When publishing or updating a post, the Gutenberg editor sends two requests: |
||
| 484 | * 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id` |
||
| 485 | * 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1` |
||
| 486 | * |
||
| 487 | * The 2nd request is to update post meta, which is not supported on WP REST API. |
||
| 488 | * When syncing post data, we will include if this was a meta box update. |
||
| 489 | * |
||
| 490 | * @todo Implement nonce verification. |
||
| 491 | * |
||
| 492 | * @return boolean Whether this is a Gutenberg meta box update. |
||
| 493 | */ |
||
| 494 | public function is_gutenberg_meta_box_update() { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Shim for ::wp_insert_post for items coming in through the REST API. |
||
| 507 | * |
||
| 508 | * @param WP_Post $post Post object. |
||
| 509 | * @param WP_REST_Request $request The original REST request. Unused. |
||
| 510 | * @param bool $new If a item is newly created. True if new, false if updated. |
||
| 511 | */ |
||
| 512 | public function wp_insert_post_shim( $post, $request, $new ) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Handler for the wp_insert_post hook. |
||
| 522 | * Called upon creation of a new post. |
||
| 523 | * |
||
| 524 | * @param int $post_ID Post ID. |
||
|
|
|||
| 525 | * @param \WP_Post $post Post object. |
||
| 526 | * @param boolean $update Whether this is an existing post being updated or not. |
||
| 527 | */ |
||
| 528 | public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Send a published post for sync. |
||
| 576 | * |
||
| 577 | * @param int $post_ID Post ID. |
||
| 578 | * @param \WP_Post $post Post object. |
||
| 579 | */ |
||
| 580 | public function send_published( $post_ID, $post ) { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Expand post IDs to post objects within a hook before they are serialized and sent to the server. |
||
| 658 | * |
||
| 659 | * @access public |
||
| 660 | * |
||
| 661 | * @param array $args The hook parameters. |
||
| 662 | * @return array $args The expanded hook parameters. |
||
| 663 | */ |
||
| 664 | public function expand_post_ids( $args ) { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
||
| 681 | * |
||
| 682 | * @access public |
||
| 683 | * |
||
| 684 | * @param int $batch_size The batch size for objects. |
||
| 685 | * @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
||
| 686 | * |
||
| 687 | * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found. |
||
| 688 | */ |
||
| 689 | public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) { |
||
| 692 | } |
||
| 693 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.