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 |
||
11 | class Jetpack_AMP_Support { |
||
12 | |||
13 | /** |
||
14 | * Apply custom AMP changes on the front-end. |
||
15 | */ |
||
16 | public static function init() { |
||
70 | |||
71 | /** |
||
72 | * Disable the Comment Likes feature on AMP views. |
||
73 | * |
||
74 | * @param bool $enabled Should comment likes be enabled. |
||
75 | */ |
||
76 | public static function comment_likes_enabled( $enabled ) { |
||
79 | |||
80 | /** |
||
81 | * Apply custom AMP changes in wp-admin. |
||
82 | */ |
||
83 | public static function admin_init() { |
||
87 | |||
88 | /** |
||
89 | * Is the page in AMP 'canonical mode'. |
||
90 | * Used when themes register support for AMP with `add_theme_support( 'amp' )`. |
||
91 | * |
||
92 | * @return bool is_amp_canonical |
||
93 | */ |
||
94 | public static function is_amp_canonical() { |
||
97 | |||
98 | /** |
||
99 | * Is AMP available for this request |
||
100 | * This returns false for admin, CLI requests etc. |
||
101 | * |
||
102 | * @return bool is_amp_available |
||
103 | */ |
||
104 | public static function is_amp_available() { |
||
107 | |||
108 | /** |
||
109 | * Does the page return AMP content. |
||
110 | * |
||
111 | * @return bool $is_amp_request Are we on am AMP view. |
||
112 | */ |
||
113 | public static function is_amp_request() { |
||
125 | |||
126 | /** |
||
127 | * Remove content filters added by Jetpack. |
||
128 | */ |
||
129 | public static function amp_disable_the_content_filters() { |
||
139 | |||
140 | /** |
||
141 | * Do not add comment likes on AMP requests. |
||
142 | * |
||
143 | * @param string $content Post content. |
||
144 | */ |
||
145 | public static function disable_comment_likes_before_the_content( $content ) { |
||
151 | |||
152 | /** |
||
153 | * Do not display the Likes' Admin bar on AMP requests. |
||
154 | * |
||
155 | * @param bool $is_admin_bar_button_visible Should the Like button be visible in the Admin bar. Default to true. |
||
156 | */ |
||
157 | public static function disable_likes_admin_bar( $is_admin_bar_button_visible ) { |
||
163 | |||
164 | /** |
||
165 | * Add Jetpack stats pixel. |
||
166 | * |
||
167 | * @since 6.2.1 |
||
168 | */ |
||
169 | public static function add_stats_pixel() { |
||
175 | |||
176 | /** |
||
177 | * Add publisher and image metadata to legacy AMP post. |
||
178 | * |
||
179 | * @since 6.2.0 |
||
180 | * |
||
181 | * @param array $metadata Metadata array. |
||
182 | * @param WP_Post $post Post. |
||
183 | * @return array Modified metadata array. |
||
184 | */ |
||
185 | public static function amp_post_template_metadata( $metadata, $post ) { |
||
196 | |||
197 | /** |
||
198 | * Add blavatar to legacy AMP post metadata. |
||
199 | * |
||
200 | * @since 6.2.0 |
||
201 | * |
||
202 | * @param array $metadata Metadata. |
||
203 | * |
||
204 | * @return array Metadata. |
||
205 | */ |
||
206 | private static function add_site_icon_to_metadata( $metadata ) { |
||
228 | |||
229 | /** |
||
230 | * Add image to legacy AMP post metadata. |
||
231 | * |
||
232 | * @since 6.2.0 |
||
233 | * |
||
234 | * @param array $metadata Metadata. |
||
235 | * @param WP_Post $post Post. |
||
236 | * @return array Metadata. |
||
237 | */ |
||
238 | private static function add_image_to_metadata( $metadata, $post ) { |
||
280 | |||
281 | /** |
||
282 | * Add fallback image to legacy AMP post metadata. |
||
283 | * |
||
284 | * @since 6.2.0 |
||
285 | * |
||
286 | * @param array $metadata Metadata. |
||
287 | * @return array Metadata. |
||
288 | */ |
||
289 | private static function add_fallback_image_to_metadata( $metadata ) { |
||
302 | |||
303 | /** |
||
304 | * Return static WordPress.com domain to use to load resources from WordPress.com. |
||
305 | * |
||
306 | * @param string $domain Asset URL. |
||
307 | */ |
||
308 | private static function staticize_subdomain( $domain ) { |
||
316 | |||
317 | /** |
||
318 | * Extract image dimensions via wpcom/imagesize, only on WPCOM |
||
319 | * |
||
320 | * @since 6.2.0 |
||
321 | * |
||
322 | * @param array $dimensions Dimensions. |
||
323 | * @return array Dimensions. |
||
324 | */ |
||
325 | private static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
||
346 | |||
347 | /** |
||
348 | * Display Open Graph Meta tags in AMP views. |
||
349 | */ |
||
350 | public static function amp_post_jetpack_og_tags() { |
||
359 | |||
360 | /** |
||
361 | * Force Freedom mode in VideoPress. |
||
362 | * |
||
363 | * @param array $options Array of VideoPress shortcode options. |
||
364 | */ |
||
365 | public static function videopress_enable_freedom_mode( $options ) { |
||
371 | |||
372 | /** |
||
373 | * Display custom markup for the sharing buttons when in an AMP view. |
||
374 | * |
||
375 | * @param string $markup Content markup of the Jetpack sharing links. |
||
376 | * @param array $sharing_enabled Array of Sharing Services currently enabled. |
||
377 | */ |
||
378 | public static function render_sharing_html( $markup, $sharing_enabled ) { |
||
410 | |||
411 | /** |
||
412 | * Tells Jetpack not to enqueue CSS for share buttons. |
||
413 | * |
||
414 | * @param bool $enqueue Whether or not to enqueue. |
||
415 | * @return bool Whether or not to enqueue. |
||
416 | */ |
||
417 | public static function amp_disable_sharedaddy_css( $enqueue ) { |
||
424 | |||
425 | /** |
||
426 | * Enqueues the AMP specific sharing styles for the sharing icons. |
||
427 | */ |
||
428 | public static function amp_enqueue_sharing_css() { |
||
433 | |||
434 | /** |
||
435 | * For the AMP Reader mode template, include styles that we need. |
||
436 | */ |
||
437 | public static function amp_reader_sharing_css() { |
||
461 | |||
462 | /** |
||
463 | * Ensure proper Photon image dimensions for AMP Stories. |
||
464 | * |
||
465 | * @param array $args Array of Photon Arguments. |
||
466 | * @param array $details { |
||
467 | * Array of image details. |
||
468 | * |
||
469 | * @type string $tag Image tag (Image HTML output). |
||
470 | * @type string $src Image URL. |
||
471 | * @type string $src_orig Original Image URL. |
||
472 | * @type int|false $width Image width. |
||
473 | * @type int|false $height Image height. |
||
474 | * @type int|false $width_orig Original image width before constrained by content_width. |
||
475 | * @type int|false $height_orig Original Image height before constrained by content_width. |
||
476 | * @type string $transform_orig Original transform before constrained by content_width. |
||
477 | * } |
||
478 | * @return array Args. |
||
479 | */ |
||
480 | public static function filter_photon_post_image_args_for_stories( $args, $details ) { |
||
529 | |||
530 | /** |
||
531 | * Adds amp-options to the list of options to sync, if AMP is available |
||
532 | * |
||
533 | * @param array $options_safelist Safelist of options to sync. |
||
534 | * |
||
535 | * @return array Updated options safelist |
||
536 | */ |
||
537 | public static function filter_jetpack_options_safelist( $options_safelist ) { |
||
543 | } |
||
544 | |||
548 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.