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 Jetpack_AMP_Support 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 Jetpack_AMP_Support, and based on these observations, apply Extract Interface, too.
| 1 | <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 10 | class Jetpack_AMP_Support { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Apply custom AMP changes on the front-end. |
||
| 14 | */ |
||
| 15 | public static function init() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Disable the Comment Likes feature on AMP views. |
||
| 65 | * |
||
| 66 | * @param bool $enabled Should comment likes be enabled. |
||
| 67 | */ |
||
| 68 | public static function comment_likes_enabled( $enabled ) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Apply custom AMP changes in wp-admin. |
||
| 74 | */ |
||
| 75 | public static function admin_init() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Is the page in AMP 'canonical mode'. |
||
| 82 | * Used when themes register support for AMP with `add_theme_support( 'amp' )`. |
||
| 83 | * |
||
| 84 | * @return bool is_amp_canonical |
||
| 85 | */ |
||
| 86 | public static function is_amp_canonical() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Does the page return AMP content. |
||
| 92 | * |
||
| 93 | * @return bool $is_amp_request Are we on am AMP view. |
||
| 94 | */ |
||
| 95 | public static function is_amp_request() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Remove content filters added by Jetpack. |
||
| 110 | */ |
||
| 111 | public static function amp_disable_the_content_filters() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Do not add comment likes on AMP requests. |
||
| 124 | * |
||
| 125 | * @param string $content Post content. |
||
| 126 | */ |
||
| 127 | public static function disable_comment_likes_before_the_content( $content ) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Do not display the Likes' Admin bar on AMP requests. |
||
| 136 | * |
||
| 137 | * @param bool $is_admin_bar_button_visible Should the Like button be visible in the Admin bar. Default to true. |
||
| 138 | */ |
||
| 139 | public static function disable_likes_admin_bar( $is_admin_bar_button_visible ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Add Jetpack stats pixel. |
||
| 148 | * |
||
| 149 | * @since 6.2.1 |
||
| 150 | */ |
||
| 151 | public static function add_stats_pixel() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Add publisher and image metadata to legacy AMP post. |
||
| 160 | * |
||
| 161 | * @since 6.2.0 |
||
| 162 | * |
||
| 163 | * @param array $metadata Metadata array. |
||
| 164 | * @param WP_Post $post Post. |
||
| 165 | * @return array Modified metadata array. |
||
| 166 | */ |
||
| 167 | public static function amp_post_template_metadata( $metadata, $post ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Add blavatar to legacy AMP post metadata. |
||
| 181 | * |
||
| 182 | * @since 6.2.0 |
||
| 183 | * |
||
| 184 | * @param array $metadata Metadata. |
||
| 185 | * |
||
| 186 | * @return array Metadata. |
||
| 187 | */ |
||
| 188 | private static function add_site_icon_to_metadata( $metadata ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Add image to legacy AMP post metadata. |
||
| 213 | * |
||
| 214 | * @since 6.2.0 |
||
| 215 | * |
||
| 216 | * @param array $metadata Metadata. |
||
| 217 | * @param WP_Post $post Post. |
||
| 218 | * @return array Metadata. |
||
| 219 | */ |
||
| 220 | private static function add_image_to_metadata( $metadata, $post ) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Add fallback image to legacy AMP post metadata. |
||
| 265 | * |
||
| 266 | * @since 6.2.0 |
||
| 267 | * |
||
| 268 | * @param array $metadata Metadata. |
||
| 269 | * @return array Metadata. |
||
| 270 | */ |
||
| 271 | private static function add_fallback_image_to_metadata( $metadata ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Return static WordPress.com domain to use to load resources from WordPress.com. |
||
| 287 | * |
||
| 288 | * @param string $domain Asset URL. |
||
| 289 | */ |
||
| 290 | private static function staticize_subdomain( $domain ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Extract image dimensions via wpcom/imagesize, only on WPCOM |
||
| 301 | * |
||
| 302 | * @since 6.2.0 |
||
| 303 | * |
||
| 304 | * @param array $dimensions Dimensions. |
||
| 305 | * @return array Dimensions. |
||
| 306 | */ |
||
| 307 | private static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Display Open Graph Meta tags in AMP views. |
||
| 331 | */ |
||
| 332 | public static function amp_post_jetpack_og_tags() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Force Freedom mode in VideoPress. |
||
| 344 | * |
||
| 345 | * @param array $options Array of VideoPress shortcode options. |
||
| 346 | */ |
||
| 347 | public static function videopress_enable_freedom_mode( $options ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Display custom markup for the sharing buttons when in an AMP view. |
||
| 356 | * |
||
| 357 | * @param string $markup Content markup of the Jetpack sharing links. |
||
| 358 | * @param array $sharing_enabled Array of Sharing Services currently enabled. |
||
| 359 | */ |
||
| 360 | public static function render_sharing_html( $markup, $sharing_enabled ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Tells Jetpack not to enqueue CSS for share buttons. |
||
| 411 | * |
||
| 412 | * @param bool $enqueue Whether or not to enqueue. |
||
| 413 | * @return bool Whether or not to enqueue. |
||
| 414 | */ |
||
| 415 | public static function amp_disable_sharedaddy_css( $enqueue ) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Ensure proper Photon image dimensions for AMP Stories. |
||
| 425 | * |
||
| 426 | * @param array $args Array of Photon Arguments. |
||
| 427 | * @param array $details { |
||
| 428 | * Array of image details. |
||
| 429 | * |
||
| 430 | * @type string $tag Image tag (Image HTML output). |
||
| 431 | * @type string $src Image URL. |
||
| 432 | * @type string $src_orig Original Image URL. |
||
| 433 | * @type int|false $width Image width. |
||
| 434 | * @type int|false $height Image height. |
||
| 435 | * @type int|false $width_orig Original image width before constrained by content_width. |
||
| 436 | * @type int|false $height_orig Original Image height before constrained by content_width. |
||
| 437 | * @type string $transform_orig Original transform before constrained by content_width. |
||
| 438 | * } |
||
| 439 | * @return array Args. |
||
| 440 | */ |
||
| 441 | public static function filter_photon_post_image_args_for_stories( $args, $details ) { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Adds amp-options to the list of options to sync, if AMP is available |
||
| 493 | * |
||
| 494 | * @param array $options_whitelist Whitelist of options to sync. |
||
| 495 | * @return array Updated options whitelist |
||
| 496 | */ |
||
| 497 | public static function filter_jetpack_options_whitelist( $options_whitelist ) { |
||
| 503 | } |
||
| 504 | |||
| 508 |