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 Atomic_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 Atomic_Admin_Menu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Atomic_Admin_Menu extends Admin_Menu { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Atomic_Admin_Menu constructor. |
||
| 19 | */ |
||
| 20 | protected function __construct() { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Dequeues unnecessary scripts. |
||
| 42 | */ |
||
| 43 | public function dequeue_scripts() { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Determines whether the current locale is right-to-left (RTL). |
||
| 49 | * |
||
| 50 | * Performs the check against the current locale set on the WordPress.com's account settings. |
||
| 51 | * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`. |
||
| 52 | */ |
||
| 53 | public function is_rtl() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Create the desired menu output. |
||
| 59 | */ |
||
| 60 | View Code Duplication | public function reregister_menu_items() { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Forces Posts menu to WPAdmin for Atomic sites only. |
||
| 77 | * Overloads `add_posts_menu` in parent class. |
||
| 78 | * |
||
| 79 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 80 | */ |
||
| 81 | public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Forces Pages menu to WPAdmin for Atomic sites only. |
||
| 87 | * Overloads `add_page_menu` in parent class. |
||
| 88 | * |
||
| 89 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 90 | */ |
||
| 91 | public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Adds Plugins menu. |
||
| 97 | * |
||
| 98 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 99 | */ |
||
| 100 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Adds the site switcher link if user has more than one site. |
||
| 107 | */ |
||
| 108 | public function add_browse_sites_link() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Adds a custom element class for Site Switcher menu item. |
||
| 121 | * |
||
| 122 | * @param array $menu Associative array of administration menu items. |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Adds a link to the menu to create a new site. |
||
| 141 | */ |
||
| 142 | public function add_new_site_link() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Adds site card component. |
||
| 154 | */ |
||
| 155 | public function add_site_card_menu() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Adds a custom element class and id for Site Card's menu item. |
||
| 188 | * |
||
| 189 | * @param array $menu Associative array of administration menu items. |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function set_site_card_menu_class( array $menu ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Adds Upgrades menu. |
||
| 216 | * |
||
| 217 | * @param string $plan The current WPCOM plan of the blog. |
||
|
|
|||
| 218 | */ |
||
| 219 | public function add_upgrades_menu( $plan = null ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Adds Tools menu. |
||
| 247 | * |
||
| 248 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 249 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 250 | */ |
||
| 251 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Adds Settings 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_options_menu( $wp_admin = false ) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Adds Appearance menu. |
||
| 275 | * |
||
| 276 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 277 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 278 | */ |
||
| 279 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly. |
||
| 288 | * |
||
| 289 | * @param string $submenu_file The current pages $submenu_file global variable value. |
||
| 290 | * @return string | null |
||
| 291 | */ |
||
| 292 | public function override_the_theme_installer( $submenu_file ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Adds Users menu. |
||
| 303 | * |
||
| 304 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 305 | */ |
||
| 306 | public function add_users_menu( $wp_admin = false ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Also remove the Gutenberg plugin menu. |
||
| 314 | * |
||
| 315 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 316 | */ |
||
| 317 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Always use WP Admin for comments. |
||
| 325 | * |
||
| 326 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 327 | */ |
||
| 328 | public function add_comments_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Saves the sidebar state ( expanded / collapsed ) via an ajax request. |
||
| 334 | */ |
||
| 335 | public function ajax_sidebar_state() { |
||
| 349 | } |
||
| 350 | |||
| 351 |
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.