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 | * Retrieve a post by its ID. |
||
| 79 | * |
||
| 80 | * @access public |
||
| 81 | * |
||
| 82 | * @param string $object_type Type of the sync object. |
||
| 83 | * @param int $id ID of the sync object. |
||
| 84 | * @return \WP_Post|bool Filtered \WP_Post object, or false if the object is not a post. |
||
| 85 | */ |
||
| 86 | public function get_object_by_id( $object_type, $id ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Initialize posts action listeners. |
||
| 99 | * |
||
| 100 | * @access public |
||
| 101 | * |
||
| 102 | * @param callable $callable Action handler callable. |
||
| 103 | */ |
||
| 104 | public function init_listeners( $callable ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Before Akismet's daily cleanup of spam detection metadata. |
||
| 127 | * |
||
| 128 | * @access public |
||
| 129 | * |
||
| 130 | * @param array $feedback_ids IDs of feedback posts. |
||
| 131 | */ |
||
| 132 | public function daily_akismet_meta_cleanup_before( $feedback_ids ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * After Akismet's daily cleanup of spam detection metadata. |
||
| 149 | * |
||
| 150 | * @access public |
||
| 151 | * |
||
| 152 | * @param array $feedback_ids IDs of feedback posts. |
||
| 153 | */ |
||
| 154 | public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Initialize posts action listeners for full sync. |
||
| 160 | * |
||
| 161 | * @access public |
||
| 162 | * |
||
| 163 | * @param callable $callable Action handler callable. |
||
| 164 | */ |
||
| 165 | public function init_full_sync_listeners( $callable ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Initialize the module in the sender. |
||
| 171 | * |
||
| 172 | * @access public |
||
| 173 | */ |
||
| 174 | public function init_before_send() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Enqueue the posts actions for full sync. |
||
| 183 | * |
||
| 184 | * @access public |
||
| 185 | * |
||
| 186 | * @param array $config Full sync configuration for this sync module. |
||
| 187 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 188 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 189 | * @return array Number of actions enqueued, and next module state. |
||
| 190 | */ |
||
| 191 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 199 | * |
||
| 200 | * @access public |
||
| 201 | * |
||
| 202 | * @todo Use $wpdb->prepare for the SQL query. |
||
| 203 | * |
||
| 204 | * @param array $config Full sync configuration for this sync module. |
||
| 205 | * @return array Number of items yet to be enqueued. |
||
| 206 | */ |
||
| 207 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Retrieve the WHERE SQL clause based on the module config. |
||
| 219 | * |
||
| 220 | * @access private |
||
| 221 | * |
||
| 222 | * @param array $config Full sync configuration for this sync module. |
||
| 223 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 224 | */ |
||
| 225 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 238 | * |
||
| 239 | * @access public |
||
| 240 | * |
||
| 241 | * @return array Full sync actions of this module. |
||
| 242 | */ |
||
| 243 | public function get_full_sync_actions() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Process content before send. |
||
| 249 | * |
||
| 250 | * @param array $args Arguments of the `wp_insert_post` hook. |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function expand_jetpack_sync_save_post( $args ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Filter all blacklisted post types. |
||
| 261 | * |
||
| 262 | * @param array $args Hook arguments. |
||
| 263 | * @return array|false Hook arguments, or false if the post type is a blacklisted one. |
||
| 264 | */ |
||
| 265 | public function filter_blacklisted_post_types( $args ) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Filter all meta that is not blacklisted, or is stored for a disallowed post type. |
||
| 277 | * |
||
| 278 | * @param array $args Hook arguments. |
||
| 279 | * @return array|false Hook arguments, or false if meta was filtered. |
||
| 280 | */ |
||
| 281 | public function filter_meta( $args ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Whether a post meta key is whitelisted. |
||
| 291 | * |
||
| 292 | * @param string $meta_key Meta key. |
||
| 293 | * @return boolean Whether the post meta key is whitelisted. |
||
| 294 | */ |
||
| 295 | public function is_whitelisted_post_meta( $meta_key ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Whether a post type is allowed. |
||
| 302 | * A post type will be disallowed if it's present in the post type blacklist. |
||
| 303 | * |
||
| 304 | * @param int $post_id ID of the post. |
||
| 305 | * @return boolean Whether the post type is allowed. |
||
| 306 | */ |
||
| 307 | public function is_post_type_allowed( $post_id ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Remove the embed shortcode. |
||
| 317 | * |
||
| 318 | * @global $wp_embed |
||
| 319 | */ |
||
| 320 | View Code Duplication | public function remove_embed() { |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Add the embed shortcode. |
||
| 331 | * |
||
| 332 | * @global $wp_embed |
||
| 333 | */ |
||
| 334 | View Code Duplication | public function add_embed() { |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Expands wp_insert_post to include filtered content |
||
| 345 | * |
||
| 346 | * @param \WP_Post $post_object Post object. |
||
| 347 | */ |
||
| 348 | public function filter_post_content_and_add_links( $post_object ) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Handle transition from another post status to a published one. |
||
| 456 | * |
||
| 457 | * @param string $new_status New post status. |
||
| 458 | * @param string $old_status Old post status. |
||
| 459 | * @param \WP_Post $post Post object. |
||
| 460 | */ |
||
| 461 | public function save_published( $new_status, $old_status, $post ) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * When publishing or updating a post, the Gutenberg editor sends two requests: |
||
| 471 | * 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id` |
||
| 472 | * 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1` |
||
| 473 | * |
||
| 474 | * The 2nd request is to update post meta, which is not supported on WP REST API. |
||
| 475 | * When syncing post data, we will include if this was a meta box update. |
||
| 476 | * |
||
| 477 | * @todo Implement nonce verification. |
||
| 478 | * |
||
| 479 | * @return boolean Whether this is a Gutenberg meta box update. |
||
| 480 | */ |
||
| 481 | public function is_gutenberg_meta_box_update() { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Handler for the wp_insert_post hook. |
||
| 494 | * Called upon creation of a new post. |
||
| 495 | * |
||
| 496 | * @param int $post_ID Post ID. |
||
|
|
|||
| 497 | * @param \WP_Post $post Post object. |
||
| 498 | * @param boolean $update Whether this is an existing post being updated or not. |
||
| 499 | */ |
||
| 500 | public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Send a published post for sync. |
||
| 543 | * |
||
| 544 | * @param int $post_ID Post ID. |
||
| 545 | * @param \WP_Post $post Post object. |
||
| 546 | */ |
||
| 547 | public function send_published( $post_ID, $post ) { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Expand post IDs to post objects within a hook before they are serialized and sent to the server. |
||
| 625 | * |
||
| 626 | * @access public |
||
| 627 | * |
||
| 628 | * @param array $args The hook parameters. |
||
| 629 | * @return array $args The expanded hook parameters. |
||
| 630 | */ |
||
| 631 | public function expand_post_ids( $args ) { |
||
| 645 | } |
||
| 646 |
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.