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 |
||
| 22 | abstract class SAL_Site { |
||
| 23 | public $blog_id; |
||
| 24 | public $platform; |
||
| 25 | |||
| 26 | public function __construct( $blog_id, $platform ) { |
||
| 30 | |||
| 31 | public function get_id() { |
||
| 34 | |||
| 35 | public function get_name() { |
||
| 38 | |||
| 39 | public function get_description() { |
||
| 42 | |||
| 43 | public function get_url() { |
||
| 46 | |||
| 47 | public function get_post_count() { |
||
| 50 | |||
| 51 | abstract public function has_videopress(); |
||
| 52 | |||
| 53 | abstract public function upgraded_filetypes_enabled(); |
||
| 54 | |||
| 55 | abstract public function is_mapped_domain(); |
||
| 56 | |||
| 57 | abstract public function is_redirect(); |
||
| 58 | |||
| 59 | abstract public function featured_images_enabled(); |
||
| 60 | |||
| 61 | abstract public function has_wordads(); |
||
| 62 | |||
| 63 | abstract public function get_frame_nonce(); |
||
| 64 | |||
| 65 | abstract public function allowed_file_types(); |
||
| 66 | |||
| 67 | abstract public function get_post_formats(); |
||
| 68 | |||
| 69 | abstract public function is_private(); |
||
| 70 | |||
| 71 | abstract public function is_following(); |
||
| 72 | |||
| 73 | abstract public function get_subscribers_count(); |
||
| 74 | |||
| 75 | abstract public function get_locale(); |
||
| 76 | |||
| 77 | abstract public function is_jetpack(); |
||
| 78 | |||
| 79 | abstract public function get_jetpack_modules(); |
||
| 80 | |||
| 81 | abstract public function is_vip(); |
||
| 82 | |||
| 83 | abstract public function is_multisite(); |
||
| 84 | |||
| 85 | abstract public function is_single_user_site(); |
||
| 86 | |||
| 87 | abstract public function get_plan(); |
||
| 88 | |||
| 89 | abstract public function get_ak_vp_bundle_enabled(); |
||
| 90 | |||
| 91 | abstract public function before_render(); |
||
| 92 | |||
| 93 | abstract public function after_render( &$response ); |
||
| 94 | |||
| 95 | // TODO - factor this out? Seems an odd thing to have on a site |
||
|
|
|||
| 96 | abstract public function after_render_options( &$options ); |
||
| 97 | |||
| 98 | // wrap a WP_Post object with SAL methods |
||
| 99 | abstract public function wrap_post( $post, $context ); |
||
| 100 | |||
| 101 | |||
| 102 | public function get_post_by_id( $post_id, $context ) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Validate current user can access the post |
||
| 117 | * |
||
| 118 | * @return WP_Error or post |
||
| 119 | */ |
||
| 120 | private function validate_access( $post ) { |
||
| 121 | $context = $post->context; |
||
| 122 | |||
| 123 | View Code Duplication | if ( ! $this->is_post_type_allowed( $post->post_type ) |
|
| 124 | && |
||
| 125 | ( ! function_exists( 'is_post_freshly_pressed' ) || ! is_post_freshly_pressed( $post->ID ) ) ) { |
||
| 126 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
||
| 127 | } |
||
| 128 | |||
| 129 | switch ( $context ) { |
||
| 130 | case 'edit' : |
||
| 131 | if ( ! current_user_can( 'edit_post', $post ) ) { |
||
| 132 | return new WP_Error( 'unauthorized', 'User cannot edit post', 403 ); |
||
| 133 | } |
||
| 134 | break; |
||
| 135 | case 'display' : |
||
| 136 | $can_view = $this->user_can_view_post( $post ); |
||
| 137 | if ( is_wp_error( $can_view ) ) { |
||
| 138 | return $can_view; |
||
| 139 | } |
||
| 140 | break; |
||
| 141 | default : |
||
| 142 | return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 ); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $post; |
||
| 146 | } |
||
| 147 | |||
| 148 | // copied from class.json-api-endpoints.php |
||
| 149 | View Code Duplication | private function is_post_type_allowed( $post_type ) { |
|
| 164 | |||
| 165 | // copied from class.json-api-endpoints.php |
||
| 166 | /** |
||
| 167 | * Gets the whitelisted post types that JP should allow access to. |
||
| 168 | * |
||
| 169 | * @return array Whitelisted post types. |
||
| 170 | */ |
||
| 171 | View Code Duplication | private function _get_whitelisted_post_types() { |
|
| 187 | |||
| 188 | // copied and modified a little from class.json-api-endpoints.php |
||
| 189 | private function user_can_view_post( $post ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get post by name |
||
| 247 | * |
||
| 248 | * Attempts to match name on post title and page path |
||
| 249 | * |
||
| 250 | * @param string $name |
||
| 251 | * @param string $context (display or edit) |
||
| 252 | * |
||
| 253 | * @return int|object Post ID on success, WP_Error object on failure |
||
| 254 | **/ |
||
| 255 | View Code Duplication | public function get_post_by_name( $name, $context ) { |
|
| 278 | |||
| 279 | function user_can_manage() { |
||
| 282 | |||
| 283 | function get_xmlrpc_url() { |
||
| 287 | |||
| 288 | function get_registered_date() { |
||
| 298 | |||
| 299 | function get_capabilities() { |
||
| 318 | |||
| 319 | function is_visible() { |
||
| 335 | |||
| 336 | function get_logo() { |
||
| 360 | |||
| 361 | function get_timezone() { |
||
| 364 | |||
| 365 | function get_gmt_offset() { |
||
| 368 | |||
| 369 | function get_login_url() { |
||
| 372 | |||
| 373 | function get_admin_url() { |
||
| 376 | |||
| 377 | function get_unmapped_url() { |
||
| 380 | |||
| 381 | function get_theme_slug() { |
||
| 384 | |||
| 385 | function get_header_image() { |
||
| 388 | |||
| 389 | function get_background_color() { |
||
| 392 | |||
| 393 | function get_image_default_link_type() { |
||
| 396 | |||
| 397 | function get_image_thumbnail_width() { |
||
| 400 | |||
| 401 | function get_image_thumbnail_height() { |
||
| 404 | |||
| 405 | function get_image_thumbnail_crop() { |
||
| 408 | |||
| 409 | function get_image_medium_width() { |
||
| 412 | |||
| 413 | function get_image_medium_height() { |
||
| 416 | |||
| 417 | function get_image_large_width() { |
||
| 420 | |||
| 421 | function get_image_large_height() { |
||
| 424 | |||
| 425 | function get_permalink_structure() { |
||
| 428 | |||
| 429 | function get_default_post_format() { |
||
| 432 | |||
| 433 | function get_default_category() { |
||
| 436 | |||
| 437 | function get_show_on_front() { |
||
| 440 | |||
| 441 | function is_custom_front_page() { |
||
| 444 | |||
| 445 | function get_default_likes_enabled() { |
||
| 448 | |||
| 449 | function get_default_sharing_status() { |
||
| 450 | $default_sharing_status = false; |
||
| 451 | if ( class_exists( 'Sharing_Service' ) ) { |
||
| 452 | $ss = new Sharing_Service(); |
||
| 453 | $blog_services = $ss->get_blog_services(); |
||
| 454 | $default_sharing_status = ! empty( $blog_services['visible'] ); |
||
| 455 | } |
||
| 456 | return (bool) $default_sharing_status; |
||
| 457 | } |
||
| 458 | |||
| 459 | function get_default_comment_status() { |
||
| 462 | |||
| 463 | function default_ping_status() { |
||
| 466 | |||
| 467 | function is_publicize_permanently_disabled() { |
||
| 474 | |||
| 475 | function get_page_on_front() { |
||
| 478 | |||
| 479 | function get_page_for_posts() { |
||
| 482 | |||
| 483 | function is_headstart() { |
||
| 486 | |||
| 487 | function get_wordpress_version() { |
||
| 491 | } |