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 |
||
| 20 | trait Management { |
||
| 21 | /** |
||
| 22 | * Id of current session |
||
| 23 | * |
||
| 24 | * @var false|string |
||
| 25 | */ |
||
| 26 | protected $session_id; |
||
| 27 | /** |
||
| 28 | * User id of current session |
||
| 29 | * |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | protected $user_id; |
||
| 33 | /** |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | protected $is_admin; |
||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | protected $is_user; |
||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $is_guest; |
||
| 45 | /** |
||
| 46 | * Use cookie as source of session id, load session |
||
| 47 | */ |
||
| 48 | 46 | protected function init_session () { |
|
| 58 | /** |
||
| 59 | * Updates information about who is user accessed by methods ::guest() ::user() admin() |
||
| 60 | */ |
||
| 61 | 46 | protected function update_user_is () { |
|
| 79 | /** |
||
| 80 | * Is admin |
||
| 81 | * |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | 10 | public function admin () { |
|
| 87 | /** |
||
| 88 | * Is user |
||
| 89 | * |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | 4 | public function user () { |
|
| 95 | /** |
||
| 96 | * Is guest |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | 16 | public function guest () { |
|
| 103 | /** |
||
| 104 | * Returns id of current session |
||
| 105 | * |
||
| 106 | * @return false|string |
||
| 107 | */ |
||
| 108 | 38 | public function get_id () { |
|
| 111 | /** |
||
| 112 | * Returns user id of current session |
||
| 113 | * |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | 46 | public function get_user () { |
|
| 119 | /** |
||
| 120 | * Returns session details by session id |
||
| 121 | * |
||
| 122 | * @param false|null|string $session_id If `null` - loaded from `$this->session_id`, and if that also empty - from cookies |
||
| 123 | * |
||
| 124 | * @return false|array |
||
| 125 | */ |
||
| 126 | 2 | public function get ($session_id) { |
|
| 131 | /** |
||
| 132 | * @param false|null|string $session_id |
||
| 133 | * |
||
| 134 | * @return false|array |
||
| 135 | */ |
||
| 136 | 18 | protected function get_internal ($session_id) { |
|
| 159 | /** |
||
| 160 | * Check whether session was not expired, user agent and IP corresponds to what is expected and user is actually active |
||
| 161 | * |
||
| 162 | * @param mixed $session_data |
||
| 163 | * |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | 18 | protected function is_good_session ($session_data) { |
|
| 172 | /** |
||
| 173 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
| 174 | * |
||
| 175 | * @param string $session_id |
||
| 176 | * @param string $user_agent |
||
| 177 | * @param string $remote_addr |
||
| 178 | * @param string $ip |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | 2 | public function is_session_owner ($session_id, $user_agent, $remote_addr, $ip) { |
|
| 186 | /** |
||
| 187 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
| 188 | * |
||
| 189 | * @param array $session_data |
||
| 190 | * @param string|null $user_agent |
||
| 191 | * @param string|null $remote_addr |
||
| 192 | * @param string|null $ip |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 16 | protected function is_session_owner_internal ($session_data, $user_agent = null, $remote_addr = null, $ip = null) { |
|
| 216 | /** |
||
| 217 | * Load session by id and return id of session owner (user), update session expiration |
||
| 218 | * |
||
| 219 | * @param false|null|string $session_id If not specified - loaded from `$this->session_id`, and if that also empty - from cookies |
||
| 220 | * |
||
| 221 | * @return int User id |
||
| 222 | */ |
||
| 223 | 16 | public function load ($session_id = null) { |
|
| 249 | /** |
||
| 250 | * Initialize session (set user id, session id and update who user is) |
||
| 251 | * |
||
| 252 | * @param string $session_id |
||
| 253 | * @param int $user_id |
||
| 254 | * |
||
| 255 | * @return int User id |
||
| 256 | */ |
||
| 257 | 32 | protected function load_initialization ($session_id, $user_id) { |
|
| 263 | /** |
||
| 264 | * Whether profile is activated, not disabled and not blocked |
||
| 265 | * |
||
| 266 | * @param int $user |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | 32 | protected function is_user_active ($user) { |
|
| 279 | /** |
||
| 280 | * Create the session for the user with specified id |
||
| 281 | * |
||
| 282 | * @param int $user |
||
| 283 | * @param bool $delete_current_session |
||
| 284 | * |
||
| 285 | * @return false|string Session id on success, `false` otherwise |
||
| 286 | */ |
||
| 287 | 32 | public function add ($user, $delete_current_session = true) { |
|
| 316 | /** |
||
| 317 | * @param int $user |
||
| 318 | * |
||
| 319 | * @return array Session data |
||
| 320 | */ |
||
| 321 | 32 | protected function create_unique_session ($user) { |
|
| 349 | /** |
||
| 350 | * Destroying of the session |
||
| 351 | * |
||
| 352 | * @param null|string $session_id |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | 16 | public function del ($session_id = null) { |
|
| 386 | /** |
||
| 387 | * Delete all old sessions from DB |
||
| 388 | */ |
||
| 389 | 4 | protected function delete_old_sessions () { |
|
| 395 | /** |
||
| 396 | * Deletion of all user sessions |
||
| 397 | * |
||
| 398 | * @param false|int $user If not specified - current user assumed |
||
| 399 | * |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | 28 | public function del_all ($user = false) { |
|
| 425 | } |
||
| 426 |