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() { |
||
67 | |||
68 | /** |
||
69 | * Disable the admin bar on AMP views. |
||
70 | * |
||
71 | * @param Whether bool $show the admin bar should be shown. Default false. |
||
72 | */ |
||
73 | public static function show_admin_bar( $show ) { |
||
76 | |||
77 | /** |
||
78 | * Disable the Comment Likes feature on AMP views. |
||
79 | * |
||
80 | * @param bool $enabled Should comment likes be enabled. |
||
81 | */ |
||
82 | public static function comment_likes_enabled( $enabled ) { |
||
85 | |||
86 | /** |
||
87 | * Apply custom AMP changes in wp-admin. |
||
88 | */ |
||
89 | public static function admin_init() { |
||
93 | |||
94 | /** |
||
95 | * Is the page in AMP 'canonical mode'. |
||
96 | * Used when themes register support for AMP with `add_theme_support( 'amp' )`. |
||
97 | * |
||
98 | * @return bool is_amp_canonical |
||
99 | */ |
||
100 | public static function is_amp_canonical() { |
||
103 | |||
104 | /** |
||
105 | * Does the page return AMP content. |
||
106 | * |
||
107 | * @return bool $is_amp_request Are we on am AMP view. |
||
108 | */ |
||
109 | public static function is_amp_request() { |
||
121 | |||
122 | /** |
||
123 | * Remove content filters added by Jetpack. |
||
124 | */ |
||
125 | public static function amp_disable_the_content_filters() { |
||
135 | |||
136 | /** |
||
137 | * Do not add comment likes on AMP requests. |
||
138 | * |
||
139 | * @param string $content Post content. |
||
140 | */ |
||
141 | public static function disable_comment_likes_before_the_content( $content ) { |
||
147 | |||
148 | /** |
||
149 | * Add Jetpack stats pixel. |
||
150 | * |
||
151 | * @since 6.2.1 |
||
152 | */ |
||
153 | public static function add_stats_pixel() { |
||
159 | |||
160 | /** |
||
161 | * Add publisher and image metadata to legacy AMP post. |
||
162 | * |
||
163 | * @since 6.2.0 |
||
164 | * |
||
165 | * @param array $metadata Metadata array. |
||
166 | * @param WP_Post $post Post. |
||
167 | * @return array Modified metadata array. |
||
168 | */ |
||
169 | public static function amp_post_template_metadata( $metadata, $post ) { |
||
180 | |||
181 | /** |
||
182 | * Add blavatar to legacy AMP post metadata. |
||
183 | * |
||
184 | * @since 6.2.0 |
||
185 | * |
||
186 | * @param array $metadata Metadata. |
||
187 | * |
||
188 | * @return array Metadata. |
||
189 | */ |
||
190 | private static function add_site_icon_to_metadata( $metadata ) { |
||
212 | |||
213 | /** |
||
214 | * Add image to legacy AMP post metadata. |
||
215 | * |
||
216 | * @since 6.2.0 |
||
217 | * |
||
218 | * @param array $metadata Metadata. |
||
219 | * @param WP_Post $post Post. |
||
220 | * @return array Metadata. |
||
221 | */ |
||
222 | private static function add_image_to_metadata( $metadata, $post ) { |
||
264 | |||
265 | /** |
||
266 | * Add fallback image to legacy AMP post metadata. |
||
267 | * |
||
268 | * @since 6.2.0 |
||
269 | * |
||
270 | * @param array $metadata Metadata. |
||
271 | * @return array Metadata. |
||
272 | */ |
||
273 | private static function add_fallback_image_to_metadata( $metadata ) { |
||
286 | |||
287 | /** |
||
288 | * Return static WordPress.com domain to use to load resources from WordPress.com. |
||
289 | * |
||
290 | * @param string $domain Asset URL. |
||
291 | */ |
||
292 | private static function staticize_subdomain( $domain ) { |
||
300 | |||
301 | /** |
||
302 | * Extract image dimensions via wpcom/imagesize, only on WPCOM |
||
303 | * |
||
304 | * @since 6.2.0 |
||
305 | * |
||
306 | * @param array $dimensions Dimensions. |
||
307 | * @return array Dimensions. |
||
308 | */ |
||
309 | private static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
||
330 | |||
331 | /** |
||
332 | * Display Open Graph Meta tags in AMP views. |
||
333 | */ |
||
334 | public static function amp_post_jetpack_og_tags() { |
||
343 | |||
344 | /** |
||
345 | * Force Freedom mode in VideoPress. |
||
346 | * |
||
347 | * @param array $options Array of VideoPress shortcode options. |
||
348 | */ |
||
349 | public static function videopress_enable_freedom_mode( $options ) { |
||
355 | |||
356 | /** |
||
357 | * Display custom markup for the sharing buttons when in an AMP view. |
||
358 | * |
||
359 | * @param string $markup Content markup of the Jetpack sharing links. |
||
360 | * @param array $sharing_enabled Array of Sharing Services currently enabled. |
||
361 | */ |
||
362 | public static function render_sharing_html( $markup, $sharing_enabled ) { |
||
394 | |||
395 | /** |
||
396 | * Tells Jetpack not to enqueue CSS for share buttons. |
||
397 | * |
||
398 | * @param bool $enqueue Whether or not to enqueue. |
||
399 | * @return bool Whether or not to enqueue. |
||
400 | */ |
||
401 | public static function amp_disable_sharedaddy_css( $enqueue ) { |
||
408 | |||
409 | /** |
||
410 | * Enqueues the AMP specific sharing styles for the sharing icons. |
||
411 | */ |
||
412 | public static function amp_enqueue_sharing_css() { |
||
417 | |||
418 | /** |
||
419 | * For the AMP Reader mode template, include styles that we need. |
||
420 | */ |
||
421 | public static function amp_reader_sharing_css() { |
||
425 | |||
426 | /** |
||
427 | * Ensure proper Photon image dimensions for AMP Stories. |
||
428 | * |
||
429 | * @param array $args Array of Photon Arguments. |
||
430 | * @param array $details { |
||
431 | * Array of image details. |
||
432 | * |
||
433 | * @type string $tag Image tag (Image HTML output). |
||
434 | * @type string $src Image URL. |
||
435 | * @type string $src_orig Original Image URL. |
||
436 | * @type int|false $width Image width. |
||
437 | * @type int|false $height Image height. |
||
438 | * @type int|false $width_orig Original image width before constrained by content_width. |
||
439 | * @type int|false $height_orig Original Image height before constrained by content_width. |
||
440 | * @type string $transform_orig Original transform before constrained by content_width. |
||
441 | * } |
||
442 | * @return array Args. |
||
443 | */ |
||
444 | public static function filter_photon_post_image_args_for_stories( $args, $details ) { |
||
493 | |||
494 | /** |
||
495 | * Adds amp-options to the list of options to sync, if AMP is available |
||
496 | * |
||
497 | * @param array $options_whitelist Whitelist of options to sync. |
||
498 | * @return array Updated options whitelist |
||
499 | */ |
||
500 | public static function filter_jetpack_options_whitelist( $options_whitelist ) { |
||
506 | } |
||
507 | |||
511 |