Complex classes like profile 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 profile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | trait profile { |
||
21 | static function profile_get () { |
||
22 | $User = User::instance(); |
||
23 | $fields = [ |
||
24 | 'id', |
||
25 | 'login', |
||
26 | 'username', |
||
27 | 'language', |
||
28 | 'timezone', |
||
29 | 'avatar' |
||
30 | ]; |
||
31 | $result = $User->get($fields, $User->id); |
||
32 | $result['id'] = (int)$result['id']; |
||
33 | if ($User->guest()) { |
||
34 | $result['username'] = Language::instance()->system_profile_guest; |
||
35 | $result['avatar'] = $User->avatar(); |
||
36 | } |
||
37 | return $result; |
||
38 | } |
||
39 | /** |
||
40 | * @param \cs\Request $Request |
||
41 | * |
||
42 | * @throws ExitException |
||
43 | */ |
||
44 | static function profile_patch ($Request) { |
||
45 | $user_data = $Request->data('login', 'username', 'language', 'timezone', 'avatar'); |
||
46 | if ( |
||
47 | !$user_data || |
||
48 | !$user_data['login'] |
||
49 | ) { |
||
50 | throw new ExitException(400); |
||
51 | } |
||
52 | $Config = Config::instance(); |
||
53 | $User = User::instance(); |
||
54 | if ($User->guest()) { |
||
55 | throw new ExitException(403); |
||
56 | } |
||
57 | $user_data = xap($user_data, false); |
||
58 | if ( |
||
59 | ( |
||
60 | $user_data['language'] && |
||
61 | !in_array($user_data['language'], $Config->core['active_languages']) |
||
62 | ) || |
||
63 | ( |
||
64 | $user_data['timezone'] && |
||
65 | !in_array($user_data['timezone'], get_timezones_list()) |
||
66 | ) |
||
67 | ) { |
||
68 | throw new ExitException(400); |
||
69 | } |
||
70 | $user_data['login'] = mb_strtolower($user_data['login']); |
||
71 | /** |
||
72 | * Check for changing login to new one and whether it is available |
||
73 | */ |
||
74 | if ( |
||
75 | $user_data['login'] != $User->login && |
||
76 | $user_data['login'] != $User->email && |
||
77 | ( |
||
78 | filter_var($user_data['login'], FILTER_VALIDATE_EMAIL) || |
||
79 | $User->get_id(hash('sha224', $user_data['login'])) !== false |
||
80 | ) |
||
81 | ) { |
||
82 | throw new ExitException(Language::instance()->system_admin_users_login_occupied, 400); |
||
83 | } |
||
84 | if (!$User->set($user_data)) { |
||
85 | throw new ExitException(500); |
||
86 | } |
||
87 | } |
||
88 | /** |
||
89 | * @param \cs\Request $Request |
||
90 | * |
||
91 | * @throws ExitException |
||
92 | */ |
||
93 | static function profile_change_password ($Request) { |
||
94 | $L = Language::prefix('system_profile_'); |
||
95 | $User = User::instance(); |
||
96 | $data = $Request->data('current_password', 'new_password'); |
||
97 | if (!$data) { |
||
98 | throw new ExitException(400); |
||
99 | } |
||
100 | if (!$User->user()) { |
||
101 | throw new ExitException(403); |
||
102 | } |
||
103 | if (!$data['new_password']) { |
||
104 | throw new ExitException($L->please_type_new_password, 400); |
||
105 | } |
||
106 | if (!$User->validate_password($data['current_password'], $User->id, true)) { |
||
107 | throw new ExitException($L->wrong_current_password, 400); |
||
108 | } |
||
109 | $id = $User->id; |
||
110 | if ($User->set_password($data['new_password'], $id, true)) { |
||
111 | Session::instance()->add($id); |
||
112 | } else { |
||
113 | throw new ExitException($L->change_password_server_error, 400); |
||
114 | } |
||
115 | } |
||
116 | /** |
||
117 | * @param \cs\Request $Request |
||
118 | * @param \cs\Response $Response |
||
119 | * |
||
120 | * @throws ExitException |
||
121 | */ |
||
122 | static function profile_registration ($Request, $Response) { |
||
123 | $Config = Config::instance(); |
||
124 | $L = Language::prefix('system_profile_registration_'); |
||
125 | $User = User::instance(); |
||
126 | if (!$User->guest()) { |
||
127 | throw new ExitException(403); |
||
128 | } |
||
129 | if (!$Config->core['allow_user_registration']) { |
||
130 | throw new ExitException($L->prohibited, 403); |
||
131 | } |
||
132 | $email = $Request->data('email'); |
||
133 | $email = mb_strtolower($email); |
||
134 | $result = static::try_to_register($User, $L, $email); |
||
135 | $confirm = $result['reg_key'] !== true; |
||
136 | static::fill_optional_profile_data($Request, $User, $result['id']); |
||
137 | $title = $L->success_mail(get_core_ml_text('name')); |
||
138 | $body = $L->success_mail( |
||
139 | $User->username($result['id']), |
||
140 | get_core_ml_text('name'), |
||
141 | $Config->core_url().'/profile/settings', |
||
142 | $User->get('login', $result['id']) |
||
143 | ); |
||
144 | if ($confirm) { |
||
145 | $title = $L->need_confirmation_mail(get_core_ml_text('name')); |
||
146 | $body = $L->need_confirmation_mail_body( |
||
147 | $User->username($result['id']), |
||
148 | get_core_ml_text('name'), |
||
149 | $Config->core_url()."/profile/registration_confirmation/$result[reg_key]", |
||
150 | $L->time($Config->core['registration_confirmation_time'], 'd') |
||
151 | ); |
||
152 | } elseif (!$Request->data('password') && $result['password']) { |
||
153 | $body = $L->success_mail_with_password_body( |
||
154 | $User->username($result['id']), |
||
155 | get_core_ml_text('name'), |
||
156 | $Config->core_url().'/profile/settings', |
||
157 | $User->get('login', $result['id']), |
||
158 | $result['password'] |
||
159 | ); |
||
160 | } |
||
161 | if (!Mail::instance()->send_to($email, $title, $body)) { |
||
162 | $User->registration_cancel(); |
||
163 | throw new ExitException($L->mail_sending_error, 500); |
||
164 | } |
||
165 | $Response->code = $confirm ? 202 : 201; |
||
166 | } |
||
167 | /** |
||
168 | * @param User $User |
||
169 | * @param Language\Prefix $L |
||
170 | * @param string $email |
||
171 | * |
||
172 | * @return array |
||
173 | * |
||
174 | * @throws ExitException |
||
175 | */ |
||
176 | protected static function try_to_register ($User, $L, $email) { |
||
189 | /** |
||
190 | * @param \cs\Request $Request |
||
191 | * @param User $User |
||
192 | * @param int $user_id |
||
193 | */ |
||
194 | protected static function fill_optional_profile_data ($Request, $User, $user_id) { |
||
212 | /** |
||
213 | * @param \cs\Request $Request |
||
214 | * |
||
215 | * @throws ExitException |
||
216 | */ |
||
217 | static function profile_restore_password ($Request) { |
||
249 | /** |
||
250 | * @param \cs\Request $Request |
||
251 | * |
||
252 | * @throws ExitException |
||
253 | */ |
||
254 | 4 | static function profile_sign_in ($Request) { |
|
299 | /** |
||
300 | * @param \cs\Request $Request |
||
301 | * @param \cs\Response $Response |
||
302 | * |
||
303 | * @throws ExitException |
||
304 | */ |
||
305 | static function profile_sign_out ( |
||
329 | } |
||
330 |