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_bot; |
||
47 | /** |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $is_guest; |
||
51 | /** |
||
52 | * Use cookie as source of session id, load session |
||
53 | * |
||
54 | * Bots detection is also done here |
||
55 | */ |
||
56 | protected function init_session () { |
||
71 | /** |
||
72 | * Try to determine whether visitor is a known bot, bots have no sessions |
||
73 | */ |
||
74 | protected function bots_detection () { |
||
102 | /** |
||
103 | * Get list of all bots |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | protected function all_bots () { |
||
130 | /** |
||
131 | * Check whether user agent and IP (login and email for bots) corresponds to passed bot data |
||
132 | * |
||
133 | * @param array $bot |
||
134 | * @param string $login |
||
135 | * @param string $email |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | protected function is_this_bot ($bot, $login, $email) { |
||
156 | /** |
||
157 | * Updates information about who is user accessed by methods ::guest() ::bot() ::user() admin() |
||
158 | */ |
||
159 | protected function update_user_is () { |
||
181 | /** |
||
182 | * Is admin |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | function admin () { |
||
189 | /** |
||
190 | * Is user |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | function user () { |
||
197 | /** |
||
198 | * Is guest |
||
199 | * |
||
200 | * @return bool |
||
201 | */ |
||
202 | function guest () { |
||
205 | /** |
||
206 | * Is bot |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | function bot () { |
||
213 | /** |
||
214 | * Returns id of current session |
||
215 | * |
||
216 | * @return false|string |
||
217 | */ |
||
218 | function get_id () { |
||
224 | /** |
||
225 | * Returns user id of current session |
||
226 | * |
||
227 | * @return int |
||
228 | */ |
||
229 | function get_user () { |
||
232 | /** |
||
233 | * Returns session details by session id |
||
234 | * |
||
235 | * @param false|null|string $session_id If `null` - loaded from `$this->session_id`, and if that also empty - from cookies |
||
236 | * |
||
237 | * @return false|array |
||
238 | */ |
||
239 | function get ($session_id) { |
||
244 | /** |
||
245 | * @param false|null|string $session_id |
||
246 | * |
||
247 | * @return false|array |
||
248 | */ |
||
249 | protected function get_internal ($session_id) { |
||
272 | /** |
||
273 | * Check whether session was not expired, user agent and IP corresponds to what is expected and user is actually active |
||
274 | * |
||
275 | * @param mixed $session_data |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | protected function is_good_session ($session_data) { |
||
285 | /** |
||
286 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
287 | * |
||
288 | * @param string $session_id |
||
289 | * @param string $user_agent |
||
290 | * @param string $remote_addr |
||
291 | * @param string $ip |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | function is_session_owner ($session_id, $user_agent, $remote_addr, $ip) { |
||
299 | /** |
||
300 | * Whether session data belongs to current visitor (user agent, remote addr and ip check) |
||
301 | * |
||
302 | * @param array $session_data |
||
303 | * @param string|null $user_agent |
||
304 | * @param string|null $remote_addr |
||
305 | * @param string|null $ip |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | protected function is_session_owner_internal ($session_data, $user_agent = null, $remote_addr = null, $ip = null) { |
||
329 | /** |
||
330 | * Load session by id and return id of session owner (user), update session expiration |
||
331 | * |
||
332 | * @param false|null|string $session_id If not specified - loaded from `$this->session_id`, and if that also empty - from cookies |
||
333 | * |
||
334 | * @return int User id |
||
335 | */ |
||
336 | function load ($session_id = null) { |
||
364 | /** |
||
365 | * Initialize session (set user id, session id and update who user is) |
||
366 | * |
||
367 | * @param string $session_id |
||
368 | * @param int $user_id |
||
369 | * |
||
370 | * @return int User id |
||
371 | */ |
||
372 | protected function load_initialization ($session_id, $user_id) { |
||
378 | /** |
||
379 | * Whether profile is activated, not disabled and not blocked |
||
380 | * |
||
381 | * @param int $user |
||
382 | * |
||
383 | * @return bool |
||
384 | */ |
||
385 | protected function is_user_active ($user) { |
||
429 | /** |
||
430 | * Create the session for the user with specified id |
||
431 | * |
||
432 | * @param int $user |
||
433 | * @param bool $delete_current_session |
||
434 | * |
||
435 | * @return false|string Session id on success, `false` otherwise |
||
436 | */ |
||
437 | function add ($user, $delete_current_session = true) { |
||
469 | /** |
||
470 | * @param int $user |
||
471 | * |
||
472 | * @return array Session data |
||
473 | */ |
||
474 | protected function create_unique_session ($user) { |
||
502 | /** |
||
503 | * Destroying of the session |
||
504 | * |
||
505 | * @param null|string $session_id |
||
506 | * |
||
507 | * @return bool |
||
508 | */ |
||
509 | function del ($session_id = null) { |
||
512 | /** |
||
513 | * Deletion of the session |
||
514 | * |
||
515 | * @param string|null $session_id |
||
516 | * @param bool $create_guest_session |
||
517 | * |
||
518 | * @return bool |
||
519 | */ |
||
520 | protected function del_internal ($session_id = null, $create_guest_session = true) { |
||
548 | /** |
||
549 | * Delete all old sessions from DB |
||
550 | */ |
||
551 | protected function delete_old_sessions () { |
||
557 | /** |
||
558 | * Deletion of all user sessions |
||
559 | * |
||
560 | * @param false|int $user If not specified - current user assumed |
||
561 | * |
||
562 | * @return bool |
||
563 | */ |
||
564 | function del_all ($user = false) { |
||
587 | } |
||
588 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.