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 | 46 | protected function initialize_data () { |
|
| 39 | 46 | $this->users_columns = $this->cache->get( |
|
| 40 | 46 | 'columns', |
|
| 41 | function () { |
||
| 42 | 4 | return $this->db()->columns('[prefix]users'); |
|
| 43 | 46 | } |
|
| 44 | ); |
||
| 45 | 46 | } |
|
| 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 | 34 | public 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 | 34 | protected function get_internal ($item, $user = false) { |
|
| 69 | 34 | $user = (int)$user ?: $this->id; |
|
| 70 | 34 | if (isset($this->data[$user])) { |
|
| 71 | 32 | $data = $this->data[$user]; |
|
| 72 | } else { |
||
| 73 | 34 | $data = $this->cache->get( |
|
| 74 | $user, |
||
| 75 | function () use ($user) { |
||
| 76 | 32 | return $this->db()->qf( |
|
| 77 | "SELECT * |
||
| 78 | FROM `[prefix]users` |
||
| 79 | 32 | WHERE `id` = $user |
|
| 80 | LIMIT 1" |
||
| 81 | 32 | ) ?: false; |
|
| 82 | 34 | } |
|
| 83 | ); |
||
| 84 | 34 | if (!$data) { |
|
| 85 | 6 | return false; |
|
| 86 | 34 | } elseif ($this->memory_cache || $user == User::GUEST_ID) { |
|
| 87 | 34 | $this->data[$user] = $data; |
|
| 88 | } |
||
| 89 | } |
||
| 90 | /** |
||
| 91 | * If get an array of values |
||
| 92 | */ |
||
| 93 | 34 | if (is_array($item)) { |
|
| 94 | 32 | $result = []; |
|
| 95 | /** |
||
| 96 | * Trying to get value from the local cache, or make up an array of missing values |
||
| 97 | */ |
||
| 98 | 32 | foreach ($item as $i) { |
|
| 99 | 32 | if (in_array($i, $this->users_columns)) { |
|
| 100 | 32 | $result[$i] = $data[$i]; |
|
| 101 | } |
||
| 102 | } |
||
| 103 | 32 | return $result; |
|
| 104 | } |
||
| 105 | 32 | return in_array($item, $this->users_columns) ? $data[$item] : false; |
|
| 106 | } |
||
| 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 | 28 | public function set ($item, $value = null, $user = false) { |
|
| 117 | 28 | $user = (int)$user ?: $this->id; |
|
| 118 | 28 | $data_set = []; |
|
| 119 | 28 | $data = is_array($item) ? $item : [$item => $value]; |
|
| 120 | 28 | $result = true; |
|
| 121 | 28 | foreach (xap($data) as $i => $v) { |
|
| 122 | 28 | $result = $result && $this->set_internal($i, $v, $user, $data_set); |
|
| 123 | } |
||
| 124 | 28 | if (!$result) { |
|
| 125 | 4 | return false; |
|
| 126 | } |
||
| 127 | 28 | if (!$data_set) { |
|
| 128 | 2 | return true; |
|
| 129 | } |
||
| 130 | 28 | $this->set_internal_correct_login($data_set, $user); |
|
| 131 | 28 | $update = []; |
|
| 132 | 28 | foreach (array_keys($data_set) as $column) { |
|
| 133 | 28 | $update[] = "`$column` = ?"; |
|
| 134 | } |
||
| 135 | 28 | $update = implode(', ', $update); |
|
| 136 | 28 | $result = $this->db_prime()->q( |
|
| 137 | "UPDATE `[prefix]users` |
||
| 138 | 28 | SET $update |
|
| 139 | 28 | WHERE `id` = '$user'", |
|
| 140 | 28 | $data_set |
|
| 141 | ); |
||
| 142 | 28 | if ($result) { |
|
| 143 | 28 | unset($this->data[$user], $this->cache->$user); |
|
| 144 | } |
||
| 145 | 28 | return (bool)$result; |
|
| 146 | } |
||
| 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 | 28 | 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 | 28 | protected function set_internal_allowed ($user, $item, $value) { |
|
| 215 | if ( |
||
| 216 | 28 | $user == User::GUEST_ID || |
|
| 217 | 28 | $item == 'id' || |
|
| 218 | 28 | !in_array($item, $this->users_columns, true) |
|
| 219 | ) { |
||
| 220 | 4 | return false; |
|
| 221 | } |
||
| 222 | 28 | 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 | 28 | 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 | 28 | 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 | 28 | public function get_id ($login_hash) { |
|
| 276 | 28 | if (!preg_match('/^[0-9a-z]{56}$/', $login_hash)) { |
|
| 277 | 4 | return false; |
|
| 278 | } |
||
| 279 | 28 | $id = $this->cache->get( |
|
| 280 | $login_hash, |
||
| 281 | 28 | function () use ($login_hash) { |
|
| 282 | 28 | return (int)$this->db()->qfs( |
|
| 283 | 28 | "SELECT `id` |
|
| 284 | FROM `[prefix]users` |
||
| 285 | WHERE |
||
| 286 | `login_hash` = '%s' OR |
||
| 287 | `email_hash` = '%s' |
||
| 288 | LIMIT 1", |
||
| 289 | $login_hash, |
||
| 290 | 28 | $login_hash |
|
| 291 | 28 | ) ?: false; |
|
| 292 | 28 | } |
|
| 293 | ); |
||
| 294 | 28 | return $id && $id != User::GUEST_ID ? $id : false; |
|
| 295 | } |
||
| 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 | 8 | 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 |