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 | * Whether the SSO module is enabled. |
||
| 21 | * |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | protected $is_sso_enabled = false; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Atomic_Admin_Menu constructor. |
||
| 28 | */ |
||
| 29 | protected function __construct() { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Dequeues unnecessary scripts. |
||
| 57 | */ |
||
| 58 | public function dequeue_scripts() { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Determines whether the current locale is right-to-left (RTL). |
||
| 64 | * |
||
| 65 | * Performs the check against the current locale set on the WordPress.com's account settings. |
||
| 66 | * See `Masterbar::__construct` in `modules/masterbar/masterbar/class-masterbar.php`. |
||
| 67 | */ |
||
| 68 | public function is_rtl() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Create the desired menu output. |
||
| 74 | */ |
||
| 75 | public function reregister_menu_items() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Whether to use wp-admin pages rather than Calypso. |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | public function should_link_to_wp_admin() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Adds Posts menu. |
||
| 109 | * |
||
| 110 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 111 | */ |
||
| 112 | public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Adds Page menu. |
||
| 122 | * |
||
| 123 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 124 | */ |
||
| 125 | public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Adds Plugins menu. |
||
| 135 | * |
||
| 136 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 137 | */ |
||
| 138 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Adds the site switcher link if user has more than one site. |
||
| 145 | */ |
||
| 146 | public function add_browse_sites_link() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Adds a custom element class for Site Switcher menu item. |
||
| 159 | * |
||
| 160 | * @param array $menu Associative array of administration menu items. |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Adds a link to the menu to create a new site. |
||
| 179 | */ |
||
| 180 | public function add_new_site_link() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Adds site card component. |
||
| 192 | */ |
||
| 193 | public function add_site_card_menu() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Adds a custom element class and id for Site Card's menu item. |
||
| 226 | * |
||
| 227 | * @param array $menu Associative array of administration menu items. |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function set_site_card_menu_class( array $menu ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Returns the first available upsell nudge. |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function get_upsell_nudge() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Adds Upgrades menu. |
||
| 278 | * |
||
| 279 | * @param string $plan The current WPCOM plan of the blog. |
||
| 280 | */ |
||
| 281 | public function add_upgrades_menu( $plan = null ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Adds Tools menu. |
||
| 309 | * |
||
| 310 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 311 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 312 | */ |
||
| 313 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Adds Settings menu. |
||
| 320 | * |
||
| 321 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 322 | */ |
||
| 323 | public function add_options_menu( $wp_admin = false ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Adds Appearance menu. |
||
| 338 | * |
||
| 339 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 340 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 341 | */ |
||
| 342 | View Code Duplication | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly. |
||
| 356 | * |
||
| 357 | * @param string $submenu_file The current pages $submenu_file global variable value. |
||
| 358 | * @return string | null |
||
| 359 | */ |
||
| 360 | public function override_the_theme_installer( $submenu_file ) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Adds Users menu. |
||
| 371 | * |
||
| 372 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 373 | */ |
||
| 374 | View Code Duplication | public function add_users_menu( $wp_admin = false ) { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Also remove the Gutenberg plugin menu. |
||
| 387 | * |
||
| 388 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 389 | */ |
||
| 390 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Adds Comments menu. |
||
| 398 | * |
||
| 399 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 400 | */ |
||
| 401 | public function add_comments_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Saves the sidebar state ( expanded / collapsed ) via an ajax request. |
||
| 408 | */ |
||
| 409 | public function ajax_sidebar_state() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Checks whether Calypso links should be enforced. |
||
| 426 | * |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | public function should_force_calypso_links() { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Checks whether the navigation customizations should be performed. |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | public function should_customize_nav() { |
||
| 443 | } |
||
| 444 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: