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() { |
||
24 | parent::__construct(); |
||
25 | |||
26 | add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 ); |
||
27 | add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 ); |
||
28 | add_action( 'wp_ajax_sidebar_state', array( $this, 'ajax_sidebar_state' ) ); |
||
29 | |||
30 | if ( ! $this->is_api_request ) { |
||
31 | add_filter( 'submenu_file', array( $this, 'override_the_theme_installer' ), 10, 2 ); |
||
32 | } |
||
33 | |||
34 | add_action( |
||
35 | 'admin_menu', |
||
36 | function () { |
||
37 | remove_action( 'admin_menu', 'gutenberg_menu', 9 ); |
||
38 | }, |
||
39 | 0 |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Dequeues unnecessary scripts. |
||
45 | */ |
||
46 | public function dequeue_scripts() { |
||
47 | wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php. |
||
48 | } |
||
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() { |
||
57 | return get_user_option( 'jetpack_wpcom_is_rtl' ); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Create the desired menu output. |
||
62 | */ |
||
63 | View Code Duplication | public function reregister_menu_items() { |
|
64 | parent::reregister_menu_items(); |
||
65 | |||
66 | $this->add_my_home_menu(); |
||
67 | |||
68 | // Not needed outside of wp-admin. |
||
69 | if ( ! $this->is_api_request ) { |
||
70 | $this->add_browse_sites_link(); |
||
71 | $this->add_site_card_menu(); |
||
72 | $this->add_new_site_link(); |
||
73 | } |
||
74 | |||
75 | ksort( $GLOBALS['menu'] ); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Forces Posts menu to WPAdmin for Atomic sites only. |
||
80 | * Overloads `add_posts_menu` in parent class. |
||
81 | * |
||
82 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
83 | */ |
||
84 | public function add_posts_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
85 | return false; // return explicit `false` to force WPAdmin links. |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Forces Pages menu to WPAdmin for Atomic sites only. |
||
90 | * Overloads `add_page_menu` in parent class. |
||
91 | * |
||
92 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
93 | */ |
||
94 | public function add_page_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
95 | return false; // return explicit `false` to force WPAdmin links. |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Adds Plugins menu. |
||
100 | * |
||
101 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
102 | */ |
||
103 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
104 | // Plugins on Atomic sites are always managed on WP Admin. |
||
105 | parent::add_plugins_menu( true ); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Adds the site switcher link if user has more than one site. |
||
110 | */ |
||
111 | public function add_browse_sites_link() { |
||
112 | $site_count = get_user_option( 'wpcom_site_count' ); |
||
113 | if ( ! $site_count || $site_count < 2 ) { |
||
114 | return; |
||
115 | } |
||
116 | |||
117 | // Add the menu item. |
||
118 | add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 ); |
||
119 | add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) ); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Adds a custom element class for Site Switcher menu item. |
||
124 | * |
||
125 | * @param array $menu Associative array of administration menu items. |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
130 | foreach ( $menu as $key => $menu_item ) { |
||
131 | if ( 'site-switcher' !== $menu_item[3] ) { |
||
132 | continue; |
||
133 | } |
||
134 | |||
135 | $menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] ); |
||
136 | break; |
||
137 | } |
||
138 | |||
139 | return $menu; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Adds a link to the menu to create a new site. |
||
144 | */ |
||
145 | public function add_new_site_link() { |
||
146 | $site_count = get_user_option( 'wpcom_site_count' ); |
||
147 | if ( $site_count && $site_count > 1 ) { |
||
148 | return; |
||
149 | } |
||
150 | |||
151 | $this->add_admin_menu_separator(); |
||
152 | add_menu_page( __( 'Add New Site', 'jetpack' ), __( 'Add New Site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt' ); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Adds site card component. |
||
157 | */ |
||
158 | public function add_site_card_menu() { |
||
188 | |||
189 | /** |
||
190 | * Adds a custom element class and id for Site Card's menu item. |
||
191 | * |
||
192 | * @param array $menu Associative array of administration menu items. |
||
193 | * |
||
194 | * @return array |
||
195 | */ |
||
196 | public function set_site_card_menu_class( array $menu ) { |
||
216 | |||
217 | /** |
||
218 | * Adds Upgrades menu. |
||
219 | * |
||
220 | * @param string $plan The current WPCOM plan of the blog. |
||
|
|||
221 | */ |
||
222 | public function add_upgrades_menu( $plan = null ) { |
||
223 | $products = Jetpack_Plan::get(); |
||
224 | if ( array_key_exists( 'product_name_short', $products ) ) { |
||
225 | $plan = $products['product_name_short']; |
||
226 | } |
||
231 | |||
232 | /** |
||
233 | * Adds Tools menu. |
||
234 | * |
||
235 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
236 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
237 | */ |
||
238 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
242 | |||
243 | /** |
||
244 | * Adds Settings menu. |
||
245 | * |
||
246 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
247 | */ |
||
248 | public function add_options_menu( $wp_admin = false ) { |
||
259 | |||
260 | /** |
||
261 | * Adds Appearance menu. |
||
262 | * |
||
263 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
264 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
265 | */ |
||
266 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
272 | |||
273 | /** |
||
274 | * Override the global submenu_file for theme-install.php page so the WP Admin menu item gets highlighted correctly. |
||
275 | * |
||
276 | * @param string $submenu_file The current pages $submenu_file global variable value. |
||
277 | * @return string | null |
||
278 | */ |
||
279 | public function override_the_theme_installer( $submenu_file ) { |
||
287 | |||
288 | /** |
||
289 | * Adds Users menu. |
||
290 | * |
||
291 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
292 | */ |
||
293 | public function add_users_menu( $wp_admin = false ) { |
||
298 | |||
299 | /** |
||
300 | * Also remove the Gutenberg plugin 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_gutenberg_menus( $wp_admin = false ) { |
||
309 | |||
310 | /** |
||
311 | * Saves the sidebar state ( expanded / collapsed ) via an ajax request. |
||
312 | */ |
||
313 | public function ajax_sidebar_state() { |
||
327 | } |
||
328 | |||
329 |
This check looks for
@param
annotations 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.