Complex classes like Management 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 Management, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | trait Management { |
||
| 23 | /** |
||
| 24 | * Id of current session |
||
| 25 | * |
||
| 26 | * @var false|string |
||
| 27 | */ |
||
| 28 | protected $session_id; |
||
| 29 | /** |
||
| 30 | * User id of current session |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $user_id; |
||
| 35 | /** |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $is_admin; |
||
| 39 | /** |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $is_user; |
||
| 43 | /** |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected $is_guest; |
||
| 47 | /** |
||
| 48 | * Use cookie as source of session id, load session |
||
| 49 | */ |
||
| 50 | protected function init_session () { |
||
| 60 | /** |
||
| 61 | * Updates information about who is user accessed by methods ::guest() ::user() admin() |
||
| 62 | */ |
||
| 63 | protected function update_user_is () { |
||
| 81 | /** |
||
| 82 | * Is admin |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | function admin () { |
||
| 89 | /** |
||
| 90 | * Is user |
||
| 91 | * |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | function user () { |
||
| 97 | /** |
||
| 98 | * Is guest |
||
| 99 | * |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | function guest () { |
||
| 105 | /** |
||
| 106 | * Returns id of current session |
||
| 107 | * |
||
| 108 | * @return false|string |
||
| 109 | */ |
||
| 110 | function get_id () { |
||
| 113 | /** |
||
| 114 | * Returns user id of current session |
||
| 115 | * |
||
| 116 | * @return int |
||
| 117 | */ |
||
| 118 | function get_user () { |
||
| 121 | /** |
||
| 122 | * Returns session details by session id |
||
| 123 | * |
||
| 124 | * @param false|null|string $session_id If `null` - loaded from `$this->session_id`, and if that also empty - from cookies |
||
| 125 | * |
||
| 126 | * @return false|array |
||
| 127 | */ |
||
| 128 | function get ($session_id) { |
||
| 133 | /** |
||
| 134 | * @param false|null|string $session_id |
||
| 135 | * |
||
| 136 | * @return false|array |
||
| 137 | */ |
||
| 138 | protected function get_internal ($session_id) { |
||
| 161 | /** |
||
| 162 | * Check whether session was not expired, user agent and IP corresponds to what is expected and user is actually active |
||
| 163 | * |
||
| 164 | * @param mixed $session_data |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | protected function is_good_session ($session_data) { |
||
| 174 | /** |
||
| 175 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
| 176 | * |
||
| 177 | * @param string $session_id |
||
| 178 | * @param string $user_agent |
||
| 179 | * @param string $remote_addr |
||
| 180 | * @param string $ip |
||
| 181 | * |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | function is_session_owner ($session_id, $user_agent, $remote_addr, $ip) { |
||
| 188 | /** |
||
| 189 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
| 190 | * |
||
| 191 | * @param array $session_data |
||
| 192 | * @param string|null $user_agent |
||
| 193 | * @param string|null $remote_addr |
||
| 194 | * @param string|null $ip |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | protected function is_session_owner_internal ($session_data, $user_agent = null, $remote_addr = null, $ip = null) { |
||
| 218 | /** |
||
| 219 | * Load session by id and return id of session owner (user), update session expiration |
||
| 220 | * |
||
| 221 | * @param false|null|string $session_id If not specified - loaded from `$this->session_id`, and if that also empty - from cookies |
||
| 222 | * |
||
| 223 | * @return int User id |
||
| 224 | */ |
||
| 225 | function load ($session_id = null) { |
||
| 250 | /** |
||
| 251 | * Initialize session (set user id, session id and update who user is) |
||
| 252 | * |
||
| 253 | * @param string $session_id |
||
| 254 | * @param int $user_id |
||
| 255 | * |
||
| 256 | * @return int User id |
||
| 257 | */ |
||
| 258 | protected function load_initialization ($session_id, $user_id) { |
||
| 264 | /** |
||
| 265 | * Whether profile is activated, not disabled and not blocked |
||
| 266 | * |
||
| 267 | * @param int $user |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | protected function is_user_active ($user) { |
||
| 315 | /** |
||
| 316 | * Create the session for the user with specified id |
||
| 317 | * |
||
| 318 | * @param int $user |
||
| 319 | * @param bool $delete_current_session |
||
| 320 | * |
||
| 321 | * @return false|string Session id on success, `false` otherwise |
||
| 322 | */ |
||
| 323 | function add ($user, $delete_current_session = true) { |
||
| 355 | /** |
||
| 356 | * @param int $user |
||
| 357 | * |
||
| 358 | * @return array Session data |
||
| 359 | */ |
||
| 360 | protected function create_unique_session ($user) { |
||
| 388 | /** |
||
| 389 | * Destroying of the session |
||
| 390 | * |
||
| 391 | * @param null|string $session_id |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | function del ($session_id = null) { |
||
| 398 | /** |
||
| 399 | * Deletion of the session |
||
| 400 | * |
||
| 401 | * @param string|null $session_id |
||
| 402 | * @param bool $create_guest_session |
||
| 403 | * |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | protected function del_internal ($session_id = null, $create_guest_session = true) { |
||
| 434 | /** |
||
| 435 | * Delete all old sessions from DB |
||
| 436 | */ |
||
| 437 | protected function delete_old_sessions () { |
||
| 443 | /** |
||
| 444 | * Deletion of all user sessions |
||
| 445 | * |
||
| 446 | * @param false|int $user If not specified - current user assumed |
||
| 447 | * |
||
| 448 | * @return bool |
||
| 449 | */ |
||
| 450 | function del_all ($user = false) { |
||
| 473 | } |
||
| 474 |