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 featured_images_enabled(); |
||
| 53 | |||
| 54 | abstract public function has_wordads(); |
||
| 55 | |||
| 56 | abstract public function get_frame_nonce(); |
||
| 57 | |||
| 58 | abstract public function allowed_file_types(); |
||
| 59 | |||
| 60 | abstract public function get_post_formats(); |
||
| 61 | |||
| 62 | abstract public function is_private(); |
||
| 63 | |||
| 64 | abstract public function is_following(); |
||
| 65 | |||
| 66 | abstract public function get_subscribers_count(); |
||
| 67 | |||
| 68 | abstract public function get_locale(); |
||
| 69 | |||
| 70 | abstract public function is_jetpack(); |
||
| 71 | |||
| 72 | abstract public function get_jetpack_modules(); |
||
| 73 | |||
| 74 | abstract public function is_vip(); |
||
| 75 | |||
| 76 | abstract public function is_multisite(); |
||
| 77 | |||
| 78 | abstract public function is_single_user_site(); |
||
| 79 | |||
| 80 | abstract public function get_plan(); |
||
| 81 | |||
| 82 | abstract public function get_ak_vp_bundle_enabled(); |
||
| 83 | |||
| 84 | abstract public function get_jetpack_seo_front_page_description(); |
||
| 85 | |||
| 86 | abstract public function get_jetpack_seo_title_formats(); |
||
| 87 | |||
| 88 | abstract public function get_verification_services_codes(); |
||
| 89 | |||
| 90 | abstract public function before_render(); |
||
| 91 | |||
| 92 | abstract public function after_render( &$response ); |
||
| 93 | |||
| 94 | // TODO - factor this out? Seems an odd thing to have on a site |
||
|
|
|||
| 95 | abstract public function after_render_options( &$options ); |
||
| 96 | |||
| 97 | // wrap a WP_Post object with SAL methods |
||
| 98 | abstract public function wrap_post( $post, $context ); |
||
| 99 | |||
| 100 | |||
| 101 | public function get_post_by_id( $post_id, $context ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Validate current user can access the post |
||
| 116 | * |
||
| 117 | * @return WP_Error or post |
||
| 118 | */ |
||
| 119 | private function validate_access( $post ) { |
||
| 146 | |||
| 147 | // copied from class.json-api-endpoints.php |
||
| 148 | private function is_post_type_allowed( $post_type ) { |
||
| 149 | // if the post type is empty, that's fine, WordPress will default to post |
||
| 150 | if ( empty( $post_type ) ) |
||
| 151 | return true; |
||
| 152 | |||
| 153 | // allow special 'any' type |
||
| 154 | if ( 'any' == $post_type ) |
||
| 155 | return true; |
||
| 156 | |||
| 157 | // check for allowed types |
||
| 158 | if ( in_array( $post_type, $this->_get_whitelisted_post_types() ) ) |
||
| 159 | return true; |
||
| 160 | |||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | // copied from class.json-api-endpoints.php |
||
| 165 | /** |
||
| 166 | * Gets the whitelisted post types that JP should allow access to. |
||
| 167 | * |
||
| 168 | * @return array Whitelisted post types. |
||
| 169 | */ |
||
| 170 | View Code Duplication | private function _get_whitelisted_post_types() { |
|
| 186 | |||
| 187 | // copied and modified a little from class.json-api-endpoints.php |
||
| 188 | private function user_can_view_post( $post ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get post ID by name |
||
| 246 | * |
||
| 247 | * Attempts to match name on post title and page path |
||
| 248 | * |
||
| 249 | * @param string $name |
||
| 250 | * |
||
| 251 | * @return int|object Post ID on success, WP_Error object on failure |
||
| 252 | */ |
||
| 253 | public function get_post_id_by_name( $name ) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get post by name |
||
| 281 | * |
||
| 282 | * Attempts to match name on post title and page path |
||
| 283 | * |
||
| 284 | * @param string $name |
||
| 285 | * @param string $context (display or edit) |
||
| 286 | * |
||
| 287 | * @return object Post object on success, WP_Error object on failure |
||
| 288 | **/ |
||
| 289 | public function get_post_by_name( $name, $context ) { |
||
| 297 | |||
| 298 | function user_can_manage() { |
||
| 301 | |||
| 302 | function get_xmlrpc_url() { |
||
| 306 | |||
| 307 | function get_registered_date() { |
||
| 317 | |||
| 318 | function get_capabilities() { |
||
| 337 | |||
| 338 | function is_visible() { |
||
| 354 | |||
| 355 | function get_logo() { |
||
| 379 | |||
| 380 | function get_timezone() { |
||
| 383 | |||
| 384 | function get_gmt_offset() { |
||
| 387 | |||
| 388 | function get_login_url() { |
||
| 391 | |||
| 392 | function get_admin_url() { |
||
| 395 | |||
| 396 | function get_unmapped_url() { |
||
| 399 | |||
| 400 | function get_theme_slug() { |
||
| 403 | |||
| 404 | function get_header_image() { |
||
| 407 | |||
| 408 | function get_background_color() { |
||
| 411 | |||
| 412 | function get_image_default_link_type() { |
||
| 415 | |||
| 416 | function get_image_thumbnail_width() { |
||
| 419 | |||
| 420 | function get_image_thumbnail_height() { |
||
| 423 | |||
| 424 | function get_image_thumbnail_crop() { |
||
| 427 | |||
| 428 | function get_image_medium_width() { |
||
| 431 | |||
| 432 | function get_image_medium_height() { |
||
| 435 | |||
| 436 | function get_image_large_width() { |
||
| 439 | |||
| 440 | function get_image_large_height() { |
||
| 443 | |||
| 444 | function get_permalink_structure() { |
||
| 447 | |||
| 448 | function get_default_post_format() { |
||
| 451 | |||
| 452 | function get_default_category() { |
||
| 455 | |||
| 456 | function get_show_on_front() { |
||
| 459 | |||
| 460 | function is_custom_front_page() { |
||
| 463 | |||
| 464 | function get_default_likes_enabled() { |
||
| 467 | |||
| 468 | function get_default_sharing_status() { |
||
| 477 | |||
| 478 | function get_default_comment_status() { |
||
| 481 | |||
| 482 | function default_ping_status() { |
||
| 485 | |||
| 486 | function is_publicize_permanently_disabled() { |
||
| 493 | |||
| 494 | function get_page_on_front() { |
||
| 497 | |||
| 498 | function get_page_for_posts() { |
||
| 501 | |||
| 502 | function is_headstart() { |
||
| 505 | |||
| 506 | function get_wordpress_version() { |
||
| 510 | } |
||
| 511 |