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 |
||
| 18 | class Posts extends Module { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Action handler callable. |
||
| 22 | * |
||
| 23 | * @access private |
||
| 24 | * |
||
| 25 | * @var callable |
||
| 26 | */ |
||
| 27 | private $action_handler; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Import end. |
||
| 31 | * |
||
| 32 | * @access private |
||
| 33 | * |
||
| 34 | * @todo This appears to be unused - let's remove it. |
||
| 35 | * |
||
| 36 | * @var boolean |
||
| 37 | */ |
||
| 38 | private $import_end = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Default previous post state. |
||
| 42 | * Used for default previous post status. |
||
| 43 | * |
||
| 44 | * @access public |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | const DEFAULT_PREVIOUS_STATE = 'new'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Sync module name. |
||
| 52 | * |
||
| 53 | * @access public |
||
| 54 | * |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | public function name() { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The table in the database. |
||
| 63 | * |
||
| 64 | * @access public |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function table_name() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Retrieve a post by its ID. |
||
| 74 | * |
||
| 75 | * @access public |
||
| 76 | * |
||
| 77 | * @param string $object_type Type of the sync object. |
||
| 78 | * @param int $id ID of the sync object. |
||
| 79 | * @return \WP_Post|bool Filtered \WP_Post object, or false if the object is not a post. |
||
| 80 | */ |
||
| 81 | public function get_object_by_id( $object_type, $id ) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Initialize posts action listeners. |
||
| 94 | * |
||
| 95 | * @access public |
||
| 96 | * |
||
| 97 | * @param callable $callable Action handler callable. |
||
| 98 | */ |
||
| 99 | public function init_listeners( $callable ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Initalizes the post sync item early. |
||
| 128 | * |
||
| 129 | * @param int $post_parent Post Parent ID. |
||
| 130 | * @param int $post_ID Post ID. |
||
| 131 | * @param array $args an Array containing post data. |
||
| 132 | * |
||
| 133 | * @return mixed |
||
| 134 | */ |
||
| 135 | public function maybe_initialize_post_sync_item( $post_parent, $post_ID, $args ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Initalizes the post sync item early. |
||
| 146 | * |
||
| 147 | * @param string $action Not used. |
||
| 148 | * @param array $result Used to check if it is not tull. |
||
| 149 | */ |
||
| 150 | public function maybe_initialize_post_sync_item_referer( $action, $result ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Sets the post sync item. |
||
| 163 | * |
||
| 164 | * @param int|string $post_ID Key under which to store the Sync Item on. |
||
| 165 | * |
||
| 166 | * @return Item |
||
| 167 | */ |
||
| 168 | private function set_post_sync_item( $post_ID ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Check if the post type is an attachment. |
||
| 182 | * |
||
| 183 | * @param array $args Post Arguments. |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | private function is_attachment( $args ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Checks if we already have a sync item with a particular post ID. |
||
| 193 | * |
||
| 194 | * @param string|int $post_ID Post Id. |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | private function has_sync_item( $post_ID ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Gets the post sync item. |
||
| 204 | * |
||
| 205 | * @param string|int $post_ID Post Id. |
||
| 206 | * |
||
| 207 | * @return Item |
||
| 208 | */ |
||
| 209 | private function get_post_sync_item( $post_ID ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Checks if we area dealing with a valid post action. |
||
| 216 | * |
||
| 217 | * @param bool $result Result of the post edit action. |
||
| 218 | * |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | private function is_valid_editpost_action( $result ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Gets the post ID from a post request. |
||
| 236 | * |
||
| 237 | * @return int|null Post Id. |
||
| 238 | */ |
||
| 239 | private function get_post_id_from_post_request() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Before Akismet's daily cleanup of spam detection metadata. |
||
| 248 | * |
||
| 249 | * @access public |
||
| 250 | * |
||
| 251 | * @param array $feedback_ids IDs of feedback posts. |
||
| 252 | */ |
||
| 253 | public function daily_akismet_meta_cleanup_before( $feedback_ids ) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * After Akismet's daily cleanup of spam detection metadata. |
||
| 270 | * |
||
| 271 | * @access public |
||
| 272 | * |
||
| 273 | * @param array $feedback_ids IDs of feedback posts. |
||
| 274 | */ |
||
| 275 | public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Initialize posts action listeners for full sync. |
||
| 281 | * |
||
| 282 | * @access public |
||
| 283 | * |
||
| 284 | * @param callable $callable Action handler callable. |
||
| 285 | */ |
||
| 286 | public function init_full_sync_listeners( $callable ) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Initialize the module in the sender. |
||
| 292 | * |
||
| 293 | * @access public |
||
| 294 | */ |
||
| 295 | public function init_before_send() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Enqueue the posts actions for full sync. |
||
| 304 | * |
||
| 305 | * @access public |
||
| 306 | * |
||
| 307 | * @param array $config Full sync configuration for this sync module. |
||
| 308 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 309 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 310 | * @return array Number of actions enqueued, and next module state. |
||
| 311 | */ |
||
| 312 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 320 | * |
||
| 321 | * @access public |
||
| 322 | * |
||
| 323 | * @todo Use $wpdb->prepare for the SQL query. |
||
| 324 | * |
||
| 325 | * @param array $config Full sync configuration for this sync module. |
||
| 326 | * @return array Number of items yet to be enqueued. |
||
| 327 | */ |
||
| 328 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Retrieve the WHERE SQL clause based on the module config. |
||
| 340 | * |
||
| 341 | * @access public |
||
| 342 | * |
||
| 343 | * @param array $config Full sync configuration for this sync module. |
||
| 344 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 345 | */ |
||
| 346 | View Code Duplication | public function get_where_sql( $config ) { |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 359 | * |
||
| 360 | * @access public |
||
| 361 | * |
||
| 362 | * @return array Full sync actions of this module. |
||
| 363 | */ |
||
| 364 | public function get_full_sync_actions() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Process content before send. |
||
| 370 | * |
||
| 371 | * @param array $args Arguments of the `wp_insert_post` hook. |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | public function expand_jetpack_sync_save_post( $args ) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Filter all blacklisted post types. |
||
| 382 | * |
||
| 383 | * @param array $args Hook arguments. |
||
| 384 | * @return array|false Hook arguments, or false if the post type is a blacklisted one. |
||
| 385 | */ |
||
| 386 | public function filter_blacklisted_post_types( $args ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Filter all meta that is not blacklisted, or is stored for a disallowed post type. |
||
| 398 | * |
||
| 399 | * @param array $args Hook arguments. |
||
| 400 | * @return array|false Hook arguments, or false if meta was filtered. |
||
| 401 | */ |
||
| 402 | public function filter_meta( $args ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Whether a post meta key is whitelisted. |
||
| 412 | * |
||
| 413 | * @param string $meta_key Meta key. |
||
| 414 | * @return boolean Whether the post meta key is whitelisted. |
||
| 415 | */ |
||
| 416 | public function is_whitelisted_post_meta( $meta_key ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Whether a post type is allowed. |
||
| 423 | * A post type will be disallowed if it's present in the post type blacklist. |
||
| 424 | * |
||
| 425 | * @param int $post_id ID of the post. |
||
| 426 | * @return boolean Whether the post type is allowed. |
||
| 427 | */ |
||
| 428 | public function is_post_type_allowed( $post_id ) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Remove the embed shortcode. |
||
| 439 | * |
||
| 440 | * @global $wp_embed |
||
| 441 | */ |
||
| 442 | View Code Duplication | public function remove_embed() { |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Add the embed shortcode. |
||
| 453 | * |
||
| 454 | * @global $wp_embed |
||
| 455 | */ |
||
| 456 | View Code Duplication | public function add_embed() { |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Expands wp_insert_post to include filtered content |
||
| 467 | * |
||
| 468 | * @param \WP_Post $post_object Post object. |
||
| 469 | */ |
||
| 470 | public function filter_post_content_and_add_links( $post_object ) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Handle transition from another post status to a published one. |
||
| 578 | * |
||
| 579 | * @param string $new_status New post status. |
||
| 580 | * @param string $old_status Old post status. |
||
| 581 | * @param \WP_Post $post Post object. |
||
| 582 | */ |
||
| 583 | public function save_published( $new_status, $old_status, $post ) { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * When publishing or updating a post, the Gutenberg editor sends two requests: |
||
| 592 | * 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id` |
||
| 593 | * 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1` |
||
| 594 | * |
||
| 595 | * The 2nd request is to update post meta, which is not supported on WP REST API. |
||
| 596 | * When syncing post data, we will include if this was a meta box update. |
||
| 597 | * |
||
| 598 | * @todo Implement nonce verification. |
||
| 599 | * |
||
| 600 | * @return boolean Whether this is a Gutenberg meta box update. |
||
| 601 | */ |
||
| 602 | public function is_gutenberg_meta_box_update() { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Handler for the wp_insert_post hook. |
||
| 615 | * Called upon creation of a new post. |
||
| 616 | * |
||
| 617 | * @param int $post_ID Post ID. |
||
| 618 | * @param \WP_Post $post Post object. |
||
| 619 | * @param boolean $update Whether this is an existing post being updated or not. |
||
| 620 | */ |
||
| 621 | public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Send a published post for sync. |
||
| 658 | * |
||
| 659 | * @param int $post_ID Post ID. |
||
| 660 | * @param \WP_Post $post Post object. |
||
| 661 | * @param Item $sync_item Sync Item Object. |
||
| 662 | */ |
||
| 663 | public function send_published( $post_ID, $post, $sync_item ) { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Expand post IDs to post objects within a hook before they are serialized and sent to the server. |
||
| 741 | * |
||
| 742 | * @access public |
||
| 743 | * |
||
| 744 | * @param array $args The hook parameters. |
||
| 745 | * @return array $args The expanded hook parameters. |
||
| 746 | */ |
||
| 747 | public function expand_post_ids( $args ) { |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
||
| 764 | * |
||
| 765 | * @access public |
||
| 766 | * |
||
| 767 | * @param int $batch_size The batch size for objects. |
||
| 768 | * @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
||
| 769 | * |
||
| 770 | * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found. |
||
| 771 | */ |
||
| 772 | public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) { |
||
| 775 | } |
||
| 776 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: