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 |
||
| 18 | class Atomic_Admin_Menu extends Admin_Menu { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Atomic_Admin_Menu constructor. |
||
| 22 | */ |
||
| 23 | protected function __construct() { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Dequeues unnecessary scripts. |
||
| 45 | */ |
||
| 46 | public function dequeue_scripts() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Determines whether the current locale is right-to-left (RTL). |
||
| 52 | * |
||
| 53 | * Performs the check against the current locale set on the WordPress.com's account settings. |
||
| 54 | * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`. |
||
| 55 | */ |
||
| 56 | public function is_rtl() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Create the desired menu output. |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function reregister_menu_items() { |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Forces Posts menu to WPAdmin for Atomic sites only. |
||
| 84 | * Overloads `add_posts_menu` in parent class. |
||
| 85 | * |
||
| 86 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 87 | */ |
||
| 88 | public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Forces Pages menu to WPAdmin for Atomic sites only. |
||
| 94 | * Overloads `add_page_menu` in parent class. |
||
| 95 | * |
||
| 96 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 97 | */ |
||
| 98 | public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Adds Plugins menu. |
||
| 104 | * |
||
| 105 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 106 | */ |
||
| 107 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Adds the site switcher link if user has more than one site. |
||
| 114 | */ |
||
| 115 | public function add_browse_sites_link() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Adds a custom element class for Site Switcher menu item. |
||
| 128 | * |
||
| 129 | * @param array $menu Associative array of administration menu items. |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Adds a link to the menu to create a new site. |
||
| 148 | */ |
||
| 149 | public function add_new_site_link() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Adds site card component. |
||
| 161 | */ |
||
| 162 | public function add_site_card_menu() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Adds a custom element class and id for Site Card's menu item. |
||
| 195 | * |
||
| 196 | * @param array $menu Associative array of administration menu items. |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function set_site_card_menu_class( array $menu ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns the first available upsell nudge. |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function get_upsell_nudge() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Adds Upgrades menu. |
||
| 247 | * |
||
| 248 | * @param string $plan The current WPCOM plan of the blog. |
||
| 249 | */ |
||
| 250 | public function add_upgrades_menu( $plan = null ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Adds Tools menu. |
||
| 278 | * |
||
| 279 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 280 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 281 | */ |
||
| 282 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Adds Settings menu. |
||
| 289 | * |
||
| 290 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 291 | */ |
||
| 292 | public function add_options_menu( $wp_admin = false ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Adds Appearance menu. |
||
| 305 | * |
||
| 306 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 307 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 308 | */ |
||
| 309 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly. |
||
| 318 | * |
||
| 319 | * @param string $submenu_file The current pages $submenu_file global variable value. |
||
| 320 | * @return string | null |
||
| 321 | */ |
||
| 322 | public function override_the_theme_installer( $submenu_file ) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Adds Users menu. |
||
| 333 | * |
||
| 334 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 335 | */ |
||
| 336 | public function add_users_menu( $wp_admin = false ) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Also remove the Gutenberg plugin menu. |
||
| 344 | * |
||
| 345 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 346 | */ |
||
| 347 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Always use WP Admin for comments. |
||
| 355 | * |
||
| 356 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 357 | */ |
||
| 358 | public function add_comments_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Saves the sidebar state ( expanded / collapsed ) via an ajax request. |
||
| 364 | */ |
||
| 365 | public function ajax_sidebar_state() { |
||
| 379 | } |
||
| 380 | |||
| 381 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.