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) { |
|
221 | /** |
||
222 | * Load session by id and return id of session owner (user), update session expiration |
||
223 | * |
||
224 | * @param false|null|string $session_id If not specified - loaded from `$this->session_id`, and if that also empty - from cookies |
||
225 | * |
||
226 | * @return int User id |
||
227 | */ |
||
228 | 16 | public function load ($session_id = null) { |
|
254 | /** |
||
255 | * Initialize session (set user id, session id and update who user is) |
||
256 | * |
||
257 | * @param string $session_id |
||
258 | * @param int $user_id |
||
259 | * |
||
260 | * @return int User id |
||
261 | */ |
||
262 | 32 | protected function load_initialization ($session_id, $user_id) { |
|
268 | /** |
||
269 | * Whether profile is activated, not disabled and not blocked |
||
270 | * |
||
271 | * @param int $user |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | 32 | protected function is_user_active ($user) { |
|
284 | /** |
||
285 | * Create the session for the user with specified id |
||
286 | * |
||
287 | * @param int $user |
||
288 | * @param bool $delete_current_session |
||
289 | * |
||
290 | * @return false|string Session id on success, `false` otherwise |
||
291 | */ |
||
292 | 32 | public function add ($user, $delete_current_session = true) { |
|
321 | /** |
||
322 | * @param int $user |
||
323 | * |
||
324 | * @return array Session data |
||
325 | */ |
||
326 | 32 | protected function create_unique_session ($user) { |
|
354 | /** |
||
355 | * Destroying of the session |
||
356 | * |
||
357 | * @param null|string $session_id |
||
358 | * |
||
359 | * @return bool |
||
360 | */ |
||
361 | 16 | public function del ($session_id = null) { |
|
391 | /** |
||
392 | * Delete all old sessions from DB |
||
393 | */ |
||
394 | 5 | protected function delete_old_sessions () { |
|
400 | /** |
||
401 | * Deletion of all user sessions |
||
402 | * |
||
403 | * @param false|int $user If not specified - current user assumed |
||
404 | * |
||
405 | * @return bool |
||
406 | */ |
||
407 | 28 | public function del_all ($user = false) { |
|
430 | } |
||
431 |