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 | 22 | protected function initialize_data () { |
|
| 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 | 16 | function get ($item, $user = false) { |
|
| 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 | 16 | 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 | 10 | function set ($item, $value = null, $user = false) { |
|
| 141 | /** |
||
| 142 | * Set data item of specified user |
||
| 143 | * |
||
| 144 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
| 145 | * @param int|null|string $value |
||
| 146 | * @param int $user If not specified - current user assumed |
||
| 147 | * @param array $data_set |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | 10 | protected function set_internal ($item, $value, $user, &$data_set) { |
|
| 207 | /** |
||
| 208 | * Check whether setting specified item to specified value for specified user is allowed |
||
| 209 | * |
||
| 210 | * @param int $user |
||
| 211 | * @param string $item |
||
| 212 | * @param string $value |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | 10 | protected function set_internal_allowed ($user, $item, $value) { |
|
| 239 | /** |
||
| 240 | * Get user id by login or email hash (sha224) (hash from lowercase string) |
||
| 241 | * |
||
| 242 | * @param string $login_hash Login or email hash |
||
| 243 | * |
||
| 244 | * @return false|int User id if found and not guest, otherwise - boolean <i>false</i> |
||
| 245 | */ |
||
| 246 | 14 | function get_id ($login_hash) { |
|
| 267 | /** |
||
| 268 | * Get user avatar, if no one present - uses Gravatar |
||
| 269 | * |
||
| 270 | * @param int|null $size Avatar size, if not specified or resizing is not possible - original image is used |
||
| 271 | * @param false|int $user If not specified - current user assumed |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | 2 | function avatar ($size = null, $user = false) { |
|
| 289 | /** |
||
| 290 | * Get user name or login or email, depending on existing information |
||
| 291 | * |
||
| 292 | * @param false|int $user If not specified - current user assumed |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | 2 | function username ($user = false) { |
|
| 310 | /** |
||
| 311 | * Returns array of users columns, available for getting of data |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | function get_users_columns () { |
||
| 318 | } |
||
| 319 |