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 | * Forces Posts menu to WPAdmin for Atomic sites only. |
||
| 96 | * Overloads `add_posts_menu` in parent class. |
||
| 97 | * |
||
| 98 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 99 | */ |
||
| 100 | public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Forces Pages menu to WPAdmin for Atomic sites only. |
||
| 106 | * Overloads `add_page_menu` in parent class. |
||
| 107 | * |
||
| 108 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 109 | */ |
||
| 110 | public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Adds Plugins menu. |
||
| 116 | * |
||
| 117 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 118 | */ |
||
| 119 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Adds the site switcher link if user has more than one site. |
||
| 126 | */ |
||
| 127 | public function add_browse_sites_link() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Adds a custom element class for Site Switcher menu item. |
||
| 140 | * |
||
| 141 | * @param array $menu Associative array of administration menu items. |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Adds a link to the menu to create a new site. |
||
| 160 | */ |
||
| 161 | public function add_new_site_link() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Adds site card component. |
||
| 173 | */ |
||
| 174 | public function add_site_card_menu() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Adds a custom element class and id for Site Card's menu item. |
||
| 207 | * |
||
| 208 | * @param array $menu Associative array of administration menu items. |
||
| 209 | * |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | public function set_site_card_menu_class( array $menu ) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns the first available upsell nudge. |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function get_upsell_nudge() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Adds Upgrades menu. |
||
| 259 | * |
||
| 260 | * @param string $plan The current WPCOM plan of the blog. |
||
| 261 | */ |
||
| 262 | public function add_upgrades_menu( $plan = null ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Adds Tools menu. |
||
| 290 | * |
||
| 291 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 292 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 293 | */ |
||
| 294 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Adds Settings menu. |
||
| 301 | * |
||
| 302 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 303 | */ |
||
| 304 | public function add_options_menu( $wp_admin = false ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Adds Appearance menu. |
||
| 317 | * |
||
| 318 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 319 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 320 | */ |
||
| 321 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly. |
||
| 330 | * |
||
| 331 | * @param string $submenu_file The current pages $submenu_file global variable value. |
||
| 332 | * @return string | null |
||
| 333 | */ |
||
| 334 | public function override_the_theme_installer( $submenu_file ) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Adds Users menu. |
||
| 345 | * |
||
| 346 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 347 | */ |
||
| 348 | public function add_users_menu( $wp_admin = false ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Also remove the Gutenberg plugin menu. |
||
| 356 | * |
||
| 357 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 358 | */ |
||
| 359 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Always use WP Admin for comments. |
||
| 367 | * |
||
| 368 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 369 | */ |
||
| 370 | public function add_comments_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Saves the sidebar state ( expanded / collapsed ) via an ajax request. |
||
| 376 | */ |
||
| 377 | public function ajax_sidebar_state() { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Checks whether the navigation customizations should be performed. |
||
| 394 | * |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | public function should_customize_nav() { |
||
| 401 | } |
||
| 402 |
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: