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) { |
|
| 164 | /** |
||
| 165 | * Set data item of specified user |
||
| 166 | * |
||
| 167 | * @param string $item Item-value array may be specified for setting several items at once |
||
| 168 | * @param int|string $value |
||
| 169 | * @param int $user If not specified - current user assumed |
||
| 170 | * @param array $data_set |
||
| 171 | * |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | 18 | protected function set_internal ($item, $value, $user, &$data_set) { |
|
| 222 | /** |
||
| 223 | * Check whether setting specified item to specified value for specified user is allowed |
||
| 224 | * |
||
| 225 | * @param int $user |
||
| 226 | * @param string $item |
||
| 227 | * @param string $value |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | 18 | protected function set_internal_allowed ($user, $item, $value) { |
|
| 262 | /** |
||
| 263 | * Get user id by login or email hash (sha224) (hash from lowercase string) |
||
| 264 | * |
||
| 265 | * @param string $login_hash Login or email hash |
||
| 266 | * |
||
| 267 | * @return false|int User id if found and not guest, otherwise - boolean <i>false</i> |
||
| 268 | */ |
||
| 269 | 20 | public function get_id ($login_hash) { |
|
| 290 | /** |
||
| 291 | * Get user avatar, if no one present - uses Gravatar |
||
| 292 | * |
||
| 293 | * @param int|null $size Avatar size, if not specified or resizing is not possible - original image is used |
||
| 294 | * @param false|int $user If not specified - current user assumed |
||
| 295 | * |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | 8 | public function avatar ($size = null, $user = false) { |
|
| 312 | /** |
||
| 313 | * Get user name or login or email, depending on existing information |
||
| 314 | * |
||
| 315 | * @param false|int $user If not specified - current user assumed |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | 4 | public function username ($user = false) { |
|
| 330 | /** |
||
| 331 | * Returns array of users columns, available for getting of data |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | 2 | public function get_users_columns () { |
|
| 338 | } |
||
| 339 |