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() { | 
            ||
| 72 | |||
| 73 | /**  | 
            ||
| 74 | * Selectively disable AMP validation errors for some Jetpack content  | 
            ||
| 75 | *  | 
            ||
| 76 | * @param array $sanitizers The array of sanitizers, 'MyClassName' => [] // array of constructor params for class.  | 
            ||
| 77 | */  | 
            ||
| 78 | 	public static function amp_content_sanitizers( $sanitizers ) { | 
            ||
| 87 | |||
| 88 | /**  | 
            ||
| 89 | * Disable the Comment Likes feature on AMP views.  | 
            ||
| 90 | *  | 
            ||
| 91 | * @param bool $enabled Should comment likes be enabled.  | 
            ||
| 92 | */  | 
            ||
| 93 | 	public static function comment_likes_enabled( $enabled ) { | 
            ||
| 96 | |||
| 97 | /**  | 
            ||
| 98 | * Apply custom AMP changes in wp-admin.  | 
            ||
| 99 | */  | 
            ||
| 100 | 	public static function admin_init() { | 
            ||
| 104 | |||
| 105 | /**  | 
            ||
| 106 | * Is the page in AMP 'canonical mode'.  | 
            ||
| 107 | * Used when themes register support for AMP with `add_theme_support( 'amp' )`.  | 
            ||
| 108 | *  | 
            ||
| 109 | * @return bool is_amp_canonical  | 
            ||
| 110 | */  | 
            ||
| 111 | 	public static function is_amp_canonical() { | 
            ||
| 114 | |||
| 115 | /**  | 
            ||
| 116 | * Does the page return AMP content.  | 
            ||
| 117 | *  | 
            ||
| 118 | * @return bool $is_amp_request Are we on am AMP view.  | 
            ||
| 119 | */  | 
            ||
| 120 | 	public static function is_amp_request() { | 
            ||
| 132 | |||
| 133 | /**  | 
            ||
| 134 | * Remove content filters added by Jetpack.  | 
            ||
| 135 | */  | 
            ||
| 136 | 	public static function amp_disable_the_content_filters() { | 
            ||
| 146 | |||
| 147 | /**  | 
            ||
| 148 | * Do not add comment likes on AMP requests.  | 
            ||
| 149 | *  | 
            ||
| 150 | * @param string $content Post content.  | 
            ||
| 151 | */  | 
            ||
| 152 | 	public static function disable_comment_likes_before_the_content( $content ) { | 
            ||
| 158 | |||
| 159 | /**  | 
            ||
| 160 | * Do not display the Likes' Admin bar on AMP requests.  | 
            ||
| 161 | *  | 
            ||
| 162 | * @param bool $is_admin_bar_button_visible Should the Like button be visible in the Admin bar. Default to true.  | 
            ||
| 163 | */  | 
            ||
| 164 | 	public static function disable_likes_admin_bar( $is_admin_bar_button_visible ) { | 
            ||
| 170 | |||
| 171 | /**  | 
            ||
| 172 | * Add Jetpack stats pixel.  | 
            ||
| 173 | *  | 
            ||
| 174 | * @since 6.2.1  | 
            ||
| 175 | */  | 
            ||
| 176 | 	public static function add_stats_pixel() { | 
            ||
| 182 | |||
| 183 | /**  | 
            ||
| 184 | * Add publisher and image metadata to legacy AMP post.  | 
            ||
| 185 | *  | 
            ||
| 186 | * @since 6.2.0  | 
            ||
| 187 | *  | 
            ||
| 188 | * @param array $metadata Metadata array.  | 
            ||
| 189 | * @param WP_Post $post Post.  | 
            ||
| 190 | * @return array Modified metadata array.  | 
            ||
| 191 | */  | 
            ||
| 192 | 	public static function amp_post_template_metadata( $metadata, $post ) { | 
            ||
| 203 | |||
| 204 | /**  | 
            ||
| 205 | * Add blavatar to legacy AMP post metadata.  | 
            ||
| 206 | *  | 
            ||
| 207 | * @since 6.2.0  | 
            ||
| 208 | *  | 
            ||
| 209 | * @param array $metadata Metadata.  | 
            ||
| 210 | *  | 
            ||
| 211 | * @return array Metadata.  | 
            ||
| 212 | */  | 
            ||
| 213 | 	private static function add_site_icon_to_metadata( $metadata ) { | 
            ||
| 235 | |||
| 236 | /**  | 
            ||
| 237 | * Add image to legacy AMP post metadata.  | 
            ||
| 238 | *  | 
            ||
| 239 | * @since 6.2.0  | 
            ||
| 240 | *  | 
            ||
| 241 | * @param array $metadata Metadata.  | 
            ||
| 242 | * @param WP_Post $post Post.  | 
            ||
| 243 | * @return array Metadata.  | 
            ||
| 244 | */  | 
            ||
| 245 | 	private static function add_image_to_metadata( $metadata, $post ) { | 
            ||
| 287 | |||
| 288 | /**  | 
            ||
| 289 | * Add fallback image to legacy AMP post metadata.  | 
            ||
| 290 | *  | 
            ||
| 291 | * @since 6.2.0  | 
            ||
| 292 | *  | 
            ||
| 293 | * @param array $metadata Metadata.  | 
            ||
| 294 | * @return array Metadata.  | 
            ||
| 295 | */  | 
            ||
| 296 | 	private static function add_fallback_image_to_metadata( $metadata ) { | 
            ||
| 309 | |||
| 310 | /**  | 
            ||
| 311 | * Return static WordPress.com domain to use to load resources from WordPress.com.  | 
            ||
| 312 | *  | 
            ||
| 313 | * @param string $domain Asset URL.  | 
            ||
| 314 | */  | 
            ||
| 315 | 	private static function staticize_subdomain( $domain ) { | 
            ||
| 323 | |||
| 324 | /**  | 
            ||
| 325 | * Extract image dimensions via wpcom/imagesize, only on WPCOM  | 
            ||
| 326 | *  | 
            ||
| 327 | * @since 6.2.0  | 
            ||
| 328 | *  | 
            ||
| 329 | * @param array $dimensions Dimensions.  | 
            ||
| 330 | * @return array Dimensions.  | 
            ||
| 331 | */  | 
            ||
| 332 | 	private static function extract_image_dimensions_from_getimagesize( $dimensions ) { | 
            ||
| 353 | |||
| 354 | /**  | 
            ||
| 355 | * Display Open Graph Meta tags in AMP views.  | 
            ||
| 356 | */  | 
            ||
| 357 | 	public static function amp_post_jetpack_og_tags() { | 
            ||
| 366 | |||
| 367 | /**  | 
            ||
| 368 | * Force Freedom mode in VideoPress.  | 
            ||
| 369 | *  | 
            ||
| 370 | * @param array $options Array of VideoPress shortcode options.  | 
            ||
| 371 | */  | 
            ||
| 372 | 	public static function videopress_enable_freedom_mode( $options ) { | 
            ||
| 378 | |||
| 379 | /**  | 
            ||
| 380 | * Display custom markup for the sharing buttons when in an AMP view.  | 
            ||
| 381 | *  | 
            ||
| 382 | * @param string $markup Content markup of the Jetpack sharing links.  | 
            ||
| 383 | * @param array $sharing_enabled Array of Sharing Services currently enabled.  | 
            ||
| 384 | */  | 
            ||
| 385 | 	public static function render_sharing_html( $markup, $sharing_enabled ) { | 
            ||
| 417 | |||
| 418 | /**  | 
            ||
| 419 | * Tells Jetpack not to enqueue CSS for share buttons.  | 
            ||
| 420 | *  | 
            ||
| 421 | * @param bool $enqueue Whether or not to enqueue.  | 
            ||
| 422 | * @return bool Whether or not to enqueue.  | 
            ||
| 423 | */  | 
            ||
| 424 | 	public static function amp_disable_sharedaddy_css( $enqueue ) { | 
            ||
| 431 | |||
| 432 | /**  | 
            ||
| 433 | * Enqueues the AMP specific sharing styles for the sharing icons.  | 
            ||
| 434 | */  | 
            ||
| 435 | 	public static function amp_enqueue_sharing_css() { | 
            ||
| 440 | |||
| 441 | /**  | 
            ||
| 442 | * For the AMP Reader mode template, include styles that we need.  | 
            ||
| 443 | */  | 
            ||
| 444 | 	public static function amp_reader_sharing_css() { | 
            ||
| 465 | |||
| 466 | /**  | 
            ||
| 467 | * Ensure proper Photon image dimensions for AMP Stories.  | 
            ||
| 468 | *  | 
            ||
| 469 | * @param array $args Array of Photon Arguments.  | 
            ||
| 470 | 	 * @param array $details { | 
            ||
| 471 | * Array of image details.  | 
            ||
| 472 | *  | 
            ||
| 473 | * @type string $tag Image tag (Image HTML output).  | 
            ||
| 474 | * @type string $src Image URL.  | 
            ||
| 475 | * @type string $src_orig Original Image URL.  | 
            ||
| 476 | * @type int|false $width Image width.  | 
            ||
| 477 | * @type int|false $height Image height.  | 
            ||
| 478 | * @type int|false $width_orig Original image width before constrained by content_width.  | 
            ||
| 479 | * @type int|false $height_orig Original Image height before constrained by content_width.  | 
            ||
| 480 | * @type string $transform_orig Original transform before constrained by content_width.  | 
            ||
| 481 | * }  | 
            ||
| 482 | * @return array Args.  | 
            ||
| 483 | */  | 
            ||
| 484 | 	public static function filter_photon_post_image_args_for_stories( $args, $details ) { | 
            ||
| 533 | |||
| 534 | /**  | 
            ||
| 535 | * Adds amp-options to the list of options to sync, if AMP is available  | 
            ||
| 536 | *  | 
            ||
| 537 | * @param array $options_safelist Safelist of options to sync.  | 
            ||
| 538 | *  | 
            ||
| 539 | * @return array Updated options safelist  | 
            ||
| 540 | */  | 
            ||
| 541 | 	public static function filter_jetpack_options_safelist( $options_safelist ) { | 
            ||
| 547 | }  | 
            ||
| 548 | |||
| 552 |