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 A8C_WPCOM_Masterbar 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 A8C_WPCOM_Masterbar, and based on these observations, apply Extract Interface, too.
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 8 | class A8C_WPCOM_Masterbar { |
||
| 9 | /** |
||
| 10 | * Use for testing changes made to remotely enqueued scripts and styles on your sandbox. |
||
| 11 | * If not set it will default to loading the ones from WordPress.com. |
||
| 12 | * |
||
| 13 | * @var string $sandbox_url |
||
| 14 | */ |
||
| 15 | private $sandbox_url = ''; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Current locale. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $locale; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Current User ID. |
||
| 26 | * |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | private $user_id; |
||
| 30 | /** |
||
| 31 | * WordPress.com user data of the connected user. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $user_data; |
||
| 36 | /** |
||
| 37 | * WordPress.com username for the connected user. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $user_login; |
||
| 42 | /** |
||
| 43 | * WordPress.com email address for the connected user. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $user_email; |
||
| 48 | /** |
||
| 49 | * WordPress.com display name for the connected user. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $display_name; |
||
| 54 | /** |
||
| 55 | * Site URL sanitized for usage in WordPress.com slugs. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $primary_site_slug; |
||
| 60 | /** |
||
| 61 | * Text direction (ltr or rtl) based on connected WordPress.com user's interface settings. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $user_text_direction; |
||
| 66 | /** |
||
| 67 | * Number of sites owned by connected WordPress.com user. |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | private $user_site_count; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Constructor |
||
| 75 | */ |
||
| 76 | public function __construct() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get class name for RTL sites. |
||
| 148 | */ |
||
| 149 | public function get_rtl_admin_bar_class() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Adds CSS classes to admin body tag. |
||
| 155 | * |
||
| 156 | * @since 5.1 |
||
| 157 | * |
||
| 158 | * @param string $admin_body_classes CSS classes that will be added. |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function admin_body_class( $admin_body_classes ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Remove the default Admin Bar CSS. |
||
| 168 | */ |
||
| 169 | public function remove_core_styles() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Check if the user settings are for an RTL language or not. |
||
| 175 | */ |
||
| 176 | public function is_rtl() { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Enqueue our own CSS and JS to display our custom admin bar. |
||
| 182 | */ |
||
| 183 | public function add_styles_and_scripts() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get base URL where our CSS and JS will come from. |
||
| 230 | * |
||
| 231 | * @param string $file File path for a static resource. |
||
| 232 | */ |
||
| 233 | private function wpcom_static_url( $file ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Remove the default admin bar items and replace it with our own admin bar. |
||
| 247 | */ |
||
| 248 | public function replace_core_masterbar() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Remove all existing toolbar entries from core Masterbar |
||
| 261 | * |
||
| 262 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
| 263 | */ |
||
| 264 | public function clear_core_masterbar( $wp_admin_bar ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Add entries corresponding to WordPress.com Masterbar |
||
| 272 | * |
||
| 273 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
| 274 | */ |
||
| 275 | public function build_wpcom_masterbar( $wp_admin_bar ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get WordPress.com current locale name. |
||
| 294 | */ |
||
| 295 | public function get_locale() { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Add the Notifications menu item. |
||
| 316 | * |
||
| 317 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
| 318 | */ |
||
| 319 | public function add_notifications( $wp_admin_bar ) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Add the "My Site" menu item in the root default group. |
||
| 345 | * |
||
| 346 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
| 347 | */ |
||
| 348 | public function add_my_sites_submenu( $wp_admin_bar ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Add the "Reader" menu item in the root default group. |
||
| 364 | * |
||
| 365 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
| 366 | */ |
||
| 367 | public function add_reader_submenu( $wp_admin_bar ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Define main groups used in our admin bar. |
||
| 383 | * |
||
| 384 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
| 385 | */ |
||
| 386 | public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Add User info menu item. |
||
| 408 | * |
||
| 409 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
| 410 | */ |
||
| 411 | public function add_me_submenu( $wp_admin_bar ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Add Write Menu item. |
||
| 436 | * |
||
| 437 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
| 438 | */ |
||
| 439 | public function add_write_button( $wp_admin_bar ) { |
||
| 467 | } |
||
| 468 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: