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 ) { |
||
23 | |||
24 | public function get_id() { |
||
27 | |||
28 | public function get_name() { |
||
31 | |||
32 | public function get_description() { |
||
35 | |||
36 | public function get_url() { |
||
39 | |||
40 | public function get_post_count() { |
||
43 | |||
44 | public function get_quota() { |
||
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() { |
||
113 | |||
114 | public function get_post_by_id( $post_id, $context ) { |
||
126 | |||
127 | /** |
||
128 | * Validate current user can access the post |
||
129 | * |
||
130 | * @return WP_Error or post |
||
131 | */ |
||
132 | private function validate_access( $post ) { |
||
160 | |||
161 | View Code Duplication | public function current_user_can_access_post_type( $post_type, $context ) { |
|
176 | |||
177 | protected function get_post_type_object( $post_type ) { |
||
180 | |||
181 | // copied from class.json-api-endpoints.php |
||
182 | public function is_post_type_allowed( $post_type ) { |
||
197 | |||
198 | // copied from class.json-api-endpoints.php |
||
199 | /** |
||
200 | * Gets the whitelisted post types that JP should allow access to. |
||
201 | * |
||
202 | * @return array Whitelisted post types. |
||
203 | */ |
||
204 | View Code Duplication | public function get_whitelisted_post_types() { |
|
220 | |||
221 | // copied and modified a little from class.json-api-endpoints.php |
||
222 | private function user_can_view_post( $post ) { |
||
277 | |||
278 | /** |
||
279 | * Get post ID by name |
||
280 | * |
||
281 | * Attempts to match name on post title and page path |
||
282 | * |
||
283 | * @param string $name |
||
284 | * |
||
285 | * @return int|object Post ID on success, WP_Error object on failure |
||
286 | */ |
||
287 | public function get_post_id_by_name( $name ) { |
||
312 | |||
313 | /** |
||
314 | * Get post by name |
||
315 | * |
||
316 | * Attempts to match name on post title and page path |
||
317 | * |
||
318 | * @param string $name |
||
319 | * @param string $context (display or edit) |
||
320 | * |
||
321 | * @return object Post object on success, WP_Error object on failure |
||
322 | **/ |
||
323 | public function get_post_by_name( $name, $context ) { |
||
331 | |||
332 | function user_can_manage() { |
||
335 | |||
336 | function get_xmlrpc_url() { |
||
340 | |||
341 | function get_registered_date() { |
||
351 | |||
352 | function get_capabilities() { |
||
374 | |||
375 | function is_visible() { |
||
391 | |||
392 | function get_logo() { |
||
416 | |||
417 | function get_timezone() { |
||
420 | |||
421 | function get_gmt_offset() { |
||
424 | |||
425 | function get_login_url() { |
||
428 | |||
429 | function get_admin_url() { |
||
432 | |||
433 | function get_unmapped_url() { |
||
436 | |||
437 | function get_theme_slug() { |
||
440 | |||
441 | function get_header_image() { |
||
444 | |||
445 | function get_background_color() { |
||
448 | |||
449 | function get_image_default_link_type() { |
||
452 | |||
453 | function get_image_thumbnail_width() { |
||
456 | |||
457 | function get_image_thumbnail_height() { |
||
460 | |||
461 | function get_image_thumbnail_crop() { |
||
464 | |||
465 | function get_image_medium_width() { |
||
468 | |||
469 | function get_image_medium_height() { |
||
472 | |||
473 | function get_image_large_width() { |
||
476 | |||
477 | function get_image_large_height() { |
||
480 | |||
481 | function get_permalink_structure() { |
||
484 | |||
485 | function get_default_post_format() { |
||
488 | |||
489 | function get_default_category() { |
||
492 | |||
493 | function get_show_on_front() { |
||
496 | |||
497 | function is_custom_front_page() { |
||
500 | |||
501 | function get_default_likes_enabled() { |
||
504 | |||
505 | function get_default_sharing_status() { |
||
514 | |||
515 | function get_default_comment_status() { |
||
518 | |||
519 | function default_ping_status() { |
||
522 | |||
523 | function is_publicize_permanently_disabled() { |
||
530 | |||
531 | function get_page_on_front() { |
||
534 | |||
535 | function get_page_for_posts() { |
||
538 | |||
539 | function is_headstart() { |
||
542 | |||
543 | function get_wordpress_version() { |
||
547 | |||
548 | function is_domain_only() { |
||
552 | |||
553 | function get_blog_public() { |
||
556 | } |
||
557 |