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|bool |
||
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 ) { |
||
135 | |||
136 | /** |
||
137 | * Before Akismet's daily cleanup of spam detection metadata. |
||
138 | * |
||
139 | * @access public |
||
140 | * |
||
141 | * @param array $feedback_ids IDs of feedback posts. |
||
142 | */ |
||
143 | public function daily_akismet_meta_cleanup_before( $feedback_ids ) { |
||
157 | |||
158 | /** |
||
159 | * After Akismet's daily cleanup of spam detection metadata. |
||
160 | * |
||
161 | * @access public |
||
162 | * |
||
163 | * @param array $feedback_ids IDs of feedback posts. |
||
164 | */ |
||
165 | public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
168 | |||
169 | /** |
||
170 | * Initialize posts action listeners for full sync. |
||
171 | * |
||
172 | * @access public |
||
173 | * |
||
174 | * @param callable $callable Action handler callable. |
||
175 | */ |
||
176 | public function init_full_sync_listeners( $callable ) { |
||
179 | |||
180 | /** |
||
181 | * Initialize the module in the sender. |
||
182 | * |
||
183 | * @access public |
||
184 | */ |
||
185 | public function init_before_send() { |
||
191 | |||
192 | /** |
||
193 | * Enqueue the posts actions for full sync. |
||
194 | * |
||
195 | * @access public |
||
196 | * |
||
197 | * @param array $config Full sync configuration for this sync module. |
||
198 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
199 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
200 | * @return array Number of actions enqueued, and next module state. |
||
201 | */ |
||
202 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
207 | |||
208 | /** |
||
209 | * Retrieve an estimated number of actions that will be enqueued. |
||
210 | * |
||
211 | * @access public |
||
212 | * |
||
213 | * @todo Use $wpdb->prepare for the SQL query. |
||
214 | * |
||
215 | * @param array $config Full sync configuration for this sync module. |
||
216 | * @return array Number of items yet to be enqueued. |
||
217 | */ |
||
218 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
227 | |||
228 | /** |
||
229 | * Retrieve the WHERE SQL clause based on the module config. |
||
230 | * |
||
231 | * @access public |
||
232 | * |
||
233 | * @param array $config Full sync configuration for this sync module. |
||
234 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
235 | */ |
||
236 | View Code Duplication | public function get_where_sql( $config ) { |
|
246 | |||
247 | /** |
||
248 | * Retrieve the actions that will be sent for this module during a full sync. |
||
249 | * |
||
250 | * @access public |
||
251 | * |
||
252 | * @return array Full sync actions of this module. |
||
253 | */ |
||
254 | public function get_full_sync_actions() { |
||
257 | |||
258 | /** |
||
259 | * Process content before send. |
||
260 | * |
||
261 | * @param array $args Arguments of the `wp_insert_post` hook. |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | public function expand_jetpack_sync_save_post( $args ) { |
||
269 | |||
270 | /** |
||
271 | * Filter all blacklisted post types. |
||
272 | * |
||
273 | * @param array $args Hook arguments. |
||
274 | * @return array|false Hook arguments, or false if the post type is a blacklisted one. |
||
275 | */ |
||
276 | public function filter_blacklisted_post_types( $args ) { |
||
285 | |||
286 | /** |
||
287 | * Filter all meta that is not blacklisted, or is stored for a disallowed post type. |
||
288 | * |
||
289 | * @param array $args Hook arguments. |
||
290 | * @return array|false Hook arguments, or false if meta was filtered. |
||
291 | */ |
||
292 | public function filter_meta( $args ) { |
||
299 | |||
300 | /** |
||
301 | * Whether a post meta key is whitelisted. |
||
302 | * |
||
303 | * @param string $meta_key Meta key. |
||
304 | * @return boolean Whether the post meta key is whitelisted. |
||
305 | */ |
||
306 | public function is_whitelisted_post_meta( $meta_key ) { |
||
310 | |||
311 | /** |
||
312 | * Whether a post type is allowed. |
||
313 | * A post type will be disallowed if it's present in the post type blacklist. |
||
314 | * |
||
315 | * @param int $post_id ID of the post. |
||
316 | * @return boolean Whether the post type is allowed. |
||
317 | */ |
||
318 | public function is_post_type_allowed( $post_id ) { |
||
325 | |||
326 | /** |
||
327 | * Remove the embed shortcode. |
||
328 | * |
||
329 | * @global $wp_embed |
||
330 | */ |
||
331 | View Code Duplication | public function remove_embed() { |
|
339 | |||
340 | /** |
||
341 | * Add the embed shortcode. |
||
342 | * |
||
343 | * @global $wp_embed |
||
344 | */ |
||
345 | View Code Duplication | public function add_embed() { |
|
353 | |||
354 | /** |
||
355 | * Expands wp_insert_post to include filtered content |
||
356 | * |
||
357 | * @param \WP_Post $post_object Post object. |
||
358 | */ |
||
359 | public function filter_post_content_and_add_links( $post_object ) { |
||
464 | |||
465 | /** |
||
466 | * Handle transition from another post status to a published one. |
||
467 | * |
||
468 | * @param string $new_status New post status. |
||
469 | * @param string $old_status Old post status. |
||
470 | * @param \WP_Post $post Post object. |
||
471 | */ |
||
472 | public function save_published( $new_status, $old_status, $post ) { |
||
479 | |||
480 | /** |
||
481 | * When publishing or updating a post, the Gutenberg editor sends two requests: |
||
482 | * 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id` |
||
483 | * 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1` |
||
484 | * |
||
485 | * The 2nd request is to update post meta, which is not supported on WP REST API. |
||
486 | * When syncing post data, we will include if this was a meta box update. |
||
487 | * |
||
488 | * @todo Implement nonce verification. |
||
489 | * |
||
490 | * @return boolean Whether this is a Gutenberg meta box update. |
||
491 | */ |
||
492 | public function is_gutenberg_meta_box_update() { |
||
502 | |||
503 | /** |
||
504 | * Handler for the wp_insert_post hook. |
||
505 | * Called upon creation of a new post. |
||
506 | * |
||
507 | * @param int $post_ID Post ID. |
||
|
|||
508 | * @param \WP_Post $post Post object. |
||
509 | * @param boolean $update Whether this is an existing post being updated or not. |
||
510 | */ |
||
511 | public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
||
551 | |||
552 | /** |
||
553 | * Send a published post for sync. |
||
554 | * |
||
555 | * @param int $post_ID Post ID. |
||
556 | * @param \WP_Post $post Post object. |
||
557 | */ |
||
558 | public function send_published( $post_ID, $post ) { |
||
633 | |||
634 | /** |
||
635 | * Expand post IDs to post objects within a hook before they are serialized and sent to the server. |
||
636 | * |
||
637 | * @access public |
||
638 | * |
||
639 | * @param array $args The hook parameters. |
||
640 | * @return array $args The expanded hook parameters. |
||
641 | */ |
||
642 | public function expand_post_ids( $args ) { |
||
656 | |||
657 | /** |
||
658 | * Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
||
659 | * |
||
660 | * @access public |
||
661 | * |
||
662 | * @param int $batch_size The batch size for objects. |
||
663 | * @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
||
664 | * @param bool $distinct True if we should only look at distinct object ids. |
||
665 | * |
||
666 | * @return array|bool An array of min and max ids for each batch. |
||
667 | */ |
||
668 | public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false, $distinct = false ) { |
||
671 | } |
||
672 |
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.