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 | * @param string|string[] $item |
||
| 74 | * @param false|int $user If not specified - current user assumed |
||
| 75 | * |
||
| 76 | * @return false|int|string|mixed[] |
||
| 77 | */ |
||
| 78 | protected function get_internal ($item, $user = false) { |
||
| 117 | /** |
||
| 118 | * Set data item of specified user |
||
| 119 | * |
||
| 120 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
| 121 | * @param int|null|string $value |
||
| 122 | * @param false|int $user If not specified - current user assumed |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | function set ($item, $value = null, $user = false) { |
||
| 131 | /** |
||
| 132 | * Set data item of specified user |
||
| 133 | * |
||
| 134 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
| 135 | * @param int|null|string $value |
||
| 136 | * @param false|int $user If not specified - current user assumed |
||
| 137 | * |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | protected function set_internal ($item, $value = null, $user = false) { |
||
| 193 | /** |
||
| 194 | * Check whether setting specified item to specified value for specified user is allowed |
||
| 195 | * |
||
| 196 | * @param int $user |
||
| 197 | * @param string $item |
||
| 198 | * @param string $value |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | protected function set_internal_allowed ($user, $item, $value) { |
||
| 225 | /** |
||
| 226 | * Getting additional data item(s) of specified user |
||
| 227 | * |
||
| 228 | * @param string|string[] $item |
||
| 229 | * @param false|int $user If not specified - current user assumed |
||
| 230 | * |
||
| 231 | * @return false|string|mixed[] |
||
| 232 | */ |
||
| 233 | function get_data ($item, $user = false) { |
||
| 300 | /** |
||
| 301 | * Setting additional data item(s) of specified user |
||
| 302 | * |
||
| 303 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
| 304 | * @param mixed|null $value |
||
| 305 | * @param false|int $user If not specified - current user assumed |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | function set_data ($item, $value = null, $user = false) { |
||
| 340 | /** |
||
| 341 | * Deletion of additional data item(s) of specified user |
||
| 342 | * |
||
| 343 | * @param string|string[] $item |
||
| 344 | * @param false|int $user If not specified - current user assumed |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | function del_data ($item, $user = false) { |
||
| 366 | /** |
||
| 367 | * Get user id by login or email hash (sha224) (hash from lowercase string) |
||
| 368 | * |
||
| 369 | * @param string $login_hash Login or email hash |
||
| 370 | * |
||
| 371 | * @return false|int User id if found and not guest, otherwise - boolean <i>false</i> |
||
| 372 | */ |
||
| 373 | function get_id ($login_hash) { |
||
| 394 | /** |
||
| 395 | * Get user avatar, if no one present - uses Gravatar |
||
| 396 | * |
||
| 397 | * @param int|null $size Avatar size, if not specified or resizing is not possible - original image is used |
||
| 398 | * @param false|int $user If not specified - current user assumed |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | function avatar ($size = null, $user = false) { |
||
| 416 | /** |
||
| 417 | * Get user name or login or email, depending on existing information |
||
| 418 | * |
||
| 419 | * @param false|int $user If not specified - current user assumed |
||
| 420 | * |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | function username ($user = false) { |
||
| 437 | /** |
||
| 438 | * Disable memory cache |
||
| 439 | * |
||
| 440 | * Memory cache stores users data inside User class in order to get data faster next time. |
||
| 441 | * 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. |
||
| 442 | */ |
||
| 443 | function disable_memory_cache () { |
||
| 447 | /** |
||
| 448 | * Returns array of users columns, available for getting of data |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | function get_users_columns () { |
||
| 455 | /** |
||
| 456 | * Saving changes of cache and users data |
||
| 457 | */ |
||
| 458 | protected function persist_data () { |
||
| 479 | } |
||
| 480 |