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 | * An estimate of how many rows per second can be synced during a full sync. |
||
20 | * |
||
21 | * @access static |
||
22 | * |
||
23 | * @var int|null Null if speed is not important in a full sync. |
||
24 | */ |
||
25 | static $sync_speed = 7; |
||
26 | /** |
||
27 | * The post IDs of posts that were just published but not synced yet. |
||
28 | * |
||
29 | * @access private |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $just_published = array(); |
||
34 | |||
35 | /** |
||
36 | * The previous status of posts that we use for calculating post status transitions. |
||
37 | * |
||
38 | * @access private |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $previous_status = array(); |
||
43 | |||
44 | /** |
||
45 | * Action handler callable. |
||
46 | * |
||
47 | * @access private |
||
48 | * |
||
49 | * @var callable |
||
50 | */ |
||
51 | private $action_handler; |
||
52 | |||
53 | /** |
||
54 | * Import end. |
||
55 | * |
||
56 | * @access private |
||
57 | * |
||
58 | * @todo This appears to be unused - let's remove it. |
||
59 | * |
||
60 | * @var boolean |
||
61 | */ |
||
62 | private $import_end = false; |
||
63 | |||
64 | /** |
||
65 | * Default previous post state. |
||
66 | * Used for default previous post status. |
||
67 | * |
||
68 | * @access public |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | const DEFAULT_PREVIOUS_STATE = 'new'; |
||
73 | |||
74 | /** |
||
75 | * Sync module name. |
||
76 | * |
||
77 | * @access public |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function name() { |
||
84 | |||
85 | /** |
||
86 | * The table in the database. |
||
87 | * |
||
88 | * @access public |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function table_name() { |
||
95 | |||
96 | /** |
||
97 | * Retrieve a post by its ID. |
||
98 | * |
||
99 | * @access public |
||
100 | * |
||
101 | * @param string $object_type Type of the sync object. |
||
102 | * @param int $id ID of the sync object. |
||
103 | * @return \WP_Post|bool Filtered \WP_Post object, or false if the object is not a post. |
||
104 | */ |
||
105 | public function get_object_by_id( $object_type, $id ) { |
||
115 | |||
116 | /** |
||
117 | * Initialize posts action listeners. |
||
118 | * |
||
119 | * @access public |
||
120 | * |
||
121 | * @param callable $callable Action handler callable. |
||
122 | */ |
||
123 | public function init_listeners( $callable ) { |
||
143 | |||
144 | /** |
||
145 | * Before Akismet's daily cleanup of spam detection metadata. |
||
146 | * |
||
147 | * @access public |
||
148 | * |
||
149 | * @param array $feedback_ids IDs of feedback posts. |
||
150 | */ |
||
151 | public function daily_akismet_meta_cleanup_before( $feedback_ids ) { |
||
165 | |||
166 | /** |
||
167 | * After Akismet's daily cleanup of spam detection metadata. |
||
168 | * |
||
169 | * @access public |
||
170 | * |
||
171 | * @param array $feedback_ids IDs of feedback posts. |
||
172 | */ |
||
173 | public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
176 | |||
177 | /** |
||
178 | * Initialize posts action listeners for full sync. |
||
179 | * |
||
180 | * @access public |
||
181 | * |
||
182 | * @param callable $callable Action handler callable. |
||
183 | */ |
||
184 | public function init_full_sync_listeners( $callable ) { |
||
187 | |||
188 | /** |
||
189 | * Initialize the module in the sender. |
||
190 | * |
||
191 | * @access public |
||
192 | */ |
||
193 | public function init_before_send() { |
||
199 | |||
200 | /** |
||
201 | * Enqueue the posts actions for full sync. |
||
202 | * |
||
203 | * @access public |
||
204 | * |
||
205 | * @param array $config Full sync configuration for this sync module. |
||
206 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
207 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
208 | * @return array Number of actions enqueued, and next module state. |
||
209 | */ |
||
210 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
215 | |||
216 | /** |
||
217 | * Retrieve an estimated number of actions that will be enqueued. |
||
218 | * |
||
219 | * @access public |
||
220 | * |
||
221 | * @todo Use $wpdb->prepare for the SQL query. |
||
222 | * |
||
223 | * @param array $config Full sync configuration for this sync module. |
||
224 | * @return array Number of items yet to be enqueued. |
||
225 | */ |
||
226 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
235 | |||
236 | /** |
||
237 | * Retrieve the WHERE SQL clause based on the module config. |
||
238 | * |
||
239 | * @access public |
||
240 | * |
||
241 | * @param array $config Full sync configuration for this sync module. |
||
242 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
243 | */ |
||
244 | View Code Duplication | public function get_where_sql( $config ) { |
|
254 | |||
255 | /** |
||
256 | * Retrieve the actions that will be sent for this module during a full sync. |
||
257 | * |
||
258 | * @access public |
||
259 | * |
||
260 | * @return array Full sync actions of this module. |
||
261 | */ |
||
262 | public function get_full_sync_actions() { |
||
265 | |||
266 | /** |
||
267 | * Process content before send. |
||
268 | * |
||
269 | * @param array $args Arguments of the `wp_insert_post` hook. |
||
270 | * |
||
271 | * @return array |
||
272 | */ |
||
273 | public function expand_jetpack_sync_save_post( $args ) { |
||
277 | |||
278 | /** |
||
279 | * Filter all blacklisted post types. |
||
280 | * |
||
281 | * @param array $args Hook arguments. |
||
282 | * @return array|false Hook arguments, or false if the post type is a blacklisted one. |
||
283 | */ |
||
284 | public function filter_blacklisted_post_types( $args ) { |
||
293 | |||
294 | /** |
||
295 | * Filter all meta that is not blacklisted, or is stored for a disallowed post type. |
||
296 | * |
||
297 | * @param array $args Hook arguments. |
||
298 | * @return array|false Hook arguments, or false if meta was filtered. |
||
299 | */ |
||
300 | public function filter_meta( $args ) { |
||
307 | |||
308 | /** |
||
309 | * Whether a post meta key is whitelisted. |
||
310 | * |
||
311 | * @param string $meta_key Meta key. |
||
312 | * @return boolean Whether the post meta key is whitelisted. |
||
313 | */ |
||
314 | public function is_whitelisted_post_meta( $meta_key ) { |
||
318 | |||
319 | /** |
||
320 | * Whether a post type is allowed. |
||
321 | * A post type will be disallowed if it's present in the post type blacklist. |
||
322 | * |
||
323 | * @param int $post_id ID of the post. |
||
324 | * @return boolean Whether the post type is allowed. |
||
325 | */ |
||
326 | public function is_post_type_allowed( $post_id ) { |
||
334 | |||
335 | /** |
||
336 | * Remove the embed shortcode. |
||
337 | * |
||
338 | * @global $wp_embed |
||
339 | */ |
||
340 | View Code Duplication | public function remove_embed() { |
|
348 | |||
349 | /** |
||
350 | * Add the embed shortcode. |
||
351 | * |
||
352 | * @global $wp_embed |
||
353 | */ |
||
354 | View Code Duplication | public function add_embed() { |
|
362 | |||
363 | /** |
||
364 | * Expands wp_insert_post to include filtered content |
||
365 | * |
||
366 | * @param \WP_Post $post_object Post object. |
||
367 | */ |
||
368 | public function filter_post_content_and_add_links( $post_object ) { |
||
473 | |||
474 | /** |
||
475 | * Handle transition from another post status to a published one. |
||
476 | * |
||
477 | * @param string $new_status New post status. |
||
478 | * @param string $old_status Old post status. |
||
479 | * @param \WP_Post $post Post object. |
||
480 | */ |
||
481 | public function save_published( $new_status, $old_status, $post ) { |
||
488 | |||
489 | /** |
||
490 | * When publishing or updating a post, the Gutenberg editor sends two requests: |
||
491 | * 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id` |
||
492 | * 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1` |
||
493 | * |
||
494 | * The 2nd request is to update post meta, which is not supported on WP REST API. |
||
495 | * When syncing post data, we will include if this was a meta box update. |
||
496 | * |
||
497 | * @todo Implement nonce verification. |
||
498 | * |
||
499 | * @return boolean Whether this is a Gutenberg meta box update. |
||
500 | */ |
||
501 | public function is_gutenberg_meta_box_update() { |
||
511 | |||
512 | /** |
||
513 | * Handler for the wp_insert_post hook. |
||
514 | * Called upon creation of a new post. |
||
515 | * |
||
516 | * @param int $post_ID Post ID. |
||
|
|||
517 | * @param \WP_Post $post Post object. |
||
518 | * @param boolean $update Whether this is an existing post being updated or not. |
||
519 | */ |
||
520 | public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
||
560 | |||
561 | /** |
||
562 | * Send a published post for sync. |
||
563 | * |
||
564 | * @param int $post_ID Post ID. |
||
565 | * @param \WP_Post $post Post object. |
||
566 | */ |
||
567 | public function send_published( $post_ID, $post ) { |
||
642 | |||
643 | /** |
||
644 | * Expand post IDs to post objects within a hook before they are serialized and sent to the server. |
||
645 | * |
||
646 | * @access public |
||
647 | * |
||
648 | * @param array $args The hook parameters. |
||
649 | * @return array $args The expanded hook parameters. |
||
650 | */ |
||
651 | public function expand_post_ids( $args ) { |
||
665 | |||
666 | /** |
||
667 | * Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
||
668 | * |
||
669 | * @access public |
||
670 | * |
||
671 | * @param int $batch_size The batch size for objects. |
||
672 | * @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
||
673 | * |
||
674 | * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found. |
||
675 | */ |
||
676 | public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) { |
||
679 | |||
680 | /** |
||
681 | * Gets the sync speed of a module. |
||
682 | * |
||
683 | * @access public |
||
684 | * |
||
685 | * @return int |
||
686 | */ |
||
687 | public function get_sync_speed() { |
||
690 | } |
||
691 |
This check looks for
@param
annotations 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.