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 Intraface_User 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 Intraface_User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Intraface_User extends Intraface_Standard implements Intraface_Identity |
||
28 | { |
||
29 | /** |
||
30 | * @var db |
||
31 | */ |
||
32 | protected $db; |
||
33 | |||
34 | /** |
||
35 | * @var integer |
||
36 | */ |
||
37 | protected $id; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | public $value; |
||
43 | |||
44 | /** |
||
45 | * @var integer |
||
46 | */ |
||
47 | protected $intranet_id = 0; |
||
48 | |||
49 | /** |
||
50 | * @var error |
||
51 | */ |
||
52 | public $error; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $permissions = array(); |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $modules = array(); |
||
63 | |||
64 | /** |
||
65 | * @var address |
||
66 | */ |
||
67 | private $address; |
||
68 | |||
69 | /** |
||
70 | * @var boolean |
||
71 | */ |
||
72 | private $permissions_loaded = false; |
||
73 | |||
74 | /** |
||
75 | * Constructor |
||
76 | * |
||
77 | * @param integer $id User id |
||
78 | * |
||
79 | * @return void |
||
|
|||
80 | */ |
||
81 | 19 | public function __construct($id = 0) |
|
95 | |||
96 | 19 | public function getError() |
|
103 | |||
104 | /** |
||
105 | * Load |
||
106 | * |
||
107 | * @return void |
||
108 | */ |
||
109 | 19 | protected function load() |
|
124 | |||
125 | /** |
||
126 | * Gets the address object |
||
127 | * |
||
128 | * @return object |
||
129 | */ |
||
130 | 2 | public function getAddress() |
|
137 | |||
138 | /** |
||
139 | * Gets permissions |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | 2 | public function getPermissions() |
|
147 | |||
148 | /** |
||
149 | * Loads permissions |
||
150 | * |
||
151 | * @param integer $intranet_id |
||
152 | * |
||
153 | * @return boolean |
||
154 | */ |
||
155 | 9 | private function loadPermissions($intranet_id = null) |
|
180 | |||
181 | /** |
||
182 | * Gets module id from string |
||
183 | * |
||
184 | * @param integer $module |
||
185 | * |
||
186 | * @return integer |
||
187 | */ |
||
188 | 3 | private function getModuleIdFromString($module) |
|
206 | |||
207 | /** |
||
208 | * Clears cached permissions |
||
209 | * |
||
210 | * @return void |
||
211 | */ |
||
212 | 1 | public function clearCachedPermission() |
|
218 | |||
219 | /** |
||
220 | * Returns whether the permissions has been loaded |
||
221 | * |
||
222 | * @return boolean |
||
223 | */ |
||
224 | 3 | private function permissionsLoaded() |
|
228 | |||
229 | /** |
||
230 | * Returns whether the user has intranetaccess |
||
231 | * |
||
232 | * @param integer $intranet_id |
||
233 | * |
||
234 | * @return boolean |
||
235 | */ |
||
236 | 9 | public function hasIntranetAccess($intranet_id = 0) |
|
252 | |||
253 | /** |
||
254 | * Returns whether user has module Access |
||
255 | * |
||
256 | * @param integer $module |
||
257 | * @param integer $intranet_id |
||
258 | * |
||
259 | * @return integer |
||
260 | */ |
||
261 | 3 | public function hasModuleAccess($module, $intranet_id = 0) |
|
304 | |||
305 | /** |
||
306 | * Returns whether user has subaccess |
||
307 | * |
||
308 | * @param integer $module |
||
309 | * @param integer $sub_access |
||
310 | * @param integer intranet_id (n�r den skal tilg�s fra intranetmaintenance (til hvad?) |
||
311 | * |
||
312 | * @return boolean |
||
313 | */ |
||
314 | 1 | public function hasSubAccess($module, $sub_access, $intranet_id = 0) |
|
397 | |||
398 | /** |
||
399 | * Returns the active intranet |
||
400 | * |
||
401 | * @return integer |
||
402 | */ |
||
403 | 2 | public function getActiveIntranetId() |
|
431 | |||
432 | function getActiveIntranet() |
||
436 | |||
437 | function getSetting() |
||
441 | |||
442 | /** |
||
443 | * Sets intranet_id |
||
444 | * |
||
445 | * @todo what is this used for? |
||
446 | * |
||
447 | * @return boolean |
||
448 | */ |
||
449 | 4 | public function setIntranetId($id) |
|
458 | |||
459 | /** |
||
460 | * Sets active intranet_id |
||
461 | * |
||
462 | * @return boolean |
||
463 | */ |
||
464 | 2 | public function setActiveIntranetId($id) |
|
473 | |||
474 | /////////////////////////////////////////////////////////////////////////////// |
||
475 | |||
476 | /** |
||
477 | * Gets a list with the users intranets |
||
478 | * |
||
479 | * @return array |
||
480 | */ |
||
481 | 19 | public function getIntranetList() |
|
494 | |||
495 | /** |
||
496 | * Validates user info |
||
497 | * |
||
498 | * NOTICE: As it is created now, $input has to be injected by reference, |
||
499 | * because of the little hack with disabled. |
||
500 | * |
||
501 | * @param array $input |
||
502 | * |
||
503 | * @return boolean |
||
504 | */ |
||
505 | 17 | protected function validate(&$input) |
|
506 | { |
||
507 | 17 | $input = safeToDb($input); |
|
508 | 17 | $validator = new Intraface_Validator($this->error); |
|
509 | |||
510 | 17 | $validator->isEmail($input["email"], "Ugyldig E-mail"); |
|
511 | 17 | $result = $this->db->query("SELECT id FROM user WHERE email = \"".$input["email"]."\" AND id != ".$this->id); |
|
512 | 17 | if (PEAR::isError($result)) { |
|
513 | throw new Exception($result->getUserInfo()); |
||
514 | } |
||
515 | 17 | if ($result->numRows() > 0) { |
|
516 | $this->error->set("E-mail-adressen er allerede benyttet"); |
||
517 | } |
||
518 | |||
519 | 17 | if (isset($input["disabled"])) { |
|
520 | 17 | $input["disabled"] = 1; |
|
521 | 17 | } else { |
|
522 | $input["disabled"] = 0; |
||
523 | } |
||
524 | 17 | } |
|
525 | |||
526 | /** |
||
527 | * Updates the user |
||
528 | * |
||
529 | * @param array $input Data to update |
||
530 | * |
||
531 | * @return integer |
||
532 | */ |
||
533 | public function update($input) |
||
554 | |||
555 | function generateNewPassword($email) |
||
575 | |||
576 | 2 | public function updatePassword($old_password, $new_password, $repeat_password) |
|
602 | |||
603 | /** |
||
604 | * TODO M�ske kan det g�res enklere, s� der ikke skal bruges s� mange tabeller |
||
605 | */ |
||
606 | View Code Duplication | public function getList() |
|
621 | |||
622 | 1 | public function isFilledIn() |
|
629 | |||
630 | 2 | public function getId() |
|
634 | |||
635 | function getLanguage() |
||
639 | } |
||
640 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.