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 |
||
27 | trait Profile { |
||
28 | /** |
||
29 | * Copy of columns list of users table for internal needs without Cache usage |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $users_columns = []; |
||
33 | /** |
||
34 | * Local cache of users data |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $data = []; |
||
38 | 32 | protected function initialize_data () { |
|
39 | 32 | $this->users_columns = $this->cache->get( |
|
40 | 32 | 'columns', |
|
41 | function () { |
||
42 | 4 | return $this->db()->columns('[prefix]users'); |
|
43 | 32 | } |
|
44 | ); |
||
45 | 32 | } |
|
46 | /** |
||
47 | * Get data item of specified user |
||
48 | * |
||
49 | * @param string|string[] $item |
||
50 | * @param false|int $user If not specified - current user assumed |
||
51 | * |
||
52 | * @return false|int|mixed[]|string|Properties If <i>$item</i> is integer - cs\User\Properties object will be returned |
||
53 | */ |
||
54 | 26 | public function get ($item, $user = false) { |
|
55 | 26 | if (is_scalar($item) && ctype_digit((string)$item)) { |
|
56 | 2 | return new Properties($item); |
|
57 | } |
||
58 | 26 | return $this->get_internal($item, $user); |
|
59 | } |
||
60 | /** |
||
61 | * Get data item of specified user |
||
62 | * |
||
63 | * @param string|string[] $item |
||
64 | * @param false|int $user If not specified - current user assumed |
||
65 | * |
||
66 | * @return false|int|string|mixed[] |
||
67 | */ |
||
68 | 26 | protected function get_internal ($item, $user = false) { |
|
107 | /** |
||
108 | * Set data item of specified user |
||
109 | * |
||
110 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
111 | * @param int|null|string $value |
||
112 | * @param false|int $user If not specified - current user assumed |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | 18 | public function set ($item, $value = null, $user = false) { |
|
147 | /** |
||
148 | * Set data item of specified user |
||
149 | * |
||
150 | * @param string $item Item-value array may be specified for setting several items at once |
||
151 | * @param int|string $value |
||
152 | * @param int $user If not specified - current user assumed |
||
153 | * @param array $data_set |
||
154 | * |
||
155 | * @return bool |
||
156 | */ |
||
157 | 18 | protected function set_internal ($item, $value, $user, &$data_set) { |
|
205 | /** |
||
206 | * Check whether setting specified item to specified value for specified user is allowed |
||
207 | * |
||
208 | * @param int $user |
||
209 | * @param string $item |
||
210 | * @param string $value |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | 18 | protected function set_internal_allowed ($user, $item, $value) { |
|
215 | if ( |
||
216 | 18 | $user == User::GUEST_ID || |
|
217 | 18 | $item == 'id' || |
|
218 | 18 | !in_array($item, $this->users_columns, true) |
|
219 | ) { |
||
220 | 4 | return false; |
|
221 | } |
||
222 | 18 | if (in_array($item, ['login', 'email'], true)) { |
|
223 | 4 | $value = mb_strtolower($value); |
|
224 | if ( |
||
225 | 4 | $item == 'email' && |
|
226 | 4 | !filter_var($value, FILTER_VALIDATE_EMAIL) |
|
227 | ) { |
||
228 | 2 | return false; |
|
229 | } |
||
230 | if ( |
||
231 | 4 | $item == 'login' && |
|
232 | 4 | filter_var($value, FILTER_VALIDATE_EMAIL) && |
|
233 | 4 | $value != $this->get('email', $user) |
|
234 | ) { |
||
235 | 2 | return false; |
|
236 | } |
||
237 | 4 | if ($value == $this->get($item, $user)) { |
|
238 | 4 | return true; |
|
239 | } |
||
240 | 4 | $existing_user = $this->get_id(hash('sha224', $value)) ?: $user; |
|
241 | 4 | return $value && $existing_user == $user; |
|
242 | } |
||
243 | 18 | return true; |
|
244 | } |
||
245 | /** |
||
246 | * A bit tricky here |
||
247 | * |
||
248 | * User is allowed to change login to own email, but not to any other email. However, when user changes email, it might happen that login will remain to |
||
249 | * be the same as previous email, so we need to change login to new email as well. |
||
250 | * |
||
251 | * @param array $data_set |
||
252 | * @param int $user |
||
253 | */ |
||
254 | 18 | protected function set_internal_correct_login (&$data_set, $user) { |
|
268 | /** |
||
269 | * Get user id by login or email hash (sha224) (hash from lowercase string) |
||
270 | * |
||
271 | * @param string $login_hash Login or email hash |
||
272 | * |
||
273 | * @return false|int User id if found and not guest, otherwise - boolean <i>false</i> |
||
274 | */ |
||
275 | 20 | public function get_id ($login_hash) { |
|
296 | /** |
||
297 | * Get user avatar, if no one present - uses Gravatar |
||
298 | * |
||
299 | * @param int|null $size Avatar size, if not specified or resizing is not possible - original image is used |
||
300 | * @param false|int $user If not specified - current user assumed |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | 8 | public function avatar ($size = null, $user = false) { |
|
318 | /** |
||
319 | * Get user name or login or email, depending on existing information |
||
320 | * |
||
321 | * @param false|int $user If not specified - current user assumed |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | 4 | public function username ($user = false) { |
|
336 | /** |
||
337 | * Returns array of users columns, available for getting of data |
||
338 | * |
||
339 | * @return array |
||
340 | */ |
||
341 | 2 | public function get_users_columns () { |
|
344 | } |
||
345 |