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 ) { |
||
137 | |||
138 | /** |
||
139 | * Before Akismet's daily cleanup of spam detection metadata. |
||
140 | * |
||
141 | * @access public |
||
142 | * |
||
143 | * @param array $feedback_ids IDs of feedback posts. |
||
144 | */ |
||
145 | public function daily_akismet_meta_cleanup_before( $feedback_ids ) { |
||
167 | |||
168 | /** |
||
169 | * After Akismet's daily cleanup of spam detection metadata. |
||
170 | * |
||
171 | * @access public |
||
172 | * |
||
173 | * @param array $feedback_ids IDs of feedback posts. |
||
174 | */ |
||
175 | public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
178 | |||
179 | /** |
||
180 | * Initialize posts action listeners for full sync. |
||
181 | * |
||
182 | * @access public |
||
183 | * |
||
184 | * @param callable $callable Action handler callable. |
||
185 | */ |
||
186 | public function init_full_sync_listeners( $callable ) { |
||
189 | |||
190 | /** |
||
191 | * Initialize the module in the sender. |
||
192 | * |
||
193 | * @access public |
||
194 | */ |
||
195 | public function init_before_send() { |
||
201 | |||
202 | /** |
||
203 | * Enqueue the posts actions for full sync. |
||
204 | * |
||
205 | * @access public |
||
206 | * |
||
207 | * @param array $config Full sync configuration for this sync module. |
||
208 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
209 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
210 | * @return array Number of actions enqueued, and next module state. |
||
211 | */ |
||
212 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
217 | |||
218 | /** |
||
219 | * Retrieve an estimated number of actions that will be enqueued. |
||
220 | * |
||
221 | * @access public |
||
222 | * |
||
223 | * @todo Use $wpdb->prepare for the SQL query. |
||
224 | * |
||
225 | * @param array $config Full sync configuration for this sync module. |
||
226 | * @return array Number of items yet to be enqueued. |
||
227 | */ |
||
228 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
237 | |||
238 | /** |
||
239 | * Retrieve the WHERE SQL clause based on the module config. |
||
240 | * |
||
241 | * @access public |
||
242 | * |
||
243 | * @param array $config Full sync configuration for this sync module. |
||
244 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
245 | */ |
||
246 | View Code Duplication | public function get_where_sql( $config ) { |
|
256 | |||
257 | /** |
||
258 | * Retrieve the actions that will be sent for this module during a full sync. |
||
259 | * |
||
260 | * @access public |
||
261 | * |
||
262 | * @return array Full sync actions of this module. |
||
263 | */ |
||
264 | public function get_full_sync_actions() { |
||
267 | |||
268 | /** |
||
269 | * Process content before send. |
||
270 | * |
||
271 | * @param array $args Arguments of the `wp_insert_post` hook. |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | public function expand_jetpack_sync_save_post( $args ) { |
||
279 | |||
280 | /** |
||
281 | * Filter all blacklisted post types. |
||
282 | * |
||
283 | * @param array $args Hook arguments. |
||
284 | * @return array|false Hook arguments, or false if the post type is a blacklisted one. |
||
285 | */ |
||
286 | public function filter_blacklisted_post_types_deleted( $args ) { |
||
296 | |||
297 | /** |
||
298 | * Filter all blacklisted post types. |
||
299 | * |
||
300 | * @param array $args Hook arguments. |
||
301 | * @return array|false Hook arguments, or false if the post type is a blacklisted one. |
||
302 | */ |
||
303 | public function filter_blacklisted_post_types( $args ) { |
||
312 | |||
313 | /** |
||
314 | * Filter all meta that is not blacklisted, or is stored for a disallowed post type. |
||
315 | * |
||
316 | * @param array $args Hook arguments. |
||
317 | * @return array|false Hook arguments, or false if meta was filtered. |
||
318 | */ |
||
319 | public function filter_meta( $args ) { |
||
326 | |||
327 | /** |
||
328 | * Whether a post meta key is whitelisted. |
||
329 | * |
||
330 | * @param string $meta_key Meta key. |
||
331 | * @return boolean Whether the post meta key is whitelisted. |
||
332 | */ |
||
333 | public function is_whitelisted_post_meta( $meta_key ) { |
||
337 | |||
338 | /** |
||
339 | * Whether a post type is allowed. |
||
340 | * A post type will be disallowed if it's present in the post type blacklist. |
||
341 | * |
||
342 | * @param int $post_id ID of the post. |
||
343 | * @return boolean Whether the post type is allowed. |
||
344 | */ |
||
345 | public function is_post_type_allowed( $post_id ) { |
||
353 | |||
354 | /** |
||
355 | * Remove the embed shortcode. |
||
356 | * |
||
357 | * @global $wp_embed |
||
358 | */ |
||
359 | View Code Duplication | public function remove_embed() { |
|
367 | |||
368 | /** |
||
369 | * Add the embed shortcode. |
||
370 | * |
||
371 | * @global $wp_embed |
||
372 | */ |
||
373 | View Code Duplication | public function add_embed() { |
|
381 | |||
382 | /** |
||
383 | * Expands wp_insert_post to include filtered content |
||
384 | * |
||
385 | * @param \WP_Post $post_object Post object. |
||
386 | */ |
||
387 | public function filter_post_content_and_add_links( $post_object ) { |
||
492 | |||
493 | /** |
||
494 | * Handle transition from another post status to a published one. |
||
495 | * |
||
496 | * @param string $new_status New post status. |
||
497 | * @param string $old_status Old post status. |
||
498 | * @param \WP_Post $post Post object. |
||
499 | */ |
||
500 | public function save_published( $new_status, $old_status, $post ) { |
||
507 | |||
508 | /** |
||
509 | * When publishing or updating a post, the Gutenberg editor sends two requests: |
||
510 | * 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id` |
||
511 | * 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1` |
||
512 | * |
||
513 | * The 2nd request is to update post meta, which is not supported on WP REST API. |
||
514 | * When syncing post data, we will include if this was a meta box update. |
||
515 | * |
||
516 | * @todo Implement nonce verification. |
||
517 | * |
||
518 | * @return boolean Whether this is a Gutenberg meta box update. |
||
519 | */ |
||
520 | public function is_gutenberg_meta_box_update() { |
||
530 | |||
531 | /** |
||
532 | * Handler for the wp_insert_post hook. |
||
533 | * Called upon creation of a new post. |
||
534 | * |
||
535 | * @param int $post_ID Post ID. |
||
536 | * @param \WP_Post $post Post object. |
||
537 | * @param boolean $update Whether this is an existing post being updated or not. |
||
538 | */ |
||
539 | public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
||
584 | |||
585 | /** |
||
586 | * Handler for the wp_after_insert_post hook. |
||
587 | * Called after creation/update of a new post. |
||
588 | * |
||
589 | * @param int $post_ID Post ID. |
||
590 | * @param \WP_Post $post Post object. |
||
591 | **/ |
||
592 | public function wp_after_insert_post( $post_ID, $post ) { |
||
604 | |||
605 | /** |
||
606 | * Send a published post for sync. |
||
607 | * |
||
608 | * @param int $post_ID Post ID. |
||
609 | * @param \WP_Post $post Post object. |
||
610 | */ |
||
611 | public function send_published( $post_ID, $post ) { |
||
689 | |||
690 | /** |
||
691 | * Expand post IDs to post objects within a hook before they are serialized and sent to the server. |
||
692 | * |
||
693 | * @access public |
||
694 | * |
||
695 | * @param array $args The hook parameters. |
||
696 | * @return array $args The expanded hook parameters. |
||
697 | */ |
||
698 | public function expand_post_ids( $args ) { |
||
712 | |||
713 | /** |
||
714 | * Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
||
715 | * |
||
716 | * @access public |
||
717 | * |
||
718 | * @param int $batch_size The batch size for objects. |
||
719 | * @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
||
720 | * |
||
721 | * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found. |
||
722 | */ |
||
723 | public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) { |
||
726 | } |
||
727 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.