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 ) { |
||
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 ID by name |
||
247 | * |
||
248 | * Attempts to match name on post title and page path |
||
249 | * |
||
250 | * @param string $name |
||
251 | * |
||
252 | * @return int|object Post ID on success, WP_Error object on failure |
||
253 | */ |
||
254 | public function get_post_id_by_name( $name ) { |
||
255 | $name = sanitize_title( $name ); |
||
256 | |||
257 | if ( ! $name ) { |
||
258 | return new WP_Error( 'invalid_post', 'Invalid post', 400 ); |
||
259 | } |
||
260 | |||
261 | $posts = get_posts( array( |
||
262 | 'name' => $name, |
||
263 | 'numberposts' => 1, |
||
264 | 'post_type' => $this->_get_whitelisted_post_types(), |
||
265 | ) ); |
||
266 | |||
267 | if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) { |
||
268 | $page = get_page_by_path( $name ); |
||
269 | |||
270 | if ( ! $page ) { |
||
271 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
||
272 | } |
||
273 | |||
274 | return $page->ID; |
||
275 | } |
||
276 | |||
277 | return (int) $posts[0]->ID; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Get post by name |
||
282 | * |
||
283 | * Attempts to match name on post title and page path |
||
284 | * |
||
285 | * @param string $name |
||
286 | * @param string $context (display or edit) |
||
287 | * |
||
288 | * @return object Post object on success, WP_Error object on failure |
||
289 | **/ |
||
290 | public function get_post_by_name( $name, $context ) { |
||
291 | $post_id = $this->get_post_id_by_name( $name ); |
||
292 | if ( is_wp_error( $post_id ) ) { |
||
293 | return $post_id; |
||
294 | } |
||
295 | |||
296 | return $this->get_post_by_id( $post_id, $context ); |
||
297 | } |
||
298 | |||
299 | function user_can_manage() { |
||
300 | current_user_can( 'manage_options' ); |
||
301 | } |
||
302 | |||
303 | function get_xmlrpc_url() { |
||
304 | $xmlrpc_scheme = apply_filters( 'wpcom_json_api_xmlrpc_scheme', parse_url( get_option( 'home' ), PHP_URL_SCHEME ) ); |
||
305 | return site_url( 'xmlrpc.php', $xmlrpc_scheme ); |
||
306 | } |
||
307 | |||
308 | function get_registered_date() { |
||
309 | if ( function_exists( 'get_blog_details' ) ) { |
||
310 | $blog_details = get_blog_details(); |
||
311 | if ( ! empty( $blog_details->registered ) ) { |
||
312 | return WPCOM_JSON_API_Date::format_date( $blog_details->registered ); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | return '0000-00-00T00:00:00+00:00'; |
||
317 | } |
||
318 | |||
319 | function get_capabilities() { |
||
338 | |||
339 | function is_visible() { |
||
355 | |||
356 | function get_logo() { |
||
380 | |||
381 | function get_timezone() { |
||
382 | return (string) get_option( 'timezone_string' ); |
||
383 | } |
||
384 | |||
385 | function get_gmt_offset() { |
||
388 | |||
389 | function get_login_url() { |
||
392 | |||
393 | function get_admin_url() { |
||
396 | |||
397 | function get_unmapped_url() { |
||
400 | |||
401 | function get_theme_slug() { |
||
404 | |||
405 | function get_header_image() { |
||
408 | |||
409 | function get_background_color() { |
||
412 | |||
413 | function get_image_default_link_type() { |
||
416 | |||
417 | function get_image_thumbnail_width() { |
||
420 | |||
421 | function get_image_thumbnail_height() { |
||
424 | |||
425 | function get_image_thumbnail_crop() { |
||
428 | |||
429 | function get_image_medium_width() { |
||
432 | |||
433 | function get_image_medium_height() { |
||
436 | |||
437 | function get_image_large_width() { |
||
440 | |||
441 | function get_image_large_height() { |
||
444 | |||
445 | function get_permalink_structure() { |
||
448 | |||
449 | function get_default_post_format() { |
||
452 | |||
453 | function get_default_category() { |
||
456 | |||
457 | function get_show_on_front() { |
||
460 | |||
461 | function is_custom_front_page() { |
||
464 | |||
465 | function get_default_likes_enabled() { |
||
468 | |||
469 | function get_default_sharing_status() { |
||
478 | |||
479 | function get_default_comment_status() { |
||
482 | |||
483 | function default_ping_status() { |
||
486 | |||
487 | function is_publicize_permanently_disabled() { |
||
494 | |||
495 | function get_page_on_front() { |
||
498 | |||
499 | function get_page_for_posts() { |
||
502 | |||
503 | function is_headstart() { |
||
506 | |||
507 | function get_wordpress_version() { |
||
511 | } |
||
512 |