Complex classes like User_PreferencesPresenter 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 User_PreferencesPresenter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class User_PreferencesPresenter { |
||
| 22 | |||
| 23 | /** @var PFUser */ |
||
| 24 | private $user; |
||
| 25 | public $can_change_real_name; |
||
| 26 | private $can_change_email; |
||
| 27 | private $can_change_password; |
||
| 28 | |||
| 29 | private $extra_user_info; |
||
| 30 | |||
| 31 | /** @var array */ |
||
| 32 | private $user_access; |
||
| 33 | |||
| 34 | /** string */ |
||
| 35 | private $third_party_html; |
||
| 36 | |||
| 37 | private $ssh_keys_extra_html; |
||
| 38 | |||
| 39 | /** @var SVN_TokenPresenter[] */ |
||
| 40 | public $svn_tokens; |
||
| 41 | |||
| 42 | public $csrf_input_html; |
||
| 43 | |||
| 44 | /** @var array */ |
||
| 45 | public $tracker_formats; |
||
| 46 | |||
| 47 | /** @var array */ |
||
| 48 | public $all_themes; |
||
| 49 | |||
| 50 | /** @var array */ |
||
| 51 | public $languages_html; |
||
| 52 | |||
| 53 | /** @var array */ |
||
| 54 | public $user_helper_preferences; |
||
| 55 | |||
| 56 | /** @var array */ |
||
| 57 | public $plugins_prefs; |
||
| 58 | |||
| 59 | /** @var array */ |
||
| 60 | public $all_csv_separator; |
||
| 61 | |||
| 62 | /** @var array */ |
||
| 63 | public $all_csv_dateformat; |
||
| 64 | |||
| 65 | /** @var string */ |
||
| 66 | public $last_svn_token; |
||
| 67 | |||
| 68 | public function __construct( |
||
| 69 | PFUser $user, |
||
| 70 | $can_change_real_name, |
||
| 71 | $can_change_email, |
||
| 72 | $can_change_password, |
||
| 73 | array $extra_user_info, |
||
| 74 | array $user_access, |
||
| 75 | $ssh_keys_extra_html, |
||
| 76 | $svn_tokens, |
||
| 77 | $third_party_html, |
||
| 78 | $csrf_input_html, |
||
| 79 | array $tracker_formats, |
||
| 80 | array $all_themes, |
||
| 81 | array $languages_html, |
||
| 82 | array $user_helper_preferences, |
||
| 83 | array $plugins_prefs, |
||
| 84 | array $all_csv_separator, |
||
| 85 | array $all_csv_dateformat, |
||
| 86 | $last_svn_token |
||
| 87 | ) { |
||
| 88 | $this->user = $user; |
||
| 89 | $this->can_change_real_name = $can_change_real_name; |
||
| 90 | $this->can_change_email = $can_change_email; |
||
| 91 | $this->can_change_password = $can_change_password; |
||
| 92 | $this->extra_user_info = $extra_user_info; |
||
| 93 | $this->user_access = $user_access; |
||
| 94 | $this->ssh_keys_extra_html = $ssh_keys_extra_html; |
||
| 95 | $this->svn_tokens = $svn_tokens; |
||
| 96 | $this->third_party_html = $third_party_html; |
||
| 97 | $this->csrf_input_html = $csrf_input_html; |
||
| 98 | $this->tracker_formats = $tracker_formats; |
||
| 99 | $this->all_themes = $all_themes; |
||
| 100 | $this->languages_html = $languages_html; |
||
| 101 | $this->user_helper_preferences = $user_helper_preferences; |
||
| 102 | $this->plugins_prefs = $plugins_prefs; |
||
| 103 | $this->all_csv_separator = $all_csv_separator; |
||
| 104 | $this->all_csv_dateformat = $all_csv_dateformat; |
||
| 105 | $this->last_svn_token = $last_svn_token; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function generated_svn_token() { |
||
| 111 | |||
| 112 | public function has_avatar() { |
||
| 115 | |||
| 116 | public function avatar() { |
||
| 119 | |||
| 120 | public function change_real_name() { |
||
| 123 | |||
| 124 | public function real_name() { |
||
| 127 | |||
| 128 | public function user_username() { |
||
| 131 | |||
| 132 | public function welcome_user() { |
||
| 135 | |||
| 136 | public function user_id_label() { |
||
| 139 | |||
| 140 | public function user_id_value() { |
||
| 143 | |||
| 144 | public function user_email_label() { |
||
| 147 | |||
| 148 | public function user_email_value() { |
||
| 151 | |||
| 152 | public function can_change_email() { |
||
| 155 | |||
| 156 | public function change_email() { |
||
| 159 | |||
| 160 | public function can_change_password() { |
||
| 163 | |||
| 164 | public function change_password() { |
||
| 167 | |||
| 168 | public function member_since_label() { |
||
| 171 | |||
| 172 | public function member_since_value() { |
||
| 175 | |||
| 176 | public function timezone_label() { |
||
| 179 | |||
| 180 | public function timezone_value() { |
||
| 183 | |||
| 184 | public function change_timezone() { |
||
| 187 | |||
| 188 | public function extra_user_info() { |
||
| 191 | |||
| 192 | public function shell_account_title() { |
||
| 195 | |||
| 196 | public function ssh_keys_count_label() { |
||
| 199 | |||
| 200 | public function ssh_keys_count() { |
||
| 203 | |||
| 204 | public function ssh_keys_label() { |
||
| 207 | |||
| 208 | public function ssh_keys_list() { |
||
| 209 | $keys = array(); |
||
| 210 | foreach ($this->user->getAuthorizedKeysArray() as $ssh_key_number => $ssh_key_value) { |
||
|
|
|||
| 211 | $keys[] = array( |
||
| 212 | 'ssh_key_ellipsis_value' => substr($ssh_key_value, 0, 40).'...'.substr($ssh_key_value, -40), |
||
| 213 | 'ssh_key_value' => $ssh_key_value, |
||
| 214 | 'ssh_key_number' => $ssh_key_number |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | return $keys; |
||
| 218 | } |
||
| 219 | |||
| 220 | public function ssh_keys_extra_html() { |
||
| 223 | |||
| 224 | public function authentication_attempts_title() { |
||
| 227 | |||
| 228 | public function last_successful_login_label() { |
||
| 231 | |||
| 232 | public function last_successful_login_value() { |
||
| 235 | |||
| 236 | public function last_login_failure_label() { |
||
| 239 | |||
| 240 | public function last_login_failure_value() { |
||
| 243 | |||
| 244 | public function number_login_failure_label() { |
||
| 247 | |||
| 248 | public function number_login_failure_value() { |
||
| 251 | |||
| 252 | public function previous_successful_login_label() { |
||
| 255 | |||
| 256 | public function previous_successful_login_value() { |
||
| 259 | |||
| 260 | public function third_party_applications_title() { |
||
| 263 | |||
| 264 | public function third_party_applications_content() { |
||
| 267 | |||
| 268 | public function user_legal() { |
||
| 269 | ob_start(); |
||
| 270 | include $GLOBALS['Language']->getContent('account/user_legal'); |
||
| 271 | return ob_get_clean(); |
||
| 272 | } |
||
| 273 | |||
| 274 | public function add_ssh_key_button() { |
||
| 277 | |||
| 278 | public function delete_ssh_key_button() { |
||
| 281 | |||
| 282 | public function has_ssh_key() { |
||
| 285 | |||
| 286 | public function ssh_keys_no_key() { |
||
| 289 | |||
| 290 | public function has_svn_tokens() { |
||
| 293 | |||
| 294 | public function svn_tokens_title() { |
||
| 297 | |||
| 298 | public function svn_tokens_help() { |
||
| 301 | |||
| 302 | public function svn_tokens_no_token() { |
||
| 305 | |||
| 306 | public function svn_token_generated_date() { |
||
| 309 | |||
| 310 | public function svn_token_last_usage() { |
||
| 313 | |||
| 314 | public function svn_token_last_ip() { |
||
| 317 | |||
| 318 | public function svn_token_comment() { |
||
| 321 | |||
| 322 | public function generate_svn_token_button() { |
||
| 325 | |||
| 326 | public function delete_svn_tokens_button() { |
||
| 329 | |||
| 330 | public function generate_svn_token_modal_title() { |
||
| 333 | |||
| 334 | public function generate_svn_token_modal_button() { |
||
| 337 | |||
| 338 | public function generate_svn_token_modal_button_comment_label() { |
||
| 341 | |||
| 342 | public function generate_svn_token_modal_button_comment_placeholder() { |
||
| 345 | |||
| 346 | public function generate_svn_token_modal_button_help() { |
||
| 349 | |||
| 350 | |||
| 351 | |||
| 352 | /* PREFERENCES */ |
||
| 353 | |||
| 354 | public function preference_title() { |
||
| 357 | |||
| 358 | public function email_settings() { |
||
| 361 | |||
| 362 | public function user_has_mail_site_updates() { |
||
| 365 | |||
| 366 | public function user_has_sticky_login() { |
||
| 369 | |||
| 370 | public function user_has_mail_va() { |
||
| 373 | |||
| 374 | public function site_update_label() { |
||
| 377 | |||
| 378 | public function community_mail_label() { |
||
| 381 | |||
| 382 | public function tracker_mail_format_label() { |
||
| 385 | |||
| 386 | public function tracker_mail_format_select_name() { |
||
| 389 | |||
| 390 | public function session_label() { |
||
| 393 | |||
| 394 | public function remember_me() { |
||
| 397 | |||
| 398 | public function lab_features_title() { |
||
| 401 | |||
| 402 | public function lab_features_description() { |
||
| 405 | |||
| 406 | public function user_uses_lab_features() { |
||
| 409 | |||
| 410 | public function lab_features_checkbox_label() { |
||
| 413 | |||
| 414 | public function lab_features_default_image() { |
||
| 417 | |||
| 418 | public function appearance_title() { |
||
| 421 | |||
| 422 | public function theme_label() { |
||
| 425 | |||
| 426 | public function default_theme() { |
||
| 429 | |||
| 430 | public function theme_variant_label() { |
||
| 433 | |||
| 434 | public function language_label() { |
||
| 437 | |||
| 438 | public function username_display_label() { |
||
| 441 | |||
| 442 | public function import_export_title() { |
||
| 445 | |||
| 446 | public function csv_separator_label() { |
||
| 449 | |||
| 450 | public function csv_dateformat_label() { |
||
| 453 | |||
| 454 | public function preference_save_button() { |
||
| 457 | |||
| 458 | /* MODAL */ |
||
| 459 | |||
| 460 | public function add_keys_modal_title() { |
||
| 463 | |||
| 464 | public function btn_close_label() { |
||
| 467 | |||
| 468 | public function btn_save_keys_label() { |
||
| 471 | } |
||
| 472 |