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 SAL_Site 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 SAL_Site, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class SAL_Site { |
||
16 | public $blog_id; |
||
17 | public $platform; |
||
18 | |||
19 | public function __construct( $blog_id, $platform ) { |
||
20 | $this->blog_id = $blog_id; |
||
21 | $this->platform = $platform; |
||
22 | } |
||
23 | |||
24 | public function get_id() { |
||
25 | return $this->blog_id; |
||
26 | } |
||
27 | |||
28 | public function get_name() { |
||
29 | return (string) htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); |
||
30 | } |
||
31 | |||
32 | public function get_description() { |
||
33 | return (string) htmlspecialchars_decode( get_bloginfo( 'description' ), ENT_QUOTES ); |
||
34 | } |
||
35 | |||
36 | public function get_url() { |
||
37 | return (string) home_url(); |
||
38 | } |
||
39 | |||
40 | public function get_post_count() { |
||
41 | return (int) wp_count_posts( 'post' )->publish; |
||
42 | } |
||
43 | |||
44 | public function get_quota() { |
||
45 | return null; |
||
46 | } |
||
47 | |||
48 | abstract public function has_videopress(); |
||
49 | |||
50 | abstract public function upgraded_filetypes_enabled(); |
||
51 | |||
52 | abstract public function is_mapped_domain(); |
||
53 | |||
54 | abstract public function is_redirect(); |
||
55 | |||
56 | abstract public function is_headstart_fresh(); |
||
57 | |||
58 | abstract public function featured_images_enabled(); |
||
59 | |||
60 | abstract public function has_wordads(); |
||
61 | |||
62 | abstract public function get_frame_nonce(); |
||
63 | |||
64 | abstract public function allowed_file_types(); |
||
65 | |||
66 | abstract public function get_post_formats(); |
||
67 | |||
68 | abstract public function is_private(); |
||
69 | |||
70 | abstract public function is_following(); |
||
71 | |||
72 | abstract public function get_subscribers_count(); |
||
73 | |||
74 | abstract public function get_locale(); |
||
75 | |||
76 | abstract public function is_jetpack(); |
||
77 | |||
78 | abstract public function get_jetpack_modules(); |
||
79 | |||
80 | abstract public function is_vip(); |
||
81 | |||
82 | abstract public function is_multisite(); |
||
83 | |||
84 | abstract public function is_single_user_site(); |
||
85 | |||
86 | abstract public function get_plan(); |
||
87 | |||
88 | abstract public function get_ak_vp_bundle_enabled(); |
||
89 | |||
90 | abstract public function get_podcasting_archive(); |
||
91 | |||
92 | abstract public function get_jetpack_seo_front_page_description(); |
||
93 | |||
94 | abstract public function get_jetpack_seo_title_formats(); |
||
95 | |||
96 | abstract public function get_verification_services_codes(); |
||
97 | |||
98 | abstract public function before_render(); |
||
99 | |||
100 | abstract public function after_render( &$response ); |
||
101 | |||
102 | // TODO - factor this out? Seems an odd thing to have on a site |
||
103 | abstract public function after_render_options( &$options ); |
||
104 | |||
105 | // wrap a WP_Post object with SAL methods |
||
106 | abstract public function wrap_post( $post, $context ); |
||
107 | |||
108 | abstract protected function is_a8c_publication( $post_id ); |
||
109 | |||
110 | public function is_automated_transfer() { |
||
111 | /** |
||
112 | * Filter if a site is an automated-transfer site. |
||
113 | * |
||
114 | * @module json-api |
||
115 | * |
||
116 | * @since 6.4.0 |
||
127 | |||
128 | public function is_wpcom_store() { |
||
131 | |||
132 | public function woocommerce_is_active() { |
||
135 | |||
136 | public function get_post_by_id( $post_id, $context ) { |
||
152 | |||
153 | /** |
||
154 | * Validate current user can access the post |
||
155 | * |
||
156 | * @return WP_Error or post |
||
157 | */ |
||
158 | private function validate_access( $post ) { |
||
186 | |||
187 | View Code Duplication | public function current_user_can_access_post_type( $post_type, $context ) { |
|
202 | |||
203 | protected function get_post_type_object( $post_type ) { |
||
206 | |||
207 | // copied from class.json-api-endpoints.php |
||
208 | View Code Duplication | public function is_post_type_allowed( $post_type ) { |
|
235 | |||
236 | // copied from class.json-api-endpoints.php |
||
237 | /** |
||
238 | * Gets the whitelisted post types that JP should allow access to. |
||
239 | * |
||
240 | * @return array Whitelisted post types. |
||
241 | */ |
||
242 | View Code Duplication | public function get_whitelisted_post_types() { |
|
258 | |||
259 | // copied and modified a little from class.json-api-endpoints.php |
||
260 | private function user_can_view_post( $post ) { |
||
315 | |||
316 | /** |
||
317 | * Get post ID by name |
||
318 | * |
||
319 | * Attempts to match name on post title and page path |
||
320 | * |
||
321 | * @param string $name |
||
322 | * |
||
323 | * @return int|object Post ID on success, WP_Error object on failure |
||
324 | */ |
||
325 | public function get_post_id_by_name( $name ) { |
||
350 | |||
351 | /** |
||
352 | * Get post by name |
||
353 | * |
||
354 | * Attempts to match name on post title and page path |
||
355 | * |
||
356 | * @param string $name |
||
357 | * @param string $context (display or edit) |
||
358 | * |
||
359 | * @return object Post object on success, WP_Error object on failure |
||
360 | **/ |
||
361 | public function get_post_by_name( $name, $context ) { |
||
369 | |||
370 | function user_can_manage() { |
||
373 | |||
374 | function get_xmlrpc_url() { |
||
378 | |||
379 | function get_registered_date() { |
||
389 | |||
390 | function get_capabilities() { |
||
413 | |||
414 | function is_visible() { |
||
430 | |||
431 | function get_logo() { |
||
455 | |||
456 | function get_timezone() { |
||
459 | |||
460 | function get_gmt_offset() { |
||
463 | |||
464 | function get_login_url() { |
||
467 | |||
468 | function get_admin_url() { |
||
471 | |||
472 | function get_unmapped_url() { |
||
475 | |||
476 | function get_theme_slug() { |
||
479 | |||
480 | function get_header_image() { |
||
483 | |||
484 | function get_background_color() { |
||
487 | |||
488 | function get_image_default_link_type() { |
||
491 | |||
492 | function get_image_thumbnail_width() { |
||
495 | |||
496 | function get_image_thumbnail_height() { |
||
499 | |||
500 | function get_image_thumbnail_crop() { |
||
503 | |||
504 | function get_image_medium_width() { |
||
507 | |||
508 | function get_image_medium_height() { |
||
511 | |||
512 | function get_image_large_width() { |
||
515 | |||
516 | function get_image_large_height() { |
||
519 | |||
520 | function get_permalink_structure() { |
||
523 | |||
524 | function get_default_post_format() { |
||
527 | |||
528 | function get_default_category() { |
||
531 | |||
532 | function get_show_on_front() { |
||
535 | |||
536 | function is_custom_front_page() { |
||
539 | |||
540 | function get_default_likes_enabled() { |
||
543 | |||
544 | function get_default_sharing_status() { |
||
553 | |||
554 | function get_default_comment_status() { |
||
557 | |||
558 | function default_ping_status() { |
||
561 | |||
562 | function is_publicize_permanently_disabled() { |
||
569 | |||
570 | function get_page_on_front() { |
||
573 | |||
574 | function get_page_for_posts() { |
||
577 | |||
578 | function is_headstart() { |
||
581 | |||
582 | function get_wordpress_version() { |
||
586 | |||
587 | function is_domain_only() { |
||
591 | |||
592 | function get_blog_public() { |
||
595 | |||
596 | function has_pending_automated_transfer() { |
||
613 | |||
614 | function signup_is_store() { |
||
617 | |||
618 | function get_roles() { |
||
621 | |||
622 | function get_design_type() { |
||
626 | |||
627 | function get_site_goals() { |
||
631 | |||
632 | function get_launch_status() { |
||
635 | } |
||
636 |