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_module_active( $module ); |
||
81 | |||
82 | abstract public function is_vip(); |
||
83 | |||
84 | abstract public function is_multisite(); |
||
85 | |||
86 | abstract public function is_single_user_site(); |
||
87 | |||
88 | abstract public function get_plan(); |
||
89 | |||
90 | abstract public function get_ak_vp_bundle_enabled(); |
||
91 | |||
92 | abstract public function get_podcasting_archive(); |
||
93 | |||
94 | abstract public function get_jetpack_seo_front_page_description(); |
||
95 | |||
96 | abstract public function get_jetpack_seo_title_formats(); |
||
97 | |||
98 | abstract public function get_verification_services_codes(); |
||
99 | |||
100 | abstract public function before_render(); |
||
101 | |||
102 | abstract public function after_render( &$response ); |
||
103 | |||
104 | // TODO - factor this out? Seems an odd thing to have on a site |
||
105 | abstract public function after_render_options( &$options ); |
||
106 | |||
107 | // wrap a WP_Post object with SAL methods |
||
108 | abstract public function wrap_post( $post, $context ); |
||
109 | |||
110 | abstract protected function is_a8c_publication( $post_id ); |
||
111 | |||
112 | public function is_automated_transfer() { |
||
129 | |||
130 | public function is_wpcom_atomic() { |
||
133 | |||
134 | public function is_wpcom_store() { |
||
137 | |||
138 | public function woocommerce_is_active() { |
||
141 | |||
142 | public function get_post_by_id( $post_id, $context ) { |
||
158 | |||
159 | /** |
||
160 | * Validate current user can access the post |
||
161 | * |
||
162 | * @return WP_Error or post |
||
163 | */ |
||
164 | private function validate_access( $post ) { |
||
192 | |||
193 | View Code Duplication | public function current_user_can_access_post_type( $post_type, $context ) { |
|
208 | |||
209 | protected function get_post_type_object( $post_type ) { |
||
212 | |||
213 | // copied from class.json-api-endpoints.php |
||
214 | View Code Duplication | public function is_post_type_allowed( $post_type ) { |
|
241 | |||
242 | // copied from class.json-api-endpoints.php |
||
243 | /** |
||
244 | * Gets the whitelisted post types that JP should allow access to. |
||
245 | * |
||
246 | * @return array Whitelisted post types. |
||
247 | */ |
||
248 | View Code Duplication | public function get_whitelisted_post_types() { |
|
264 | |||
265 | // copied and modified a little from class.json-api-endpoints.php |
||
266 | private function user_can_view_post( $post ) { |
||
321 | |||
322 | /** |
||
323 | * Get post ID by name |
||
324 | * |
||
325 | * Attempts to match name on post title and page path |
||
326 | * |
||
327 | * @param string $name |
||
328 | * |
||
329 | * @return int|object Post ID on success, WP_Error object on failure |
||
330 | */ |
||
331 | public function get_post_id_by_name( $name ) { |
||
356 | |||
357 | /** |
||
358 | * Get post by name |
||
359 | * |
||
360 | * Attempts to match name on post title and page path |
||
361 | * |
||
362 | * @param string $name |
||
363 | * @param string $context (display or edit) |
||
364 | * |
||
365 | * @return object Post object on success, WP_Error object on failure |
||
366 | **/ |
||
367 | public function get_post_by_name( $name, $context ) { |
||
375 | |||
376 | function user_can_manage() { |
||
379 | |||
380 | function get_xmlrpc_url() { |
||
384 | |||
385 | function get_registered_date() { |
||
395 | |||
396 | function get_capabilities() { |
||
419 | |||
420 | function is_visible() { |
||
436 | |||
437 | function get_logo() { |
||
461 | |||
462 | function get_timezone() { |
||
465 | |||
466 | function get_gmt_offset() { |
||
469 | |||
470 | function get_login_url() { |
||
473 | |||
474 | function get_admin_url() { |
||
477 | |||
478 | function get_unmapped_url() { |
||
481 | |||
482 | function get_theme_slug() { |
||
485 | |||
486 | function get_header_image() { |
||
489 | |||
490 | function get_background_color() { |
||
493 | |||
494 | function get_image_default_link_type() { |
||
497 | |||
498 | function get_image_thumbnail_width() { |
||
501 | |||
502 | function get_image_thumbnail_height() { |
||
505 | |||
506 | function get_image_thumbnail_crop() { |
||
509 | |||
510 | function get_image_medium_width() { |
||
513 | |||
514 | function get_image_medium_height() { |
||
517 | |||
518 | function get_image_large_width() { |
||
521 | |||
522 | function get_image_large_height() { |
||
525 | |||
526 | function get_permalink_structure() { |
||
529 | |||
530 | function get_default_post_format() { |
||
533 | |||
534 | function get_default_category() { |
||
537 | |||
538 | function get_show_on_front() { |
||
541 | |||
542 | function is_custom_front_page() { |
||
545 | |||
546 | function get_default_likes_enabled() { |
||
549 | |||
550 | function get_default_sharing_status() { |
||
559 | |||
560 | function get_default_comment_status() { |
||
563 | |||
564 | function default_ping_status() { |
||
567 | |||
568 | function is_publicize_permanently_disabled() { |
||
575 | |||
576 | function get_page_on_front() { |
||
579 | |||
580 | function get_page_for_posts() { |
||
583 | |||
584 | function is_headstart() { |
||
587 | |||
588 | function get_wordpress_version() { |
||
592 | |||
593 | function is_domain_only() { |
||
597 | |||
598 | function get_blog_public() { |
||
601 | |||
602 | function has_pending_automated_transfer() { |
||
619 | |||
620 | function signup_is_store() { |
||
623 | |||
624 | function get_roles() { |
||
627 | |||
628 | function get_design_type() { |
||
632 | |||
633 | function get_site_goals() { |
||
637 | |||
638 | function get_launch_status() { |
||
641 | |||
642 | function get_site_segment() { |
||
645 | } |
||
646 |