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 | View Code Duplication | public function reregister_menu_items() { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Whether to use wp-admin pages rather than Calypso. |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function should_link_to_wp_admin() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Adds Posts menu. |
||
| 114 | * |
||
| 115 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 116 | */ |
||
| 117 | public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Adds Page menu. |
||
| 127 | * |
||
| 128 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 129 | */ |
||
| 130 | public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Adds Plugins menu. |
||
| 140 | * |
||
| 141 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 142 | */ |
||
| 143 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Adds the site switcher link if user has more than one site. |
||
| 150 | */ |
||
| 151 | public function add_browse_sites_link() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Adds a custom element class for Site Switcher menu item. |
||
| 164 | * |
||
| 165 | * @param array $menu Associative array of administration menu items. |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Adds a link to the menu to create a new site. |
||
| 184 | */ |
||
| 185 | public function add_new_site_link() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Adds site card component. |
||
| 197 | */ |
||
| 198 | public function add_site_card_menu() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Adds a custom element class and id for Site Card's menu item. |
||
| 231 | * |
||
| 232 | * @param array $menu Associative array of administration menu items. |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | public function set_site_card_menu_class( array $menu ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Returns the first available upsell nudge. |
||
| 259 | * |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function get_upsell_nudge() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Adds Upgrades menu. |
||
| 283 | * |
||
| 284 | * @param string $plan The current WPCOM plan of the blog. |
||
| 285 | */ |
||
| 286 | public function add_upgrades_menu( $plan = null ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Adds Tools menu. |
||
| 314 | * |
||
| 315 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 316 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 317 | */ |
||
| 318 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Adds Settings menu. |
||
| 325 | * |
||
| 326 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 327 | */ |
||
| 328 | public function add_options_menu( $wp_admin = false ) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Adds Appearance menu. |
||
| 343 | * |
||
| 344 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 345 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 346 | */ |
||
| 347 | View Code Duplication | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly. |
||
| 361 | * |
||
| 362 | * @param string $submenu_file The current pages $submenu_file global variable value. |
||
| 363 | * @return string | null |
||
| 364 | */ |
||
| 365 | public function override_the_theme_installer( $submenu_file ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Adds Users menu. |
||
| 376 | * |
||
| 377 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 378 | */ |
||
| 379 | View Code Duplication | public function add_users_menu( $wp_admin = false ) { |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Also remove the Gutenberg plugin 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_gutenberg_menus( $wp_admin = false ) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Adds Comments menu. |
||
| 403 | * |
||
| 404 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 405 | */ |
||
| 406 | public function add_comments_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Saves the sidebar state ( expanded / collapsed ) via an ajax request. |
||
| 413 | */ |
||
| 414 | public function ajax_sidebar_state() { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Checks whether Calypso links should be enforced. |
||
| 431 | * |
||
| 432 | * @return bool |
||
| 433 | */ |
||
| 434 | public function should_force_calypso_links() { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Checks whether the navigation customizations should be performed. |
||
| 441 | * |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | public function should_customize_nav() { |
||
| 448 | } |
||
| 449 |
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: