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 WPcom_Admin_Menu 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 WPcom_Admin_Menu, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 17 | class WPcom_Admin_Menu extends Admin_Menu { | 
            ||
| 18 | |||
| 19 | /**  | 
            ||
| 20 | * Sets up class properties for REST API requests.  | 
            ||
| 21 | *  | 
            ||
| 22 | * @param WP_REST_Response $response Response from the endpoint.  | 
            ||
| 23 | */  | 
            ||
| 24 | 	public function rest_api_init( $response ) { | 
            ||
| 25 | parent::rest_api_init( $response );  | 
            ||
| 26 | |||
| 27 | // Get domain for requested site.  | 
            ||
| 28 | $this->domain = ( new Status() )->get_site_suffix();  | 
            ||
| 29 | |||
| 30 | return $response;  | 
            ||
| 31 | }  | 
            ||
| 32 | |||
| 33 | /**  | 
            ||
| 34 | * Create the desired menu output.  | 
            ||
| 35 | */  | 
            ||
| 36 | View Code Duplication | 	public function reregister_menu_items() { | 
            |
| 37 | parent::reregister_menu_items();  | 
            ||
| 38 | |||
| 39 | $wp_admin = $this->should_link_to_wp_admin();  | 
            ||
| 40 | |||
| 41 | $this->add_my_home_menu( $wp_admin );  | 
            ||
| 42 | |||
| 43 | // Not needed outside of wp-admin.  | 
            ||
| 44 | 		if ( ! $this->is_api_request ) { | 
            ||
| 45 | $this->add_browse_sites_link();  | 
            ||
| 46 | $this->add_site_card_menu();  | 
            ||
| 47 | $this->add_new_site_link();  | 
            ||
| 48 | }  | 
            ||
| 49 | |||
| 50 | $this->add_gutenberg_menus( $wp_admin );  | 
            ||
| 51 | |||
| 52 | ksort( $GLOBALS['menu'] );  | 
            ||
| 53 | }  | 
            ||
| 54 | |||
| 55 | /**  | 
            ||
| 56 | * Adds the site switcher link if user has more than one site.  | 
            ||
| 57 | */  | 
            ||
| 58 | 	public function add_browse_sites_link() { | 
            ||
| 67 | |||
| 68 | /**  | 
            ||
| 69 | * Adds a custom element class for Site Switcher menu item.  | 
            ||
| 70 | *  | 
            ||
| 71 | * @param array $menu Associative array of administration menu items.  | 
            ||
| 72 | * @return array  | 
            ||
| 73 | */  | 
            ||
| 74 | View Code Duplication | 	public function set_browse_sites_link_class( array $menu ) { | 
            |
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * Adds a link to the menu to create a new site.  | 
            ||
| 89 | */  | 
            ||
| 90 | 	public function add_new_site_link() { | 
            ||
| 106 | |||
| 107 | /**  | 
            ||
| 108 | * Adds site card component.  | 
            ||
| 109 | */  | 
            ||
| 110 | 	public function add_site_card_menu() { | 
            ||
| 151 | |||
| 152 | /**  | 
            ||
| 153 | * Adds a custom element class and id for Site Card's menu item.  | 
            ||
| 154 | *  | 
            ||
| 155 | * @param array $menu Associative array of administration menu items.  | 
            ||
| 156 | * @return array  | 
            ||
| 157 | */  | 
            ||
| 158 | 	public function set_site_card_menu_class( array $menu ) { | 
            ||
| 176 | |||
| 177 | /**  | 
            ||
| 178 | * Adds Stats menu.  | 
            ||
| 179 | */  | 
            ||
| 180 | 	public function add_stats_menu() { | 
            ||
| 193 | |||
| 194 | /**  | 
            ||
| 195 | * Adds Upgrades menu.  | 
            ||
| 196 | */  | 
            ||
| 197 | 	public function add_upgrades_menu() { | 
            ||
| 202 | |||
| 203 | /**  | 
            ||
| 204 | * Adds Appearance menu.  | 
            ||
| 205 | *  | 
            ||
| 206 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).  | 
            ||
| 207 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).  | 
            ||
| 208 | */  | 
            ||
| 209 | 	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { | 
            ||
| 223 | |||
| 224 | /**  | 
            ||
| 225 | * Adds Users menu.  | 
            ||
| 226 | *  | 
            ||
| 227 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).  | 
            ||
| 228 | */  | 
            ||
| 229 | 	public function add_users_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | 
            ||
| 233 | |||
| 234 | /**  | 
            ||
| 235 | * Adds Tools menu.  | 
            ||
| 236 | *  | 
            ||
| 237 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).  | 
            ||
| 238 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).  | 
            ||
| 239 | */  | 
            ||
| 240 | 	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {  // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | 
            ||
| 244 | |||
| 245 | /**  | 
            ||
| 246 | * Adds Settings menu.  | 
            ||
| 247 | *  | 
            ||
| 248 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).  | 
            ||
| 249 | */  | 
            ||
| 250 | 	public function add_options_menu( $wp_admin = false ) { | 
            ||
| 260 | |||
| 261 | /**  | 
            ||
| 262 | * 1. Remove the Gutenberg plugin menu  | 
            ||
| 263 | * 2. Re-add the Site Editor menu  | 
            ||
| 264 | *  | 
            ||
| 265 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).  | 
            ||
| 266 | */  | 
            ||
| 267 | 	public function add_gutenberg_menus( $wp_admin = false ) { | 
            ||
| 293 | |||
| 294 | /**  | 
            ||
| 295 | * Whether to use wp-admin pages rather than Calypso.  | 
            ||
| 296 | *  | 
            ||
| 297 | * @return bool  | 
            ||
| 298 | */  | 
            ||
| 299 | 	public function should_link_to_wp_admin() { | 
            ||
| 309 | |||
| 310 | /**  | 
            ||
| 311 | * Adds Plugins menu.  | 
            ||
| 312 | *  | 
            ||
| 313 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).  | 
            ||
| 314 | */  | 
            ||
| 315 | 	public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | 
            ||
| 319 | }  | 
            ||
| 320 |