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 onthe frontend. |
||
| 14 | */ |
||
| 15 | public static function init() { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Apply custom AMP changes in wp-admin. |
||
| 43 | */ |
||
| 44 | public static function admin_init() { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Is the page in AMP 'canonical mode'. |
||
| 51 | * Used when themes register support for AMP with `add_theme_support( 'amp' )`. |
||
| 52 | * |
||
| 53 | * @return bool is_amp_canonical |
||
| 54 | */ |
||
| 55 | public static function is_amp_canonical() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Does the page return AMP content. |
||
| 61 | * |
||
| 62 | * @return bool $is_amp_request Are we on am AMP view. |
||
| 63 | */ |
||
| 64 | public static function is_amp_request() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Remove content filters added by Jetpack. |
||
| 79 | */ |
||
| 80 | public static function amp_disable_the_content_filters() { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Add Jetpack stats pixel. |
||
| 93 | * |
||
| 94 | * @since 6.2.1 |
||
| 95 | */ |
||
| 96 | public static function add_stats_pixel() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Add publisher and image metadata to legacy AMP post. |
||
| 105 | * |
||
| 106 | * @since 6.2.0 |
||
| 107 | * |
||
| 108 | * @param array $metadata Metadata array. |
||
| 109 | * @param WP_Post $post Post. |
||
| 110 | * @return array Modified metadata array. |
||
| 111 | */ |
||
| 112 | public static function amp_post_template_metadata( $metadata, $post ) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Add blavatar to legacy AMP post metadata. |
||
| 126 | * |
||
| 127 | * @since 6.2.0 |
||
| 128 | * |
||
| 129 | * @param array $metadata Metadata. |
||
| 130 | * |
||
| 131 | * @return array Metadata. |
||
| 132 | */ |
||
| 133 | private static function add_site_icon_to_metadata( $metadata ) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Add image to legacy AMP post metadata. |
||
| 158 | * |
||
| 159 | * @since 6.2.0 |
||
| 160 | * |
||
| 161 | * @param array $metadata Metadata. |
||
| 162 | * @param WP_Post $post Post. |
||
| 163 | * @return array Metadata. |
||
| 164 | */ |
||
| 165 | private static function add_image_to_metadata( $metadata, $post ) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Add fallback image to legacy AMP post metadata. |
||
| 210 | * |
||
| 211 | * @since 6.2.0 |
||
| 212 | * |
||
| 213 | * @param array $metadata Metadata. |
||
| 214 | * @return array Metadata. |
||
| 215 | */ |
||
| 216 | private static function add_fallback_image_to_metadata( $metadata ) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Return static WordPress.com domain to use to load resources from WordPress.com. |
||
| 232 | * |
||
| 233 | * @param string $domain Asset URL. |
||
| 234 | */ |
||
| 235 | private static function staticize_subdomain( $domain ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Extract image dimensions via wpcom/imagesize, only on WPCOM |
||
| 246 | * |
||
| 247 | * @since 6.2.0 |
||
| 248 | * |
||
| 249 | * @param array $dimensions Dimensions. |
||
| 250 | * @return array Dimensions. |
||
| 251 | */ |
||
| 252 | private static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Display Open Graph Meta tags in AMP views. |
||
| 276 | */ |
||
| 277 | public static function amp_post_jetpack_og_tags() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Force Freedom mode in VideoPress. |
||
| 289 | * |
||
| 290 | * @param array $options Array of VideoPress shortcode options. |
||
| 291 | */ |
||
| 292 | public static function videopress_enable_freedom_mode( $options ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Display custom markup for the sharing buttons when in an AMP view. |
||
| 301 | * |
||
| 302 | * @param string $markup Content markup of the Jetpack sharing links. |
||
| 303 | * @param array $sharing_enabled Array of Sharing Services currently enabled. |
||
| 304 | */ |
||
| 305 | public static function render_sharing_html( $markup, $sharing_enabled ) { |
||
| 353 | } |
||
| 354 | |||
| 359 |