Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like UserAccount 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 UserAccount, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class UserAccount |
||
29 | { |
||
30 | /** |
||
31 | * @var string|bool $session_key The session key |
||
32 | */ |
||
33 | private $session_key = false; |
||
34 | |||
35 | /** |
||
36 | * @var object $db The database connection object |
||
37 | */ |
||
38 | public $db; |
||
39 | |||
40 | /** |
||
41 | * Constructor |
||
42 | * @param object $db_connection The database connection object |
||
43 | * @param string $session_key The session key |
||
44 | */ |
||
45 | View Code Duplication | public function __construct($db_connection = false, $session_key = false) |
|
52 | |||
53 | /** |
||
54 | * Browse User Accounts |
||
55 | * |
||
56 | * Run a query to retreive all users in the database. |
||
57 | * |
||
58 | * @param string $sort_field The data value |
||
59 | * @param string $sort_order The data value |
||
60 | * @param int $start_record The data value |
||
61 | * @param int $stop_record The data value |
||
62 | * @param string $search The data value |
||
63 | * @param int $user_account_id The data value |
||
64 | * @return array|bool The query result |
||
65 | */ |
||
66 | public function browse_user_accounts( |
||
130 | |||
131 | /** |
||
132 | * Get Universal Administrator Emails |
||
133 | * |
||
134 | * Run a query to retrieve all administrator emails from the database. |
||
135 | * |
||
136 | * @return array|bool The query result |
||
137 | */ |
||
138 | public function get_universal_administrator_emails() |
||
147 | |||
148 | /** |
||
149 | * Get User Account Groups |
||
150 | * |
||
151 | * Run a query to retrieve all of a user's groups from the database. Used by get_user_group_roles_map(). |
||
152 | * |
||
153 | * @param int $user_account_id The data value |
||
154 | * @return array|bool The query result |
||
155 | */ |
||
156 | View Code Duplication | public function get_user_account_groups($user_account_id) |
|
168 | |||
169 | /** |
||
170 | * Get User Group Roles |
||
171 | * |
||
172 | * Run a query to retrieve all of a user's group roles from the database. Used by get_user_group_roles_map(). |
||
173 | * |
||
174 | * @param int $user_account_id The data value |
||
175 | * @param int $group_id The data value |
||
176 | * @return array|bool The query result |
||
177 | */ |
||
178 | View Code Duplication | public function get_user_group_roles($user_account_id, $group_id) |
|
191 | |||
192 | /** |
||
193 | * Get Roles |
||
194 | * |
||
195 | * Run a query to retrieve all roles from the database. |
||
196 | * |
||
197 | * @param array $exclude_ids The array |
||
198 | * @return array|bool The query result |
||
199 | */ |
||
200 | public function get_roles($exclude_ids = array()) |
||
215 | |||
216 | /** |
||
217 | * Get User Account Info |
||
218 | * |
||
219 | * Run a query to retrieve one user's account from the database. |
||
220 | * |
||
221 | * @param int $user_account_id The data value |
||
222 | * @return array|bool The query result |
||
223 | */ |
||
224 | View Code Duplication | public function get_user_account_info($user_account_id = false) |
|
236 | |||
237 | /** |
||
238 | * Get Addresses |
||
239 | * |
||
240 | * Run a query to retrieve one user's addresses from the database. |
||
241 | * |
||
242 | * @param int $user_account_id The data value |
||
243 | * @return array|bool The query result |
||
244 | */ |
||
245 | View Code Duplication | public function get_addresses($user_account_id = false) |
|
254 | |||
255 | /** |
||
256 | * Insert Addresses |
||
257 | * |
||
258 | * Run a query to insert addresses into the database. |
||
259 | * |
||
260 | * @param array $data The array |
||
261 | * @param int $user_account_id The data value |
||
262 | * @param int $editor_user_account_id The data value |
||
263 | * @return void |
||
264 | */ |
||
265 | public function insert_addresses($data, $user_account_id, $editor_user_account_id) |
||
325 | |||
326 | /** |
||
327 | * Insert/Update User Account |
||
328 | * |
||
329 | * Run queries to insert and update user accounts in the database. |
||
330 | * |
||
331 | * @uses UaserAccount::$this->delete_user_groups |
||
332 | * @param array $data The array |
||
333 | * @param int $user_account_id The data value |
||
334 | * @param bool $update_groups True/False |
||
335 | * @param int $proxy_role_id The data value |
||
336 | * @param bool $role_perm_manage_all_accounts_access The data value |
||
337 | * @return void |
||
338 | */ |
||
339 | public function insert_update_user_account( |
||
420 | |||
421 | /** |
||
422 | * Find User Account |
||
423 | * |
||
424 | * Run a query to search the database for user accounts. |
||
425 | * |
||
426 | * @param string $search The data value |
||
427 | * @return array|bool The query result |
||
428 | */ |
||
429 | View Code Duplication | public function find_user_account( $search ) |
|
444 | |||
445 | /** |
||
446 | * Delete User Account |
||
447 | * |
||
448 | * Run a query to delete a user account, and groups, from the database. |
||
449 | * |
||
450 | * @param int $user_account_id The data value |
||
451 | * @return void |
||
452 | */ |
||
453 | View Code Duplication | public function delete_user_account( $user_account_id ) |
|
464 | |||
465 | /** |
||
466 | * Delete User Groups |
||
467 | * |
||
468 | * Run a query to delete a user account's groups from the database. |
||
469 | * |
||
470 | * @param int $user_account_id The data value |
||
471 | * @return void |
||
472 | */ |
||
473 | public function delete_user_groups( $user_account_id ) |
||
480 | |||
481 | /** |
||
482 | * Get User Roles List |
||
483 | * |
||
484 | * Run a query to retrieve a user account's roles. |
||
485 | * |
||
486 | * @param int $user_account_id The data value |
||
487 | * @return array|bool The query result |
||
488 | */ |
||
489 | View Code Duplication | public function get_user_roles_list( $user_account_id ) |
|
498 | |||
499 | /** |
||
500 | * Update Acceptable Use Policy |
||
501 | * |
||
502 | * Run a query to update a user account's acceptable use policy. |
||
503 | * |
||
504 | * @param int $user_account_id The data value |
||
505 | * @param int $value The data value |
||
506 | * @return void |
||
507 | */ |
||
508 | public function update_acceptable_use_policy( $user_account_id, $value ) |
||
517 | |||
518 | /** |
||
519 | * Get User's Proxies For Groups |
||
520 | * |
||
521 | * Run a query to retrieve all the proxies that the user is associated with for a specific group. |
||
522 | * |
||
523 | * @param int $user_account_id The data value |
||
524 | * @param int $group_id The data value |
||
525 | * @return array|bool The query result |
||
526 | */ |
||
527 | View Code Duplication | public function get_users_proxies_for_group( $user_account_id, $group_id ) |
|
542 | |||
543 | /** |
||
544 | * Get User Group Roles Map |
||
545 | * |
||
546 | * Run queries to retrieve a user's current group values. |
||
547 | * |
||
548 | * @uses UserAccount::$this->get_user_account_groups |
||
549 | * @uses UserAccount::$this->get_user_group_roles |
||
550 | * @uses UserAccount::$this->get_users_proxies_for_group |
||
551 | * @param int $user_account_id The data value |
||
552 | * @param int $proxy_id The data value |
||
553 | * @return array|bool The query result |
||
554 | */ |
||
555 | public function get_user_group_roles_map( $user_account_id, $proxy_id = false ) |
||
578 | |||
579 | /** |
||
580 | * Has Role |
||
581 | * |
||
582 | * Run a query to determine if a user has a role. |
||
583 | * If assigned a role for a group, that role applies to all of that group's decendants. |
||
584 | * |
||
585 | * @param int $user_account_id The data value |
||
586 | * @param array $roles The data value |
||
587 | * @param int $group_id The data value |
||
588 | * @return array|bool The query result |
||
589 | */ |
||
590 | public function has_role( $user_account_id, $roles = array(), $group_id = false ) |
||
603 | |||
604 | } |
||
605 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: