Complex classes like Data 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 Data, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | trait Data { |
||
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 | /** |
||
39 | * Changed users data, at the finish, data in db must be replaced by this data |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $data_set = []; |
||
43 | /** |
||
44 | * Whether to use memory cache (locally, inside object, may require a lot of memory if working with many users together) |
||
45 | * @var bool |
||
46 | */ |
||
47 | protected $memory_cache = true; |
||
48 | protected function initialize_data () { |
||
56 | /** |
||
57 | * Get data item of specified user |
||
58 | * |
||
59 | * @param string|string[] $item |
||
60 | * @param false|int $user If not specified - current user assumed |
||
61 | * |
||
62 | * @return false|int|mixed[]|string|Properties If <i>$item</i> is integer - cs\User\Properties object will be returned |
||
63 | */ |
||
64 | function get ($item, $user = false) { |
||
70 | /** |
||
71 | * Get data item of specified user |
||
72 | * |
||
73 | * @todo Refactor this to select all or nothing; this selection of only necessary stuff is tricky and should be simplified |
||
74 | * |
||
75 | * @param string|string[] $item |
||
76 | * @param false|int $user If not specified - current user assumed |
||
77 | * |
||
78 | * @return false|int|string|mixed[] |
||
79 | */ |
||
80 | protected function get_internal ($item, $user = false) { |
||
123 | /** |
||
124 | * Set data item of specified user |
||
125 | * |
||
126 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
127 | * @param int|null|string $value |
||
128 | * @param false|int $user If not specified - current user assumed |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | function set ($item, $value = null, $user = false) { |
||
137 | /** |
||
138 | * Set data item of specified user |
||
139 | * |
||
140 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
141 | * @param int|null|string $value |
||
142 | * @param false|int $user If not specified - current user assumed |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | protected function set_internal ($item, $value = null, $user = false) { |
||
202 | /** |
||
203 | * Check whether setting specified item to specified value for specified user is allowed |
||
204 | * |
||
205 | * @param int $user |
||
206 | * @param string $item |
||
207 | * @param string $value |
||
208 | * |
||
209 | * @return bool |
||
210 | */ |
||
211 | protected function set_internal_allowed ($user, $item, $value) { |
||
235 | /** |
||
236 | * Getting additional data item(s) of specified user |
||
237 | * |
||
238 | * @param string|string[] $item |
||
239 | * @param false|int $user If not specified - current user assumed |
||
240 | * |
||
241 | * @return false|string|mixed[] |
||
242 | */ |
||
243 | function get_data ($item, $user = false) { |
||
314 | /** |
||
315 | * Setting additional data item(s) of specified user |
||
316 | * |
||
317 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
318 | * @param mixed|null $value |
||
319 | * @param false|int $user If not specified - current user assumed |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | function set_data ($item, $value = null, $user = false) { |
||
369 | /** |
||
370 | * Deletion of additional data item(s) of specified user |
||
371 | * |
||
372 | * @param string|string[] $item |
||
373 | * @param false|int $user If not specified - current user assumed |
||
374 | * |
||
375 | * @return bool |
||
376 | */ |
||
377 | function del_data ($item, $user = false) { |
||
395 | /** |
||
396 | * Get user id by login or email hash (sha224) (hash from lowercase string) |
||
397 | * |
||
398 | * @param string $login_hash Login or email hash |
||
399 | * |
||
400 | * @return false|int User id if found and not guest, otherwise - boolean <i>false</i> |
||
401 | */ |
||
402 | function get_id ($login_hash) { |
||
425 | /** |
||
426 | * Get user avatar, if no one present - uses Gravatar |
||
427 | * |
||
428 | * @param int|null $size Avatar size, if not specified or resizing is not possible - original image is used |
||
429 | * @param false|int $user If not specified - current user assumed |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | function avatar ($size = null, $user = false) { |
||
447 | /** |
||
448 | * Get user name or login or email, depending on existing information |
||
449 | * |
||
450 | * @param false|int $user If not specified - current user assumed |
||
451 | * |
||
452 | * @return string |
||
453 | */ |
||
454 | function username ($user = false) { |
||
468 | /** |
||
469 | * Disable memory cache |
||
470 | * |
||
471 | * Memory cache stores users data inside User class in order to get data faster next time. |
||
472 | * But in case of working with large amount of users this cache can be too large. Disabling will cause some performance drop, but save a lot of RAM. |
||
473 | */ |
||
474 | function disable_memory_cache () { |
||
477 | /** |
||
478 | * Returns array of users columns, available for getting of data |
||
479 | * |
||
480 | * @return array |
||
481 | */ |
||
482 | function get_users_columns () { |
||
485 | /** |
||
486 | * Saving changes of cache and users data |
||
487 | */ |
||
488 | protected function persist_data () { |
||
509 | } |
||
510 |