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 | abstract public function has_videopress(); |
||
| 45 | |||
| 46 | abstract public function upgraded_filetypes_enabled(); |
||
| 47 | |||
| 48 | abstract public function is_mapped_domain(); |
||
| 49 | |||
| 50 | abstract public function is_redirect(); |
||
| 51 | |||
| 52 | abstract public function is_headstart_fresh(); |
||
| 53 | |||
| 54 | abstract public function featured_images_enabled(); |
||
| 55 | |||
| 56 | abstract public function has_wordads(); |
||
| 57 | |||
| 58 | abstract public function get_frame_nonce(); |
||
| 59 | |||
| 60 | abstract public function allowed_file_types(); |
||
| 61 | |||
| 62 | abstract public function get_post_formats(); |
||
| 63 | |||
| 64 | abstract public function is_private(); |
||
| 65 | |||
| 66 | abstract public function is_following(); |
||
| 67 | |||
| 68 | abstract public function get_subscribers_count(); |
||
| 69 | |||
| 70 | abstract public function get_locale(); |
||
| 71 | |||
| 72 | abstract public function is_jetpack(); |
||
| 73 | |||
| 74 | abstract public function get_jetpack_modules(); |
||
| 75 | |||
| 76 | abstract public function is_vip(); |
||
| 77 | |||
| 78 | abstract public function is_multisite(); |
||
| 79 | |||
| 80 | abstract public function is_single_user_site(); |
||
| 81 | |||
| 82 | abstract public function get_plan(); |
||
| 83 | |||
| 84 | abstract public function get_ak_vp_bundle_enabled(); |
||
| 85 | |||
| 86 | abstract public function get_podcasting_archive(); |
||
| 87 | |||
| 88 | abstract public function get_jetpack_seo_front_page_description(); |
||
| 89 | |||
| 90 | abstract public function get_jetpack_seo_title_formats(); |
||
| 91 | |||
| 92 | abstract public function get_verification_services_codes(); |
||
| 93 | |||
| 94 | abstract public function before_render(); |
||
| 95 | |||
| 96 | abstract public function after_render( &$response ); |
||
| 97 | |||
| 98 | // TODO - factor this out? Seems an odd thing to have on a site |
||
|
|
|||
| 99 | abstract public function after_render_options( &$options ); |
||
| 100 | |||
| 101 | // wrap a WP_Post object with SAL methods |
||
| 102 | abstract public function wrap_post( $post, $context ); |
||
| 103 | |||
| 104 | abstract protected function is_a8c_publication( $post_id ); |
||
| 105 | |||
| 106 | public function is_automated_transfer() { |
||
| 109 | |||
| 110 | public function get_post_by_id( $post_id, $context ) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Validate current user can access the post |
||
| 125 | * |
||
| 126 | * @return WP_Error or post |
||
| 127 | */ |
||
| 128 | private function validate_access( $post ) { |
||
| 156 | |||
| 157 | View Code Duplication | public function current_user_can_access_post_type( $post_type, $context ) { |
|
| 172 | |||
| 173 | protected function get_post_type_object( $post_type ) { |
||
| 176 | |||
| 177 | // copied from class.json-api-endpoints.php |
||
| 178 | public function is_post_type_allowed( $post_type ) { |
||
| 193 | |||
| 194 | // copied from class.json-api-endpoints.php |
||
| 195 | /** |
||
| 196 | * Gets the whitelisted post types that JP should allow access to. |
||
| 197 | * |
||
| 198 | * @return array Whitelisted post types. |
||
| 199 | */ |
||
| 200 | View Code Duplication | public function get_whitelisted_post_types() { |
|
| 216 | |||
| 217 | // copied and modified a little from class.json-api-endpoints.php |
||
| 218 | private function user_can_view_post( $post ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get post ID by name |
||
| 276 | * |
||
| 277 | * Attempts to match name on post title and page path |
||
| 278 | * |
||
| 279 | * @param string $name |
||
| 280 | * |
||
| 281 | * @return int|object Post ID on success, WP_Error object on failure |
||
| 282 | */ |
||
| 283 | public function get_post_id_by_name( $name ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get post by name |
||
| 311 | * |
||
| 312 | * Attempts to match name on post title and page path |
||
| 313 | * |
||
| 314 | * @param string $name |
||
| 315 | * @param string $context (display or edit) |
||
| 316 | * |
||
| 317 | * @return object Post object on success, WP_Error object on failure |
||
| 318 | **/ |
||
| 319 | public function get_post_by_name( $name, $context ) { |
||
| 327 | |||
| 328 | function user_can_manage() { |
||
| 331 | |||
| 332 | function get_xmlrpc_url() { |
||
| 336 | |||
| 337 | function get_registered_date() { |
||
| 347 | |||
| 348 | function get_capabilities() { |
||
| 370 | |||
| 371 | function is_visible() { |
||
| 387 | |||
| 388 | function get_logo() { |
||
| 412 | |||
| 413 | function get_timezone() { |
||
| 416 | |||
| 417 | function get_gmt_offset() { |
||
| 420 | |||
| 421 | function get_login_url() { |
||
| 424 | |||
| 425 | function get_admin_url() { |
||
| 428 | |||
| 429 | function get_unmapped_url() { |
||
| 432 | |||
| 433 | function get_theme_slug() { |
||
| 436 | |||
| 437 | function get_header_image() { |
||
| 440 | |||
| 441 | function get_background_color() { |
||
| 444 | |||
| 445 | function get_image_default_link_type() { |
||
| 448 | |||
| 449 | function get_image_thumbnail_width() { |
||
| 452 | |||
| 453 | function get_image_thumbnail_height() { |
||
| 456 | |||
| 457 | function get_image_thumbnail_crop() { |
||
| 460 | |||
| 461 | function get_image_medium_width() { |
||
| 464 | |||
| 465 | function get_image_medium_height() { |
||
| 468 | |||
| 469 | function get_image_large_width() { |
||
| 472 | |||
| 473 | function get_image_large_height() { |
||
| 476 | |||
| 477 | function get_permalink_structure() { |
||
| 480 | |||
| 481 | function get_default_post_format() { |
||
| 484 | |||
| 485 | function get_default_category() { |
||
| 488 | |||
| 489 | function get_show_on_front() { |
||
| 492 | |||
| 493 | function is_custom_front_page() { |
||
| 496 | |||
| 497 | function get_default_likes_enabled() { |
||
| 500 | |||
| 501 | function get_default_sharing_status() { |
||
| 510 | |||
| 511 | function get_default_comment_status() { |
||
| 514 | |||
| 515 | function default_ping_status() { |
||
| 518 | |||
| 519 | function is_publicize_permanently_disabled() { |
||
| 526 | |||
| 527 | function get_page_on_front() { |
||
| 530 | |||
| 531 | function get_page_for_posts() { |
||
| 534 | |||
| 535 | function is_headstart() { |
||
| 538 | |||
| 539 | function get_wordpress_version() { |
||
| 543 | |||
| 544 | function is_domain_only() { |
||
| 548 | } |
||
| 549 |