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 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 Admin_Menu, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 16 | class Admin_Menu { | ||
| 17 | /** | ||
| 18 | * Holds class instances. | ||
| 19 | * | ||
| 20 | * @var array | ||
| 21 | */ | ||
| 22 | protected static $instances; | ||
| 23 | |||
| 24 | /** | ||
| 25 | * Whether the current request is a REST API request. | ||
| 26 | * | ||
| 27 | * @var bool | ||
| 28 | */ | ||
| 29 | protected $is_api_request = false; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Domain of the current site. | ||
| 33 | * | ||
| 34 | * @var string | ||
| 35 | */ | ||
| 36 | protected $domain; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Admin_Menu constructor. | ||
| 40 | */ | ||
| 41 | 	protected function __construct() { | ||
| 42 | add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 ); | ||
| 43 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); | ||
| 44 | add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 ); | ||
| 45 | add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 ); | ||
| 46 | add_filter( 'rest_request_before_callbacks', array( $this, 'rest_api_init' ), 11 ); | ||
| 47 | |||
| 48 | $this->domain = ( new Status() )->get_site_suffix(); | ||
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Returns class instance. | ||
| 53 | * | ||
| 54 | * @return Admin_Menu | ||
| 55 | */ | ||
| 56 | 	public static function get_instance() { | ||
| 57 | $class = get_called_class(); | ||
| 58 | |||
| 59 | 		if ( empty( static::$instances[ $class ] ) ) { | ||
| 60 | static::$instances[ $class ] = new $class(); | ||
| 61 | } | ||
| 62 | |||
| 63 | return static::$instances[ $class ]; | ||
| 64 | } | ||
| 65 | |||
| 66 | /** | ||
| 67 | * Sets up class properties for REST API requests. | ||
| 68 | * | ||
| 69 | * @param WP_REST_Response $response Response from the endpoint. | ||
| 70 | */ | ||
| 71 | 	public function rest_api_init( $response ) { | ||
| 72 | $this->is_api_request = true; | ||
| 73 | |||
| 74 | return $response; | ||
| 75 | } | ||
| 76 | |||
| 77 | /** | ||
| 78 | * Create the desired menu output. | ||
| 79 | */ | ||
| 80 | 	public function reregister_menu_items() { | ||
| 81 | // Constant is not defined until parse_request. | ||
| 82 | 		if ( ! $this->is_api_request ) { | ||
| 83 | $this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST; | ||
| 84 | } | ||
| 85 | |||
| 86 | /* | ||
| 87 | * Whether links should point to Calypso or wp-admin. | ||
| 88 | * | ||
| 89 | * Options: | ||
| 90 | * false - Calypso (Default). | ||
| 91 | * true - wp-admin. | ||
| 92 | */ | ||
| 93 | $wp_admin = $this->should_link_to_wp_admin(); | ||
| 94 | |||
| 95 | // Remove separators. | ||
| 96 | remove_menu_page( 'separator1' ); | ||
| 97 | |||
| 98 | $this->add_stats_menu(); | ||
| 99 | $this->add_upgrades_menu(); | ||
| 100 | $this->add_posts_menu( $wp_admin ); | ||
| 101 | $this->add_media_menu( $wp_admin ); | ||
| 102 | $this->add_page_menu( $wp_admin ); | ||
| 103 | $this->add_testimonials_menu( $wp_admin ); | ||
| 104 | $this->add_portfolio_menu( $wp_admin ); | ||
| 105 | $this->add_comments_menu( $wp_admin ); | ||
| 106 | |||
| 107 | // Whether Themes/Customize links should point to Calypso (false) or wp-admin (true). | ||
| 108 | $wp_admin_themes = $wp_admin; | ||
| 109 | $wp_admin_customize = $wp_admin; | ||
| 110 | $this->add_appearance_menu( $wp_admin_themes, $wp_admin_customize ); | ||
| 111 | $this->add_plugins_menu( $wp_admin ); | ||
| 112 | $this->add_users_menu( $wp_admin ); | ||
| 113 | |||
| 114 | // Whether Import/Export links should point to Calypso (false) or wp-admin (true). | ||
| 115 | $wp_admin_import = $wp_admin; | ||
| 116 | $wp_admin_export = $wp_admin; | ||
| 117 | $this->add_tools_menu( $wp_admin_import, $wp_admin_export ); | ||
| 118 | |||
| 119 | $this->add_options_menu( $wp_admin ); | ||
| 120 | $this->add_jetpack_menu(); | ||
| 121 | $this->add_gutenberg_menus( $wp_admin ); | ||
| 122 | |||
| 123 | // Remove Links Manager menu since its usage is discouraged. | ||
| 124 | // @see https://core.trac.wordpress.org/ticket/21307#comment:73. | ||
| 125 | remove_menu_page( 'link-manager.php' ); | ||
| 126 | |||
| 127 | ksort( $GLOBALS['menu'] ); | ||
| 128 | } | ||
| 129 | |||
| 130 | /** | ||
| 131 | * Adds My Home menu. | ||
| 132 | */ | ||
| 133 | 	public function add_my_home_menu() { | ||
| 134 | $this->update_menu( 'index.php', 'https://wordpress.com/home/' . $this->domain, __( 'My Home', 'jetpack' ), 'manage_options', 'dashicons-admin-home' ); | ||
| 135 | } | ||
| 136 | |||
| 137 | /** | ||
| 138 | * Adds Stats menu. | ||
| 139 | */ | ||
| 140 | 	public function add_stats_menu() { | ||
| 143 | |||
| 144 | /** | ||
| 145 | * Adds Upgrades menu. | ||
| 146 | */ | ||
| 147 | 	public function add_upgrades_menu() { | ||
| 148 | global $menu; | ||
| 170 | |||
| 171 | /** | ||
| 172 | * Adds Posts menu. | ||
| 173 | * | ||
| 174 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 175 | */ | ||
| 176 | View Code Duplication | 	public function add_posts_menu( $wp_admin = false ) { | |
| 187 | |||
| 188 | /** | ||
| 189 | * Adds Media menu. | ||
| 190 | * | ||
| 191 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 192 | */ | ||
| 193 | 	public function add_media_menu( $wp_admin = false ) { | ||
| 202 | |||
| 203 | /** | ||
| 204 | * Adds Page menu. | ||
| 205 | * | ||
| 206 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 207 | */ | ||
| 208 | View Code Duplication | 	public function add_page_menu( $wp_admin = false ) { | |
| 219 | |||
| 220 | /** | ||
| 221 | * Adds Testimonials menu. | ||
| 222 | * | ||
| 223 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 224 | */ | ||
| 225 | 	public function add_testimonials_menu( $wp_admin = false ) { | ||
| 228 | |||
| 229 | /** | ||
| 230 | * Adds Portfolio menu. | ||
| 231 | * | ||
| 232 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 233 | */ | ||
| 234 | 	public function add_portfolio_menu( $wp_admin = false ) { | ||
| 237 | |||
| 238 | /** | ||
| 239 | * Adds a custom post type menu. | ||
| 240 | * | ||
| 241 | * @param string $post_type Custom post type. | ||
| 242 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 243 | */ | ||
| 244 | 	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) { | ||
| 255 | |||
| 256 | /** | ||
| 257 | * Adds Comments menu. | ||
| 258 | * | ||
| 259 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 260 | */ | ||
| 261 | 	public function add_comments_menu( $wp_admin = false ) { | ||
| 268 | |||
| 269 | /** | ||
| 270 | * Adds Appearance menu. | ||
| 271 | * | ||
| 272 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). | ||
| 273 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). | ||
| 274 | * @return string The Customizer URL. | ||
| 275 | */ | ||
| 276 | 	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { | ||
| 320 | |||
| 321 | /** | ||
| 322 | * Adds Plugins menu. | ||
| 323 | * | ||
| 324 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 325 | */ | ||
| 326 | 	public function add_plugins_menu( $wp_admin = false ) { | ||
| 336 | |||
| 337 | /** | ||
| 338 | * Adds Users menu. | ||
| 339 | * | ||
| 340 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 341 | */ | ||
| 342 | 	public function add_users_menu( $wp_admin = false ) { | ||
| 366 | |||
| 367 | /** | ||
| 368 | * Adds Tools menu. | ||
| 369 | * | ||
| 370 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). | ||
| 371 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). | ||
| 372 | */ | ||
| 373 | 	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { | ||
| 389 | |||
| 390 | /** | ||
| 391 | * Adds Settings menu. | ||
| 392 | * | ||
| 393 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 394 | */ | ||
| 395 | 	public function add_options_menu( $wp_admin = false ) { | ||
| 405 | |||
| 406 | /** | ||
| 407 | * Adds Jetpack menu. | ||
| 408 | */ | ||
| 409 | 	public function add_jetpack_menu() { | ||
| 440 | |||
| 441 | /** | ||
| 442 | * Re-adds the Site Editor menu without the (beta) tag, and where we want it. | ||
| 443 | * | ||
| 444 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). | ||
| 445 | */ | ||
| 446 | 	public function add_gutenberg_menus( $wp_admin = false ) { | ||
| 469 | |||
| 470 | /** | ||
| 471 | * Updates the menu data of the given menu slug. | ||
| 472 | * | ||
| 473 | * @param string $slug Slug of the menu to update. | ||
| 474 | * @param string $url New menu URL. | ||
|  | |||
| 475 | * @param string $title New menu title. | ||
| 476 | * @param string $cap New menu capability. | ||
| 477 | * @param string $icon New menu icon. | ||
| 478 | * @param int $position New menu position. | ||
| 479 | * @return bool Whether the menu has been updated. | ||
| 480 | */ | ||
| 481 | 	public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) { | ||
| 536 | |||
| 537 | /** | ||
| 538 | * Updates the submenus of the given menu slug. | ||
| 539 | * | ||
| 540 | * @param string $slug Menu slug. | ||
| 541 | * @param array $submenus_to_update Array of new submenu slugs. | ||
| 542 | */ | ||
| 543 | 	public function update_submenus( $slug, $submenus_to_update ) { | ||
| 558 | |||
| 559 | /** | ||
| 560 | * Remove submenu items from given menu slug. | ||
| 561 | * | ||
| 562 | * @param string $slug Menu slug. | ||
| 563 | */ | ||
| 564 | 	public function remove_submenus( $slug ) { | ||
| 572 | |||
| 573 | /** | ||
| 574 | * Adds a menu separator. | ||
| 575 | * | ||
| 576 | * @param int $position The position in the menu order this item should appear. | ||
| 577 | * @param string $cap Optional. The capability required for this menu to be displayed to the user. | ||
| 578 | * Default: 'read'. | ||
| 579 | */ | ||
| 580 | 	public function add_admin_menu_separator( $position, $cap = 'read' ) { | ||
| 593 | |||
| 594 | /** | ||
| 595 | * Enqueues scripts and styles. | ||
| 596 | */ | ||
| 597 | 	public function enqueue_scripts() { | ||
| 619 | |||
| 620 | /** | ||
| 621 | * Dequeues unnecessary scripts. | ||
| 622 | */ | ||
| 623 | 	public function dequeue_scripts() { | ||
| 626 | |||
| 627 | /** | ||
| 628 | * Whether to use wp-admin pages rather than Calypso. | ||
| 629 | * | ||
| 630 | * @return bool | ||
| 631 | */ | ||
| 632 | 	public function should_link_to_wp_admin() { | ||
| 635 | } | ||
| 636 | 
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.